Skip to content
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

Add ability to run bin and example from other crates in workspaces #67

Closed
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ name = "bin_fixture"
predicates = { version = "1.0", default-features = false, features = ["difference"] }
predicates-core = "1.0"
predicates-tree = "1.0"
escargot = "0.3"
escargot = "0.4"

[dev-dependencies]
docmatic = "0.1"
62 changes: 62 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,46 @@ where
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
/// [`cargo`]: index.html
fn cargo_example<S: AsRef<ffi::OsStr>>(name: S) -> Result<Self, CargoError>;

/// Create a [`Command`] to run a specific binary of a specific crate from workspaces.
///
/// See the [`cargo` module documentation][`cargo`] for caveats and workarounds.
///
/// # Examples
///
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// let mut cmd = Command::cargo_crate_bin("assert_cmd", "bin_fixture")
/// .unwrap();
/// let output = cmd.unwrap();
/// ```
///
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
/// [`cargo`]: index.html
fn cargo_crate_bin<S: AsRef<ffi::OsStr>>(package: S, name: S) -> Result<Self, CargoError>;

/// Create a [`Command`] to run a specific example of a specific crate from workspaces.
///
/// See the [`cargo` module documentation][`cargo`] for caveats and workarounds.
///
/// # Examples
///
/// ```rust
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// let mut cmd = Command::cargo_crate_example("assert_cmd", "example_fixture")
/// .unwrap();
/// let output = cmd.unwrap();
/// ```
///
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
/// [`cargo`]: index.html
fn cargo_crate_example<S: AsRef<ffi::OsStr>>(package: S, name: S) -> Result<Self, CargoError>;
}

impl CommandCargoExt for process::Command {
Expand Down Expand Up @@ -175,6 +215,28 @@ impl CommandCargoExt for process::Command {
.map_err(CargoError::with_cause)?;
Ok(runner.command())
}

fn cargo_crate_bin<S: AsRef<ffi::OsStr>>(package: S, name: S) -> Result<Self, CargoError> {
let runner = escargot::CargoBuild::new()
.package(package)
.bin(name)
.current_release()
.current_target()
.run()
.map_err(CargoError::with_cause)?;
Ok(runner.command())
}

fn cargo_crate_example<S: AsRef<ffi::OsStr>>(package: S, name: S) -> Result<Self, CargoError> {
let runner = escargot::CargoBuild::new()
.package(package)
.example(name)
.current_release()
.current_target()
.run()
.map_err(CargoError::with_cause)?;
Ok(runner.command())
}
}

/// Error when finding crate binary.
Expand Down