Skip to content

Commit b037f96

Browse files
committed
feat: Add getters to mirror those available on std::process::Command
adds `get_program`, `get_args`, `get_envs` and `get_current_dir` which all wrap their `std` counterparts
1 parent 4d3dbd3 commit b037f96

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

src/cmd.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,102 @@ impl Command {
500500

501501
self.cmd.spawn()
502502
}
503+
504+
/// Returns the path to the program that was given to [`Command::new`].
505+
///
506+
/// # Examples
507+
///
508+
/// Basic usage:
509+
///
510+
/// ```rust
511+
/// use assert_cmd::Command;
512+
///
513+
/// let cmd = Command::new("echo");
514+
/// assert_eq!(cmd.get_program(), "echo");
515+
/// ```
516+
pub fn get_program(&self) -> &ffi::OsStr {
517+
self.cmd.get_program()
518+
}
519+
520+
/// Returns an iterator of the arguments that will be passed to the program.
521+
///
522+
/// This does not include the path to the program as the first argument;
523+
/// it only includes the arguments specified with [`Command::arg`] and
524+
/// [`Command::args`].
525+
///
526+
/// # Examples
527+
///
528+
/// Basic usage:
529+
///
530+
/// ```rust
531+
/// use std::ffi::OsStr;
532+
/// use assert_cmd::Command;
533+
///
534+
/// let mut cmd = Command::new("echo");
535+
/// cmd.arg("first").arg("second");
536+
/// let args: Vec<&OsStr> = cmd.get_args().collect();
537+
/// assert_eq!(args, &["first", "second"]);
538+
/// ```
539+
pub fn get_args(&self) -> process::CommandArgs<'_> {
540+
self.cmd.get_args()
541+
}
542+
543+
/// Returns an iterator of the environment variables explicitly set for the child process.
544+
///
545+
/// Environment variables explicitly set using [`Command::env`], [`Command::envs`], and
546+
/// [`Command::env_remove`] can be retrieved with this method.
547+
///
548+
/// Note that this output does not include environment variables inherited from the parent
549+
/// process.
550+
///
551+
/// Each element is a tuple key/value pair `(&OsStr, Option<&OsStr>)`. A [`None`] value
552+
/// indicates its key was explicitly removed via [`Command::env_remove`]. The associated key for
553+
/// the [`None`] value will no longer inherit from its parent process.
554+
///
555+
/// An empty iterator can indicate that no explicit mappings were added or that
556+
/// [`Command::env_clear`] was called. After calling [`Command::env_clear`], the child process
557+
/// will not inherit any environment variables from its parent process.
558+
///
559+
/// # Examples
560+
///
561+
/// Basic usage:
562+
///
563+
/// ```rust
564+
/// use std::ffi::OsStr;
565+
/// use assert_cmd::Command;
566+
///
567+
/// let mut cmd = Command::new("ls");
568+
/// cmd.env("TERM", "dumb").env_remove("TZ");
569+
/// let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
570+
/// assert_eq!(envs, &[
571+
/// (OsStr::new("TERM"), Some(OsStr::new("dumb"))),
572+
/// (OsStr::new("TZ"), None)
573+
/// ]);
574+
/// ```
575+
pub fn get_envs(&self) -> process::CommandEnvs<'_> {
576+
self.cmd.get_envs()
577+
}
578+
579+
/// Returns the working directory for the child process.
580+
///
581+
/// This returns [`None`] if the working directory will not be changed.
582+
///
583+
/// # Examples
584+
///
585+
/// Basic usage:
586+
///
587+
/// ```rust
588+
/// use std::path::Path;
589+
/// use assert_cmd::Command;
590+
///
591+
/// let mut cmd = Command::new("ls");
592+
/// assert_eq!(cmd.get_current_dir(), None);
593+
/// cmd.current_dir("/bin");
594+
/// assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));
595+
/// ```
596+
pub fn get_current_dir(&self) -> Option<&path::Path> {
597+
self.cmd.get_current_dir()
598+
}
503599
}
504600

505601
impl From<process::Command> for Command {

0 commit comments

Comments
 (0)