Skip to content

WIP: injecting builder implementation from CargoBuild #68

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

Closed
wants to merge 1 commit into from
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been working towards assert_cmd being 1.0. The current quiet period is me awaiting feedback. Escargot is further from 1.0. It is a lot closer now after my recent PRs but I still want to get some more practical usage out of it first (like with stager / cargo-tarball).

Even once escargot goes 1.0, I expected to not have much resistance to creating a 2.0, etc because I was not expecting it to be in APIs of other crates. Putting it in another crate's API makes me want to make sure the API is even more solid and could delay 1.0.


[dev-dependencies]
docmatic = "0.1"
103 changes: 97 additions & 6 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,29 @@
//! - Workaround the [per-call cargo overhead][cargo-overhead] by caching the binary location with [`lazy_static`].
//! - [If not using `--target <TRIPLET>`, bypass the first call overhead][first-call] by not
//! passing `current_target()` to [`escargot`].
//! - Using bin or example from another crate from workspaces
//!
//! This can be done by using [`CommandCargoBuildExt`] trait.
//!
//! ```rust
//! extern crate assert_cmd;
//! extern crate escargot;
//!
//! use assert_cmd::prelude::*;
//! use escargot;
//!
//! use std::process::Command;
//!
//! let bin_under_test = escargot::CargoBuild::new()
//! let mut bin_under_test = Command::cargo_builder()
//! .bin("bin_fixture")
//! .current_release()
//! .current_target()
//! .run()
//! .build_command()
//! .unwrap();
//! let mut cmd = bin_under_test.command();
//! let output = cmd.unwrap();
//! let output = bin_under_test.unwrap();
//! ```
//!
//! [`lazy_static`]: https://crates.io/crates/lazy_static
//! [`CommandCargoExt`]: trait.CommandCargoExt.html
//! [`CommandCargoBuildExt`]: trait.CommandCargoBuildExt.html
//! [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
//! [`escargot`]: https://docs.rs/escargot/
//! [cargo-overhead]: https://github.com/assert-rs/assert_cmd/issues/6
Expand All @@ -58,6 +59,68 @@ use std::process;

use escargot;

/// `CommandCargoBuildExt` is an extension trait for [`CargoBuild`][CargoBuild] to run
/// command using CargoBuild builder
///
/// 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_builder()
/// .bin("bin_fixture")
/// .package("assert_cmd")
/// .current_release()
/// .current_target()
/// .build_command()
/// .unwrap();
/// let output = cmd.unwrap();
/// ```
///
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
/// [`cargo`]: index.html
pub trait CommandCargoBuildExt
where
Self: Sized,
{
/// Create a [`Command`] using [`CargoBuild`] builder.
///
/// 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_builder()
/// .example("example_fixture")
/// .package("assert_cmd")
/// .current_release()
/// .current_target()
/// .build_command()
/// .unwrap();
/// let output = cmd.unwrap();
/// ```
///
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
/// [`CargoBuild`]: https://docs.rs/escargot/0.4.0/escargot/struct.CargoBuild.html
/// [`cargo`]: index.html
fn build_command(self) -> Result<process::Command, CargoError>;
}

impl CommandCargoBuildExt for escargot::CargoBuild {
fn build_command(self) -> Result<process::Command, CargoError> {
let runner = self.run().map_err(CargoError::with_cause)?;
Ok(runner.command())
}
}

/// Create a [`Command`] for a `bin` in the Cargo project.
///
/// `CommandCargoExt` is an extension trait for [`Command`][Command] to easily launch a crate's
Expand Down Expand Up @@ -144,6 +207,34 @@ 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 [`CargoBuild`] builder to construct any cargo run command
///
/// 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_builder()
/// .bin("bin_fixture")
/// .package("assert_cmd")
/// .current_release()
/// .current_target()
/// .build_command()
/// .unwrap();
/// let output = cmd.unwrap();
/// ```
///
/// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
/// [`CargoBuild`]: https://docs.rs/escargot/0.4.0/escargot/struct.CargoBuild.html
/// [`cargo`]: index.html
fn cargo_builder() -> escargot::CargoBuild {
escargot::CargoBuild::new()
}
}

impl CommandCargoExt for process::Command {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
//! [`Assert`]: assert/struct.Assert.html
//! [`success()`]: assert/struct.Assert.html#method.success
//! [`CommandCargoExt`]: cargo/trait.CommandCargoExt.html
//! [`CommandCargoBuildExt`]: cargo/trait.CommandCargoBuildExt.html
//! [`CommandStdInExt`]: stdin/trait.CommandStdInExt.html
//! [`OutputOkExt`]: cmd/trait.OutputOkExt.html
//! [`OutputAssertExt`]: assert/trait.OutputAssertExt.html
Expand All @@ -122,6 +123,7 @@ pub mod stdin;
/// Extension traits that are useful to have available.
pub mod prelude {
pub use assert::OutputAssertExt;
pub use cargo::CommandCargoBuildExt;
pub use cargo::CommandCargoExt;
pub use cmd::OutputOkExt;
pub use stdin::CommandStdInExt;
Expand Down