Struct snapbox::cmd::Command

source ·
pub struct Command { /* private fields */ }
Expand description

Process spawning for testing of non-interactive commands

Implementations§

source§

impl Command

source

pub fn new(program: impl AsRef<OsStr>) -> Self

source

pub fn from_std(cmd: Command) -> Self

Constructs a new Command from a std Command.

source

pub fn with_assert(self, config: Assert) -> Self

Customize the assertion behavior

source

pub fn arg(self, arg: impl AsRef<OsStr>) -> Self

Adds an argument to pass to the program.

Only one argument can be passed per use. So instead of:

.arg("-C /path/to/repo")

usage would be:

.arg("-C")
.arg("/path/to/repo")

To pass multiple arguments see args.

Examples

Basic usage:

use snapbox::cmd::Command;

Command::new("ls")
        .arg("-l")
        .arg("-a")
        .assert()
        .success();
source

pub fn args(self, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> Self

Adds multiple arguments to pass to the program.

To pass a single argument see arg.

Examples

Basic usage:

use snapbox::cmd::Command;

Command::new("ls")
        .args(&["-l", "-a"])
        .assert()
        .success();
source

pub fn env(self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self

Inserts or updates an environment variable mapping.

Note that environment variable names are case-insensitive (but case-preserving) on Windows, and case-sensitive on all other platforms.

Examples

Basic usage:

use snapbox::cmd::Command;

Command::new("ls")
        .env("PATH", "/bin")
        .assert()
        .failure();
source

pub fn envs( self, vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)> ) -> Self

Adds or updates multiple environment variable mappings.

Examples

Basic usage:

use snapbox::cmd::Command;
use std::process::Stdio;
use std::env;
use std::collections::HashMap;

let filtered_env : HashMap<String, String> =
    env::vars().filter(|&(ref k, _)|
        k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH"
    ).collect();

Command::new("printenv")
        .env_clear()
        .envs(&filtered_env)
        .assert()
        .success();
source

pub fn env_remove(self, key: impl AsRef<OsStr>) -> Self

Removes an environment variable mapping.

Examples

Basic usage:

use snapbox::cmd::Command;

Command::new("ls")
        .env_remove("PATH")
        .assert()
        .failure();
source

pub fn env_clear(self) -> Self

Clears the entire environment map for the child process.

Examples

Basic usage:

use snapbox::cmd::Command;

Command::new("ls")
        .env_clear()
        .assert()
        .failure();
source

pub fn current_dir(self, dir: impl AsRef<Path>) -> Self

Sets the working directory for the child process.

Platform-specific behavior

If the program path is relative (e.g., "./script.sh"), it’s ambiguous whether it should be interpreted relative to the parent’s working directory or relative to current_dir. The behavior in this case is platform specific and unstable, and it’s recommended to use canonicalize to get an absolute program path instead.

Examples

Basic usage:

use snapbox::cmd::Command;

Command::new("ls")
        .current_dir("/bin")
        .assert()
        .success();
source

pub fn stdin(self, stream: impl Into<Data>) -> Self

Write buffer to stdin when the Command is run.

Examples
use snapbox::cmd::Command;

let mut cmd = Command::new("cat")
    .arg("-et")
    .stdin("42")
    .assert()
    .stdout_eq("42");
source

pub fn timeout(self, timeout: Duration) -> Self

Available on crate feature cmd only.

Error out if a timeout is reached

use snapbox::cmd::Command;
use snapbox::cmd::cargo_bin;

let assert = Command::new(cargo_bin("snap-fixture"))
    .timeout(std::time::Duration::from_secs(1))
    .env("sleep", "100")
    .assert()
    .failure();
source

pub fn stderr_to_stdout(self) -> Self

Available on crate feature cmd only.

Merge stderr into stdout

source§

impl Command

source

pub fn assert(self) -> OutputAssert

Run the command and assert on the results

use snapbox::cmd::Command;

let mut cmd = Command::new("cat")
    .arg("-et")
    .stdin("42")
    .assert()
    .stdout_eq("42");
source

pub fn output(self) -> Result<Output, Error>

Available on crate feature cmd only.

Run the command and capture the Output

Trait Implementations§

source§

impl Debug for Command

source§

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

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

impl From<Command> for Command

source§

fn from(cmd: Command) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.