1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
//! # Test CLI Applications //! //! This crate's goal is to provide you some very easy tools to test your CLI //! applications. It can currently execute child processes and validate their //! exit status as well as stdout and stderr output against your assertions. //! //! Include the crate like //! //! ```rust //! #[macro_use] // <-- import the convenience macro (optional) //! extern crate assert_cli; //! # fn main() { } //! ``` //! //! ## Basic Examples //! //! Here's a trivial example: //! //! ```rust //! assert_cli::Assert::command(&["echo", "42"]) //! .stdout().contains("42") //! .unwrap(); //! ``` //! //! And here is one that will fail: //! //! ```rust,should_panic //! assert_cli::Assert::command(&["echo", "42"]) //! .stdout().is("1337") //! .unwrap(); //! ``` //! //! this will show a nice, colorful diff in your terminal, like this: //! //! ```diff //! -1337 //! +42 //! ``` //! //! ## `assert_cmd!` Macro //! //! Alternatively, you can use the `assert_cmd!` macro to construct the command more conveniently, //! but please carefully read the limitations below, or this may seriously go wrong. //! //! ```rust //! # #[macro_use] extern crate assert_cli; //! # fn main() { //! assert_cmd!(echo "42").stdout().contains("42").unwrap(); //! # } //! ``` //! //! **Tips** //! //! - Don't forget to import the crate with `#[macro_use]`. ;-) //! - Enclose arguments in the `assert_cmd!` macro in quotes `"`, //! if there are special characters, which the macro doesn't accept, e.g. //! `assert_cmd!(cat "foo.txt")`. //! //! ## Exit Status //! //! All assertion default to checking that the command exited with success. //! //! However, when you expect a command to fail, you can express it like this: //! //! ```rust //! # #[macro_use] extern crate assert_cli; //! # fn main() { //! assert_cmd!(cat "non-existing-file") //! .fails() //! .unwrap(); //! # } //! ``` //! //! Some notes on this: //! //! - Use `fails_with` to assert a specific exit status. //! - There is also a `succeeds` method, but this is already the implicit default //! and can usually be omitted. //! - The `and` method has no effect, other than to make everything more readable. //! Feel free to use it. :-) //! //! ## stdout / stderr //! //! You can add assertions on the content of **stdout** and **stderr**. They //! can be mixed together or even multiple of one stream can be used. //! //! ```rust //! # #[macro_use] extern crate assert_cli; //! # fn main() { //! assert_cmd!(echo "Hello world! The ansswer is 42.") //! .stdout().contains("Hello world") //! .stdout().contains("42") //! .stderr().is("") //! .unwrap(); //! # } //! ``` //! //! ## Assert CLI Crates //! //! If you are testing a Rust binary crate, you can start with //! `Assert::main_binary()` to use `cargo run` as command. Or, if you want to //! run a specific binary (if you have more than one), use //! `Assert::cargo_binary`. //! //! ## Don't Panic! //! //! If you don't want it to panic when the assertions are not met, simply call //! `.execute` instead of `.unwrap` to get a `Result`: //! //! ```rust //! # #[macro_use] extern crate assert_cli; //! # fn main() { //! let x = assert_cmd!(echo "1337").stdout().is("42").execute(); //! assert!(x.is_err()); //! # } //! ``` #![deny(missing_docs)] extern crate colored; extern crate difference; extern crate environment; #[macro_use] extern crate failure; #[macro_use] extern crate failure_derive; extern crate serde_json; mod errors; pub use errors::AssertionError; #[macro_use] mod macros; pub use macros::flatten_escaped_string; mod assert; mod diff; mod output; pub use assert::Assert; pub use assert::OutputAssertionBuilder; /// Environment is a re-export of the Environment crate /// /// It allow you to define/override environment variables for one or more assertions. pub use environment::Environment;