Skip to content

Commit 4e8fd3b

Browse files
authored
Merge pull request #46 from epage/cargo
fix(cargo): Point people to escargot
2 parents 30bb7d3 + 2e32822 commit 4e8fd3b

File tree

4 files changed

+61
-100
lines changed

4 files changed

+61
-100
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ name = "bin_fixture"
1818
predicates = "0.9.0"
1919
predicates-core = "0.9.0"
2020
predicates-tree = "0.9.0"
21-
escargot = "0.2"
22-
serde = { version = "1.0", features = ["derive"] }
21+
escargot = "0.3"
2322

2423
[dev-dependencies]
2524
docmatic = "0.1"

src/cargo.rs

+48-96
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@
2020
//! .unwrap();
2121
//! ```
2222
//!
23-
//! Caching the binary's location:
23+
//! For caching to minimize cargo overhead or customize the build process, see [`escargot`].
2424
//!
25-
//! ```rust
25+
//! ```rust,ignore
2626
//! use assert_cmd::prelude::*;
27+
//! use escargot;
2728
//!
2829
//! use std::process::Command;
2930
//!
30-
//! let bin_under_test = assert_cmd::cargo::main_binary_path().unwrap();
31-
//! Command::new(&bin_under_test)
31+
//! let bin_under_test = escargot::CargoBuild::new()
32+
//! .bin("bin_fixture")
33+
//! .current_release()
34+
//! .current_target()
35+
//! .run()
36+
//! .unwrap();
37+
//! bin_under_test.command()
3238
//! .unwrap();
3339
//! ```
3440
//!
@@ -37,6 +43,7 @@
3743
//! [`lazy_static`]: https://crates.io/crates/lazy_static
3844
//! [`CommandCargoExt`]: trait.CommandCargoExt.html
3945
//! [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
46+
//! [`escargot`]: https://docs.rs/escargot/
4047
4148
use std::error::Error;
4249
use std::ffi;
@@ -128,52 +135,29 @@ where
128135

129136
impl CommandCargoExt for process::Command {
130137
fn main_binary() -> Result<Self, CargoError> {
131-
let cmd = main_binary_path()?;
132-
Ok(process::Command::new(&cmd))
138+
let runner = escargot::CargoBuild::new()
139+
.current_release()
140+
.run()
141+
.map_err(CargoError::with_cause)?;
142+
Ok(runner.command())
133143
}
134144

135145
fn cargo_bin<S: AsRef<ffi::OsStr>>(name: S) -> Result<Self, CargoError> {
136-
let cmd = cargo_bin_path(name)?;
137-
Ok(process::Command::new(&cmd))
146+
let runner = escargot::CargoBuild::new()
147+
.bin(name)
148+
.current_release()
149+
.run()
150+
.map_err(CargoError::with_cause)?;
151+
Ok(runner.command())
138152
}
139153

140154
fn cargo_example<S: AsRef<ffi::OsStr>>(name: S) -> Result<Self, CargoError> {
141-
let cmd = cargo_example_path(name)?;
142-
Ok(process::Command::new(&cmd))
143-
}
144-
}
145-
146-
#[derive(Deserialize)]
147-
struct MessageTarget<'a> {
148-
#[serde(borrow)]
149-
crate_types: Vec<&'a str>,
150-
#[serde(borrow)]
151-
kind: Vec<&'a str>,
152-
}
153-
154-
#[derive(Deserialize)]
155-
struct MessageFilter<'a> {
156-
#[serde(borrow)]
157-
reason: &'a str,
158-
target: MessageTarget<'a>,
159-
filenames: Vec<path::PathBuf>,
160-
}
161-
162-
fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option<path::PathBuf> {
163-
let filter: MessageFilter = msg.convert().ok()?;
164-
if filter.reason != "compiler-artifact"
165-
|| filter.target.crate_types != ["bin"]
166-
|| filter.target.kind != [kind]
167-
{
168-
None
169-
} else {
170-
Some(
171-
filter
172-
.filenames
173-
.into_iter()
174-
.next()
175-
.expect("files must exist"),
176-
)
155+
let runner = escargot::CargoBuild::new()
156+
.example(name)
157+
.current_release()
158+
.run()
159+
.map_err(CargoError::with_cause)?;
160+
Ok(runner.command())
177161
}
178162
}
179163

@@ -194,22 +178,13 @@ fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option<path::PathBu
194178
/// Command::new(&bin_under_test)
195179
/// .unwrap();
196180
/// ```
181+
#[deprecated(since = "0.9.1", note = "For caching, using escargot directly.")]
197182
pub fn main_binary_path() -> Result<path::PathBuf, CargoError> {
198-
let cargo = escargot::Cargo::new().build().current_release();
199-
let bins: Vec<_> = cargo
200-
.exec()
201-
.map_err(CargoError::with_cause)?
202-
.filter_map(|m| extract_filenames(&m, "bin"))
203-
.collect();
204-
if bins.is_empty() {
205-
return Err(CargoError::with_context("No binaries in crate"));
206-
} else if bins.len() != 1 {
207-
return Err(CargoError::with_context(format!(
208-
"Ambiguous which binary is intended: {:?}",
209-
bins
210-
)));
211-
}
212-
Ok(bins.into_iter().next().expect("already validated"))
183+
let runner = escargot::CargoBuild::new()
184+
.current_release()
185+
.run()
186+
.map_err(CargoError::with_cause)?;
187+
Ok(runner.path().to_owned())
213188
}
214189

