Skip to content

feat: add getters to mirror those available on std::process::Command #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,102 @@ impl Command {

self.cmd.spawn()
}

/// Returns the path to the program that was given to [`Command::new`].
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// use assert_cmd::Command;
///
/// let cmd = Command::new("echo");
/// assert_eq!(cmd.get_program(), "echo");
/// ```
pub fn get_program(&self) -> &ffi::OsStr {
self.cmd.get_program()
}

/// Returns an iterator of the arguments that will be passed to the program.
///
/// This does not include the path to the program as the first argument;
/// it only includes the arguments specified with [`Command::arg`] and
/// [`Command::args`].
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// use std::ffi::OsStr;
/// use assert_cmd::Command;
///
/// let mut cmd = Command::new("echo");
/// cmd.arg("first").arg("second");
/// let args: Vec<&OsStr> = cmd.get_args().collect();
/// assert_eq!(args, &["first", "second"]);
/// ```
pub fn get_args(&self) -> process::CommandArgs<'_> {
self.cmd.get_args()
}

/// Returns an iterator of the environment variables explicitly set for the child process.
///
/// Environment variables explicitly set using [`Command::env`], [`Command::envs`], and
/// [`Command::env_remove`] can be retrieved with this method.
///
/// Note that this output does not include environment variables inherited from the parent
/// process.
///
/// Each element is a tuple key/value pair `(&OsStr, Option<&OsStr>)`. A [`None`] value
/// indicates its key was explicitly removed via [`Command::env_remove`]. The associated key for
/// the [`None`] value will no longer inherit from its parent process.
///
/// An empty iterator can indicate that no explicit mappings were added or that
/// [`Command::env_clear`] was called. After calling [`Command::env_clear`], the child process
/// will not inherit any environment variables from its parent process.
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// use std::ffi::OsStr;
/// use assert_cmd::Command;
///
/// let mut cmd = Command::new("ls");
/// cmd.env("TERM", "dumb").env_remove("TZ");
/// let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
/// assert_eq!(envs, &[
/// (OsStr::new("TERM"), Some(OsStr::new("dumb"))),
/// (OsStr::new("TZ"), None)
/// ]);
/// ```
pub fn get_envs(&self) -> process::CommandEnvs<'_> {
self.cmd.get_envs()
}

/// Returns the working directory for the child process.
///
/// This returns [`None`] if the working directory will not be changed.
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// use std::path::Path;
/// use assert_cmd::Command;
///
/// let mut cmd = Command::new("ls");
/// assert_eq!(cmd.get_current_dir(), None);
/// cmd.current_dir("/bin");
/// assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));
/// ```
pub fn get_current_dir(&self) -> Option<&path::Path> {
self.cmd.get_current_dir()
}
}

impl From<process::Command> for Command {
Expand Down
Loading