20
20
//! .unwrap();
21
21
//! ```
22
22
//!
23
- //! Caching the binary's location:
23
+ //! For caching to minimize cargo overhead or customize the build process, see [`escargot`].
24
24
//!
25
- //! ```rust
25
+ //! ```rust,ignore
26
26
//! use assert_cmd::prelude::*;
27
+ //! use escargot;
27
28
//!
28
29
//! use std::process::Command;
29
30
//!
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()
32
38
//! .unwrap();
33
39
//! ```
34
40
//!
37
43
//! [`lazy_static`]: https://crates.io/crates/lazy_static
38
44
//! [`CommandCargoExt`]: trait.CommandCargoExt.html
39
45
//! [`Command`]: https://doc.rust-lang.org/std/process/struct.Command.html
46
+ //! [`escargot`]: https://docs.rs/escargot/
40
47
41
48
use std:: error:: Error ;
42
49
use std:: ffi;
@@ -128,52 +135,29 @@ where
128
135
129
136
impl CommandCargoExt for process:: Command {
130
137
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 ( ) )
133
143
}
134
144
135
145
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 ( ) )
138
152
}
139
153
140
154
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 ( ) )
177
161
}
178
162
}
179
163
@@ -194,22 +178,13 @@ fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option<path::PathBu
194
178
/// Command::new(&bin_under_test)
195
179
/// .unwrap();
196
180
/// ```
181
+ #[ deprecated( since = "0.9.1" , note = "For caching, using escargot directly." ) ]
197
182
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 ( ) )
213
188
}
214
189
215
190
/// Get the path to the specified binary of the current crate.
@@ -227,15 +202,14 @@ pub fn main_binary_path() -> Result<path::PathBuf, CargoError> {
227
202
/// Command::new(&bin_under_test)
228
203
/// .unwrap();
229
204
/// ```
205
+ #[ deprecated( since = "0.9.1" , note = "For caching, using escargot directly." ) ]
230
206
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 ( ) )
239
213
}
240
214
241
215
/// 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
253
227
/// Command::new(&bin_under_test)
254
228
/// .unwrap();
255
229
/// ```
230
+ #[ deprecated( since = "0.9.1" , note = "For caching, using escargot directly." ) ]
256
231
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 ( )
259
233
. 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 ( ) )
268
238
}
269
239
270
240
/// Error when finding crate binary.
271
241
#[ derive( Debug ) ]
272
242
pub struct CargoError {
273
- context : Option < String > ,
274
243
cause : Option < Box < Error + Send + Sync + ' static > > ,
275
244
}
276
245
277
246
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
-
289
247
fn with_cause < E > ( cause : E ) -> Self
290
248
where
291
249
E : Error + Send + Sync + ' static ,
292
250
{
293
251
let cause = Box :: new ( cause) ;
294
- Self {
295
- context : None ,
296
- cause : Some ( cause) ,
297
- }
252
+ Self { cause : Some ( cause) }
298
253
}
299
254
}
300
255
@@ -313,9 +268,6 @@ impl Error for CargoError {
313
268
314
269
impl fmt:: Display for CargoError {
315
270
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
316
- if let Some ( ref context) = self . context {
317
- writeln ! ( f, "{}" , context) ?;
318
- }
319
271
if let Some ( ref cause) = self . cause {
320
272
writeln ! ( f, "Cause: {}" , cause) ?;
321
273
}
0 commit comments