pub struct Assert { /* private fields */ }Expand description
Assertions for a specific command.
Implementations§
Source§impl Assert
impl Assert
Sourcepub fn main_binary() -> Self
pub fn main_binary() -> Self
Run the crate’s main binary.
Defaults to asserting successful execution.
Sourcepub fn cargo_binary<S: AsRef<OsStr>>(name: S) -> Self
pub fn cargo_binary<S: AsRef<OsStr>>(name: S) -> Self
Run a specific binary of the current crate.
Defaults to asserting successful execution.
Sourcepub fn example<S: AsRef<OsStr>>(name: S) -> Self
pub fn example<S: AsRef<OsStr>>(name: S) -> Self
Run a specific example of the current crate.
Defaults to asserting successful execution.
Sourcepub fn command<S: AsRef<OsStr>>(cmd: &[S]) -> Self
pub fn command<S: AsRef<OsStr>>(cmd: &[S]) -> Self
Run a custom command.
Defaults to asserting successful execution.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["echo", "1337"])
.unwrap();Sourcepub fn with_args<S: AsRef<OsStr>>(self, args: &[S]) -> Self
pub fn with_args<S: AsRef<OsStr>>(self, args: &[S]) -> Self
Add arguments to the command.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["echo"])
.with_args(&["42"])
.stdout().contains("42")
.unwrap();
Sourcepub fn stdin<S: Into<Vec<u8>>>(self, contents: S) -> Self
pub fn stdin<S: Into<Vec<u8>>>(self, contents: S) -> Self
Add stdin to the command.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["cat"])
.stdin("42")
.stdout().contains("42")
.unwrap();Sourcepub fn current_dir<P: Into<PathBuf>>(self, dir: P) -> Self
pub fn current_dir<P: Into<PathBuf>>(self, dir: P) -> Self
Sets the working directory for the command.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["wc", "lib.rs"])
.current_dir(std::path::Path::new("src"))
.stdout().contains("lib.rs")
.execute()
.unwrap();Sourcepub fn with_env<E: Into<Environment>>(self, env: E) -> Self
pub fn with_env<E: Into<Environment>>(self, env: E) -> Self
Sets environments variables for the command.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["printenv"])
.with_env(&[("TEST_ENV", "OK")])
.stdout().contains("TEST_ENV=OK")
.execute()
.unwrap();
let env = assert_cli::Environment::empty()
.insert("FOO", "BAR");
assert_cli::Assert::command(&["printenv"])
.with_env(&env)
.stdout().is("FOO=BAR")
.execute()
.unwrap();
::std::env::set_var("BAZ", "BAR");
assert_cli::Assert::command(&["printenv"])
.stdout().contains("BAZ=BAR")
.execute()
.unwrap();Sourcepub fn and(self) -> Self
pub fn and(self) -> Self
Small helper to make chains more readable.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["cat", "non-existing-file"])
.fails()
.and()
.stderr().contains("non-existing-file")
.unwrap();Sourcepub fn succeeds(self) -> Self
pub fn succeeds(self) -> Self
Expect the command to be executed successfully.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["echo", "42"])
.succeeds()
.unwrap();Sourcepub fn fails(self) -> Self
pub fn fails(self) -> Self
Expect the command to fail.
Note: This does not include shell failures like command not found. I.e. the
command must run and fail for this assertion to pass.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["cat", "non-existing-file"])
.fails()
.and()
.stderr().contains("non-existing-file")
.unwrap();Sourcepub fn fails_with(self, expect_exit_code: i32) -> Self
pub fn fails_with(self, expect_exit_code: i32) -> Self
Expect the command to fail and return a specific error code.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["cat", "non-existing-file"])
.fails_with(1)
.and()
.stderr().is("cat: non-existing-file: No such file or directory")
.unwrap();Sourcepub fn ignore_status(self) -> Self
pub fn ignore_status(self) -> Self
Do not care whether the command exits successfully or if it fails.
This function removes any assertions that were already set, including
any expected exit code that was set with fails_with.
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["cat", "non-existing-file"])
.ignore_status()
.and()
.stderr().is("cat: non-existing-file: No such file or directory")
.unwrap();Sourcepub fn stdout(self) -> OutputAssertionBuilder
pub fn stdout(self) -> OutputAssertionBuilder
Create an assertion for stdout’s contents
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["echo", "42"])
.stdout().contains("42")
.unwrap();Sourcepub fn stderr(self) -> OutputAssertionBuilder
pub fn stderr(self) -> OutputAssertionBuilder
Create an assertion for stdout’s contents
§Examples
extern crate assert_cli;
assert_cli::Assert::command(&["cat", "non-existing-file"])
.fails_with(1)
.and()
.stderr().is("cat: non-existing-file: No such file or directory")
.unwrap();