pub struct Command { /* private fields */ }
Expand description
Process spawning for testing of non-interactive commands
Implementations§
source§impl Command
impl Command
pub fn new(program: impl AsRef<OsStr>) -> Self
sourcepub fn with_assert(self, config: Assert) -> Self
pub fn with_assert(self, config: Assert) -> Self
Customize the assertion behavior
sourcepub fn arg(self, arg: impl AsRef<OsStr>) -> Self
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();
sourcepub fn args(self, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> Self
pub fn args(self, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> Self
sourcepub fn env(self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Self
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();
sourcepub fn envs(
self,
vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>
) -> Self
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();
sourcepub fn env_remove(self, key: impl AsRef<OsStr>) -> Self
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();
sourcepub fn env_clear(self) -> Self
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();
sourcepub fn current_dir(self, dir: impl AsRef<Path>) -> Self
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();
sourcepub fn stdin(self, stream: impl Into<Data>) -> Self
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");
sourcepub fn timeout(self, timeout: Duration) -> Self
pub fn timeout(self, timeout: Duration) -> Self
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();
sourcepub fn stderr_to_stdout(self) -> Self
pub fn stderr_to_stdout(self) -> Self
Merge stderr
into stdout