From eab1076791974f8659ee9968bc40bb554d9ccec6 Mon Sep 17 00:00:00 2001 From: Andrii Radyk Date: Sat, 5 Jan 2019 14:13:13 +0100 Subject: [PATCH] injecting builder implementation from CargoBuild --- Cargo.toml | 2 +- src/cargo.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/lib.rs | 2 + 3 files changed, 100 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2178d1b..5dffdfe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/cargo.rs b/src/cargo.rs index b7c5942..919db86 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -24,28 +24,29 @@ //! - Workaround the [per-call cargo overhead][cargo-overhead] by caching the binary location with [`lazy_static`]. //! - [If not using `--target `, 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 @@ -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; +} + +impl CommandCargoBuildExt for escargot::CargoBuild { + fn build_command(self) -> Result { + 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 @@ -144,6 +207,34 @@ where /// [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html /// [`cargo`]: index.html fn cargo_example>(name: S) -> Result; + + /// 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 { diff --git a/src/lib.rs b/src/lib.rs index 96071d9..6a0b9f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -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;