Assert

Struct Assert 

Source
pub struct Assert { /* private fields */ }
Expand description

Assertions for a specific command.

Implementations§

Source§

impl Assert

Source

pub fn main_binary() -> Self

Run the crate’s main binary.

Defaults to asserting successful execution.

Source

pub fn cargo_binary<S: AsRef<OsStr>>(name: S) -> Self

Run a specific binary of the current crate.

Defaults to asserting successful execution.

Source

pub fn example<S: AsRef<OsStr>>(name: S) -> Self

Run a specific example of the current crate.

Defaults to asserting successful execution.

Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

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();
Source

pub fn execute(self) -> Result<(), AssertionError>

Execute the command and check the assertions.

§Examples
extern crate assert_cli;

let test = assert_cli::Assert::command(&["echo", "42"])
    .stdout().contains("42")
    .execute();
assert!(test.is_ok());
Source

pub fn unwrap(self)

Execute the command, check the assertions, and panic when they fail.

§Examples
extern crate assert_cli;

assert_cli::Assert::command(&["echo", "42"])
    .fails()
    .unwrap(); // panics

Trait Implementations§

Source§

impl Debug for Assert

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Assert

Source§

fn default() -> Self

Construct an assert using cargo run -- as command.

Defaults to asserting successful execution.

Auto Trait Implementations§

§

impl Freeze for Assert

§

impl !RefUnwindSafe for Assert

§

impl !Send for Assert

§

impl !Sync for Assert

§

impl Unpin for Assert

§

impl !UnwindSafe for Assert

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.