pub struct Command { /* private fields */ }Expand description
Process spawning for testing of non-interactive commands
Implementations§
Source§impl Command
§Builder API
impl Command
§Builder API
pub fn new(program: impl AsRef<OsStr>) -> Command
Sourcepub fn with_assert(self, config: Assert) -> Command
pub fn with_assert(self, config: Assert) -> Command
Customize the assertion behavior
Sourcepub fn arg(self, arg: impl AsRef<OsStr>) -> Command
pub fn arg(self, arg: impl AsRef<OsStr>) -> Command
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 env(self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Command
pub fn env(self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> Command
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>)>,
) -> Command
pub fn envs( self, vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>, ) -> Command
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>) -> Command
pub fn env_remove(self, key: impl AsRef<OsStr>) -> Command
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) -> Command
pub fn env_clear(self) -> Command
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>) -> Command
pub fn current_dir(self, dir: impl AsRef<Path>) -> Command
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();Trait Implementations§
Source§impl ArgLineCommandExt for Command
impl ArgLineCommandExt for Command
Source§impl ChannelChangerCommandExt for Command
impl ChannelChangerCommandExt for Command
Source§fn masquerade_as_nightly_cargo(self, _reasons: &[&str]) -> Self
fn masquerade_as_nightly_cargo(self, _reasons: &[&str]) -> Self
&["print-im-a-teapot"].Auto Trait Implementations§
impl Freeze for Command
impl !RefUnwindSafe for Command
impl Send for Command
impl Sync for Command
impl Unpin for Command
impl UnsafeUnpin for Command
impl !UnwindSafe for Command
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more