From 59cdbad5ee042bc9235266078aee064c812c331e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 9 Aug 2018 14:04:15 -0500 Subject: [PATCH 1/3] chore: Upgrade escargot --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5af5441..cfd8d37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ name = "bin_fixture" predicates = "0.9.0" predicates-core = "0.9.0" predicates-tree = "0.9.0" -escargot = "0.2" +escargot = "0.3" serde = { version = "1.0", features = ["derive"] } [dev-dependencies] From fe0d12c476f031f5b37059003b2becd96bb8afb4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 9 Aug 2018 14:15:57 -0500 Subject: [PATCH 2/3] refactor: Use CargoRun --- Cargo.toml | 1 - src/cargo.rs | 104 ++++++++++----------------------------------------- src/lib.rs | 2 - 3 files changed, 19 insertions(+), 88 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cfd8d37..b159ee6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,6 @@ predicates = "0.9.0" predicates-core = "0.9.0" predicates-tree = "0.9.0" escargot = "0.3" -serde = { version = "1.0", features = ["derive"] } [dev-dependencies] docmatic = "0.1" diff --git a/src/cargo.rs b/src/cargo.rs index 062e65c..feb4095 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -143,40 +143,6 @@ impl CommandCargoExt for process::Command { } } -#[derive(Deserialize)] -struct MessageTarget<'a> { - #[serde(borrow)] - crate_types: Vec<&'a str>, - #[serde(borrow)] - kind: Vec<&'a str>, -} - -#[derive(Deserialize)] -struct MessageFilter<'a> { - #[serde(borrow)] - reason: &'a str, - target: MessageTarget<'a>, - filenames: Vec, -} - -fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option { - let filter: MessageFilter = msg.convert().ok()?; - if filter.reason != "compiler-artifact" - || filter.target.crate_types != ["bin"] - || filter.target.kind != [kind] - { - None - } else { - Some( - filter - .filenames - .into_iter() - .next() - .expect("files must exist"), - ) - } -} - /// Get the path to the crate's main binary. /// /// Intended for caching the location, reducing the cargo overhead. @@ -195,21 +161,12 @@ fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option Result { - let cargo = escargot::Cargo::new().build().current_release(); - let bins: Vec<_> = cargo - .exec() - .map_err(CargoError::with_cause)? - .filter_map(|m| extract_filenames(&m, "bin")) - .collect(); - if bins.is_empty() { - return Err(CargoError::with_context("No binaries in crate")); - } else if bins.len() != 1 { - return Err(CargoError::with_context(format!( - "Ambiguous which binary is intended: {:?}", - bins - ))); - } - Ok(bins.into_iter().next().expect("already validated")) + let runner = escargot::Cargo::new() + .build() + .current_release() + .run() + .map_err(CargoError::with_cause)?; + Ok(runner.path().to_owned()) } /// Get the path to the specified binary of the current crate. @@ -228,14 +185,13 @@ pub fn main_binary_path() -> Result { /// .unwrap(); /// ``` pub fn cargo_bin_path>(name: S) -> Result { - let cargo = escargot::Cargo::new().build().bin(name).current_release(); - let bins: Vec<_> = cargo - .exec() - .map_err(CargoError::with_cause)? - .filter_map(|m| extract_filenames(&m, "bin")) - .collect(); - assert_eq!(bins.len(), 1); - Ok(bins.into_iter().next().expect("already validated")) + let runner = escargot::Cargo::new() + .build() + .bin(name) + .current_release() + .run() + .map_err(CargoError::with_cause)?; + Ok(runner.path().to_owned()) } /// Get the path to the specified example of the current crate. @@ -254,47 +210,28 @@ pub fn cargo_bin_path>(name: S) -> Result>(name: S) -> Result { - let cargo = escargot::Cargo::new() + let runner = escargot::Cargo::new() .build() .example(name) - .current_release(); - let bins: Vec<_> = cargo - .exec() - .map_err(CargoError::with_cause)? - .filter_map(|m| extract_filenames(&m, "example")) - .collect(); - assert_eq!(bins.len(), 1); - Ok(bins.into_iter().next().expect("already validated")) + .current_release() + .run() + .map_err(CargoError::with_cause)?; + Ok(runner.path().to_owned()) } /// Error when finding crate binary. #[derive(Debug)] pub struct CargoError { - context: Option, cause: Option>, } impl CargoError { - fn with_context(context: S) -> Self - where - S: Into, - { - let context = context.into(); - Self { - context: Some(context), - cause: None, - } - } - fn with_cause(cause: E) -> Self where E: Error + Send + Sync + 'static, { let cause = Box::new(cause); - Self { - context: None, - cause: Some(cause), - } + Self { cause: Some(cause) } } } @@ -313,9 +250,6 @@ impl Error for CargoError { impl fmt::Display for CargoError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if let Some(ref context) = self.context { - writeln!(f, "{}", context)?; - } if let Some(ref cause) = self.cause { writeln!(f, "Cause: {}", cause)?; } diff --git a/src/lib.rs b/src/lib.rs index 8b30c40..440dd49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,8 +64,6 @@ extern crate escargot; extern crate predicates; extern crate predicates_core; extern crate predicates_tree; -#[macro_use] -extern crate serde; pub mod assert; pub use assert::Assert; From 2e32822ca22b4299a2a07a46ea431835c8f50401 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 9 Aug 2018 14:38:21 -0500 Subject: [PATCH 3/3] fix(cargo): Point people to escargot Fixes #44 --- src/cargo.rs | 50 ++++++++++++++++++++++++++++++++++---------------- tests/cargo.rs | 12 ++++++++++++ 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/cargo.rs b/src/cargo.rs index feb4095..a00a23a 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -20,15 +20,21 @@ //! .unwrap(); //! ``` //! -//! Caching the binary's location: +//! For caching to minimize cargo overhead or customize the build process, see [`escargot`]. //! -//! ```rust +//! ```rust,ignore //! use assert_cmd::prelude::*; +//! use escargot; //! //! use std::process::Command; //! -//! let bin_under_test = assert_cmd::cargo::main_binary_path().unwrap(); -//! Command::new(&bin_under_test) +//! let bin_under_test = escargot::CargoBuild::new() +//! .bin("bin_fixture") +//! .current_release() +//! .current_target() +//! .run() +//! .unwrap(); +//! bin_under_test.command() //! .unwrap(); //! ``` //! @@ -37,6 +43,7 @@ //! [`lazy_static`]: https://crates.io/crates/lazy_static //! [`CommandCargoExt`]: trait.CommandCargoExt.html //! [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html +//! [`escargot`]: https://docs.rs/escargot/ use std::error::Error; use std::ffi; @@ -128,18 +135,29 @@ where impl CommandCargoExt for process::Command { fn main_binary() -> Result { - let cmd = main_binary_path()?; - Ok(process::Command::new(&cmd)) + let runner = escargot::CargoBuild::new() + .current_release() + .run() + .map_err(CargoError::with_cause)?; + Ok(runner.command()) } fn cargo_bin>(name: S) -> Result { - let cmd = cargo_bin_path(name)?; - Ok(process::Command::new(&cmd)) + let runner = escargot::CargoBuild::new() + .bin(name) + .current_release() + .run() + .map_err(CargoError::with_cause)?; + Ok(runner.command()) } fn cargo_example>(name: S) -> Result { - let cmd = cargo_example_path(name)?; - Ok(process::Command::new(&cmd)) + let runner = escargot::CargoBuild::new() + .example(name) + .current_release() + .run() + .map_err(CargoError::with_cause)?; + Ok(runner.command()) } } @@ -160,9 +178,9 @@ impl CommandCargoExt for process::Command { /// Command::new(&bin_under_test) /// .unwrap(); /// ``` +#[deprecated(since = "0.9.1", note = "For caching, using escargot directly.")] pub fn main_binary_path() -> Result { - let runner = escargot::Cargo::new() - .build() + let runner = escargot::CargoBuild::new() .current_release() .run() .map_err(CargoError::with_cause)?; @@ -184,9 +202,9 @@ pub fn main_binary_path() -> Result { /// Command::new(&bin_under_test) /// .unwrap(); /// ``` +#[deprecated(since = "0.9.1", note = "For caching, using escargot directly.")] pub fn cargo_bin_path>(name: S) -> Result { - let runner = escargot::Cargo::new() - .build() + let runner = escargot::CargoBuild::new() .bin(name) .current_release() .run() @@ -209,9 +227,9 @@ pub fn cargo_bin_path>(name: S) -> Result>(name: S) -> Result { - let runner = escargot::Cargo::new() - .build() + let runner = escargot::CargoBuild::new() .example(name) .current_release() .run() diff --git a/tests/cargo.rs b/tests/cargo.rs index 79a6725..61a435f 100644 --- a/tests/cargo.rs +++ b/tests/cargo.rs @@ -1,4 +1,5 @@ extern crate assert_cmd; +extern crate escargot; extern crate predicates; use std::process; @@ -46,3 +47,14 @@ fn cargo_example_with_empty_env() { cmd.env_clear().env("stdout", "42"); cmd.assert().success().stdout("42\n"); } + +#[test] +fn cargo_example_cache() { + let bin_under_test = escargot::CargoBuild::new() + .bin("bin_fixture") + .current_release() + .current_target() + .run() + .unwrap(); + bin_under_test.command().unwrap(); +}