Skip to content

Commit

Permalink
Add constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-tkach committed May 5, 2020
1 parent 9ad1c3e commit 981e665
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,54 @@ where
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
/// [`cargo`]: index.html
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError>;

/// Create a [`Command`] to run a specific binary of the current crate using absolute
/// path.
///
/// # Examples
///
/// ```rust,ignore
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// let mut cmd = Command::absolute_path(env!("CARGO_BIN_EXE_bin_fixture"))
/// .unwrap();
/// let output = cmd.unwrap();
/// println!("{:?}", output);
/// ```
///
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
fn absolute_path<T: Into<path::PathBuf>>(path: T) -> Result<Self, CargoError>;
}

impl CommandCargoExt for crate::cmd::Command {
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError> {
crate::cmd::Command::cargo_bin(name)
}

fn absolute_path<T: Into<path::PathBuf>>(path: T) -> Result<Self, CargoError> {
crate::cmd::Command::absolute_path(path)
}
}

impl CommandCargoExt for process::Command {
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError> {
cargo_bin_cmd(name)
}

fn absolute_path<T: Into<path::PathBuf>>(path: T) -> Result<Self, CargoError> {
from_path(path)
}
}

pub(crate) fn cargo_bin_cmd<S: AsRef<str>>(name: S) -> Result<process::Command, CargoError> {
let path = cargo_bin(name);
from_path(path)
}

pub(crate) fn from_path<T: Into<path::PathBuf>>(path: T) -> Result<process::Command, CargoError> {
let path = path.into();
if path.is_file() {
Ok(process::Command::new(path))
} else {
Expand Down
24 changes: 24 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ impl Command {
Ok(Self::from_std(cmd))
}

/// Create a [`Command`] to run a specific binary of the current crate using absolute
/// path.
///
/// # Examples
///
/// ```rust,ignore
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// let mut cmd = Command::absolute_path(env!("CARGO_BIN_EXE_bin_fixture"))
/// .unwrap();
/// let output = cmd.unwrap();
/// println!("{:?}", output);
/// ```
///
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
pub fn absolute_path<T: Into<path::PathBuf>>(
path: T
) -> Result<Self, crate::cargo::CargoError> {
let cmd = crate::cargo::from_path(path)?;
Ok(Self::from_std(cmd))
}

/// Write `buffer` to `stdin` when the `Command` is run.
///
/// # Examples
Expand Down
7 changes: 7 additions & 0 deletions tests/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ fn cargo_bin_example_2() {
let output = cmd.unwrap();
println!("{:?}", output);
}

#[test]
fn cargo_bin_exe() {
let cmd = Command::absolute_path(env!("CARGO_BIN_EXE_bin_fixture"));
let output = cmd.unwrap();
println!("{:?}", output);
}

0 comments on commit 981e665

Please sign in to comment.