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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
//! **Assert [`Command`]** - Easy command initialization and assertions. //! //! `assert_cmd` includes support for: //! - Setting up your program-under-test (see [`CommandCargoExt`], [`CommandStdInExt`]). //! - Verifying your program-under-test (see [`OutputOkExt`], [`OutputAssertExt`]). //! //! ```toml //! [dependencies] //! assert_cmd = "0.11" //! ``` //! //! ## Overview //! //! Create a [`Command`]: //! - `Command::new(path)`, see [`Command`] //! - `Command::cargo_bin(name)`, see [`CommandCargoExt`] //! //! Configure a [`Command`]: //! - `arg` / `args`, see [`Command`] //! - `current_dir`, see [`Command`] //! - `env` / `envs` / `env_remove` / `env_clear`, see [`Command`] //! - `with_stdin`, see [`CommandStdInExt`] //! //! Validate either a [`Command`] or `Output`: //! - `ok` / `unwrap` / `unwrap_err`, see [`OutputOkExt`] //! - `assert` ([`OutputAssertExt`]) //! - `success`, see [`Assert`] //! - `failure`, see [`Assert`] //! - `interrupted`, see [`Assert`] //! - `code`, see [`Assert`] //! - `stdout`, see [`Assert`] //! - `stderr`, see [`Assert`] //! //! ## Examples //! //! Here's a trivial example: //! ```rust,no_run //! extern crate assert_cmd; //! //! use std::process::Command; //! use assert_cmd::prelude::*; //! //! fn main() { //! let mut cmd = Command::cargo_bin("bin_fixture").unwrap(); //! cmd.assert().success(); //! } //! ``` //! //! And a little of everything: //! ```rust,no_run //! extern crate assert_cmd; //! //! use std::process::Command; //! use assert_cmd::prelude::*; //! //! fn main() { //! let mut cmd = Command::cargo_bin("bin_fixture").unwrap(); //! cmd //! .arg("-A") //! .env("stdout", "hello") //! .env("exit", "42") //! .with_stdin() //! .buffer("42"); //! let assert = cmd.assert(); //! assert //! .failure() //! .code(42) //! .stdout("hello\n"); //! } //! ``` //! //! ## Relevant crates //! //! Other crates that might be useful in testing command line programs. //! - [duct] for orchestrating multiple processes. //! - [commandspec] for easier writing of commands //! - [assert_fs] for filesystem fixtures and assertions. //! - [dir-diff] for testing file side-effects. //! - [tempfile] for scratchpad directories. //! //! ## Migrating from `assert_cli` v0.6 //! //! `assert_cmd` is the successor to [the original `assert_cli`][assert_cli]: //! - More flexible, reusable assertions (also used by [assert_fs]). //! - Can integrate with other process-management crates, like `duct`. //! - Addresses several architectural problems. //! //! Key points in migrating from `assert_cli`: //! - [`Command`] is extended with traits rather than being wrapping in custom logic. //! - The command-under-test is run eagerly, with assertions happening immediately. //! - [`success()`] is not implicit and requires being explicitly called. //! - `stdout`/`stderr` aren't automatically trimmed before being passed to the `Predicate`. //! //! [commandspec]: https://crates.io/crates/commandspec //! [assert_cli]: https://crates.io/crates/assert_cli/0.6.3 //! [dir-diff]: https://crates.io/crates/dir-diff //! [tempfile]: https://crates.io/crates/tempfile //! [duct]: https://crates.io/crates/duct //! [assert_fs]: https://crates.io/crates/assert_fs //! [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html //! [`Assert`]: assert/struct.Assert.html //! [`success()`]: assert/struct.Assert.html#method.success //! [`CommandCargoExt`]: cargo/trait.CommandCargoExt.html //! [`CommandStdInExt`]: stdin/trait.CommandStdInExt.html //! [`OutputOkExt`]: cmd/trait.OutputOkExt.html //! [`OutputAssertExt`]: assert/trait.OutputAssertExt.html #![warn(missing_docs)] extern crate escargot; extern crate predicates; extern crate predicates_core; extern crate predicates_tree; /// Allows you to pull the name from your Cargo.toml at compile time. /// /// # Examples /// /// ```should_panic /// #[macro_use] /// extern crate assert_cmd; /// /// use std::process::Command; /// use assert_cmd::prelude::*; /// /// fn main() { /// let mut cmd = Command::cargo_bin(crate_name!()).unwrap(); /// cmd /// .arg("-A") /// .env("stdout", "hello") /// .env("exit", "42") /// .with_stdin() /// .buffer("42"); /// let assert = cmd.assert(); /// assert /// .failure() /// .code(42) /// .stdout("hello\n"); /// } /// ``` #[cfg(not(feature = "no_cargo"))] #[macro_export] macro_rules! crate_name { () => { env!("CARGO_PKG_NAME") }; } pub mod assert; pub mod cargo; pub mod cmd; pub mod stdin; /// Extension traits that are useful to have available. pub mod prelude { pub use assert::OutputAssertExt; pub use cargo::CommandCargoExt; pub use cmd::OutputOkExt; pub use stdin::CommandStdInExt; }