215190
/// Get the path to the specified binary of the current crate.
@@ -227,15 +202,14 @@ pub fn main_binary_path() -> Result<path::PathBuf, CargoError> {
227202
/// Command::new(&bin_under_test)
228203
/// .unwrap();
229204
/// ```
205+
#[deprecated(since = "0.9.1", note = "For caching, using escargot directly.")]
230206
pub fn cargo_bin_path<S: AsRef<ffi::OsStr>>(name: S) -> Result<path::PathBuf, CargoError> {
231-
let cargo = escargot::Cargo::new().build().bin(name).current_release();
232-
let bins: Vec<_> = cargo
233-
.exec()
234-
.map_err(CargoError::with_cause)?
235-
.filter_map(|m| extract_filenames(&m, "bin"))
236-
.collect();
237-
assert_eq!(bins.len(), 1);
238-
Ok(bins.into_iter().next().expect("already validated"))
207+
let runner = escargot::CargoBuild::new()
208+
.bin(name)
209+
.current_release()
210+
.run()
211+
.map_err(CargoError::with_cause)?;
212+
Ok(runner.path().to_owned())
239213
}
240214

241215
/// Get the path to the specified example of the current crate.
@@ -253,48 +227,29 @@ pub fn cargo_bin_path<S: AsRef<ffi::OsStr>>(name: S) -> Result<path::PathBuf, Ca
253227
/// Command::new(&bin_under_test)
254228
/// .unwrap();
255229
/// ```
230+
#[deprecated(since = "0.9.1", note = "For caching, using escargot directly.")]
256231
pub fn cargo_example_path<S: AsRef<ffi::OsStr>>(name: S) -> Result<path::PathBuf, CargoError> {
257-
let cargo = escargot::Cargo::new()
258-
.build()
232+
let runner = escargot::CargoBuild::new()
259233
.example(name)
260-
.current_release();
261-
let bins: Vec<_> = cargo
262-
.exec()
263-
.map_err(CargoError::with_cause)?
264-
.filter_map(|m| extract_filenames(&m, "example"))
265-
.collect();
266-
assert_eq!(bins.len(), 1);
267-
Ok(bins.into_iter().next().expect("already validated"))
234+
.current_release()
235+
.run()
236+
.map_err(CargoError::with_cause)?;
237+
Ok(runner.path().to_owned())
268238
}
269239

270240
/// Error when finding crate binary.
271241
#[derive(Debug)]
272242
pub struct CargoError {
273-
context: Option<String>,
274243
cause: Option<Box<Error + Send + Sync + 'static>>,
275244
}
276245

277246
impl CargoError {
278-
fn with_context<S>(context: S) -> Self
279-
where
280-
S: Into<String>,
281-
{
282-
let context = context.into();
283-
Self {
284-
context: Some(context),
285-
cause: None,
286-
}
287-
}
288-
289247
fn with_cause<E>(cause: E) -> Self
290248
where
291249
E: Error + Send + Sync + 'static,
292250
{
293251
let cause = Box::new(cause);
294-
Self {
295-
context: None,
296-
cause: Some(cause),
297-
}
252+
Self { cause: Some(cause) }
298253
}
299254
}
300255

@@ -313,9 +268,6 @@ impl Error for CargoError {
313268

314269
impl fmt::Display for CargoError {
315270
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
316-
if let Some(ref context) = self.context {
317-
writeln!(f, "{}", context)?;
318-
}
319271
if let Some(ref cause) = self.cause {
320272
writeln!(f, "Cause: {}", cause)?;
321273
}

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ extern crate escargot;
6464
extern crate predicates;
6565
extern crate predicates_core;
6666
extern crate predicates_tree;
67-
#[macro_use]
68-
extern crate serde;
6967

7068
pub mod assert;
7169
pub use assert::Assert;

tests/cargo.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate assert_cmd;
2+
extern crate escargot;
23
extern crate predicates;
34

45
use std::process;
@@ -46,3 +47,14 @@ fn cargo_example_with_empty_env() {
4647
cmd.env_clear().env("stdout", "42");
4748
cmd.assert().success().stdout("42\n");
4849
}
50+
51+
#[test]
52+
fn cargo_example_cache() {
53+
let bin_under_test = escargot::CargoBuild::new()
54+
.bin("bin_fixture")
55+
.current_release()
56+
.current_target()
57+
.run()
58+
.unwrap();
59+
bin_under_test.command().unwrap();
60+
}

0 commit comments

Comments
 (0)