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

Process spawning for testing of non-interactive commands

Implementations

Constructs a new Command from a std Command.

Customize the assertion behavior

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

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

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

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

Removes an environment variable mapping.

Examples

Basic usage:

use snapbox::cmd::Command;

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

Clears the entire environment map for the child process.

Examples

Basic usage:

use snapbox::cmd::Command;

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

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

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");
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();
Available on crate feature cmd only.

Merge stderr into stdout

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");
Available on crate feature cmd only.

Run the command and capture the Output

Trait Implementations

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.