@@ -8,6 +8,20 @@ use std::path::PathBuf;
8
8
use std:: process:: { Command , Stdio } ;
9
9
use std:: vec:: Vec ;
10
10
11
+ fn find_cargo ( ) -> String {
12
+ let which_cargo = Command :: new ( "which" ) . arg ( "cargo" )
13
+ . output ( ) . expect ( "Cannot exectute `which` to find `cargo`." ) ;
14
+
15
+ if !which_cargo. status . success ( ) {
16
+ panic ! ( "Could not find `cargo` command" ) ;
17
+ }
18
+
19
+ String :: from_utf8 ( which_cargo. stdout )
20
+ . expect ( "Path to `cargo` is not UTF-8. This is currently unsupported by assert_cli." )
21
+ . trim ( )
22
+ . to_string ( )
23
+ }
24
+
11
25
/// Assertions for a specific command.
12
26
#[ derive( Debug ) ]
13
27
pub struct Assert {
@@ -25,11 +39,13 @@ impl default::Default for Assert {
25
39
///
26
40
/// Defaults to asserting _successful_ execution.
27
41
fn default ( ) -> Self {
42
+ let cargo_path = find_cargo ( ) ;
43
+ let args = vec ! [ "run" , "--quiet" , "--" ]
44
+ . into_iter ( )
45
+ . map ( String :: from) ;
46
+
28
47
Assert {
29
- cmd : vec ! [ "cargo" , "run" , "--quiet" , "--" ]
30
- . into_iter ( )
31
- . map ( String :: from)
32
- . collect ( ) ,
48
+ cmd : vec ! [ cargo_path] . into_iter ( ) . chain ( args) . collect ( ) ,
33
49
env : Environment :: inherit ( ) ,
34
50
current_dir : None ,
35
51
expect_success : Some ( true ) ,
@@ -52,11 +68,13 @@ impl Assert {
52
68
///
53
69
/// Defaults to asserting _successful_ execution.
54
70
pub fn cargo_binary ( name : & str ) -> Self {
71
+ let cargo_path = find_cargo ( ) ;
72
+ let args = vec ! [ "run" , "--quiet" , "--bin" , name, "--" ]
73
+ . into_iter ( )
74
+ . map ( String :: from) ;
75
+
55
76
Assert {
56
- cmd : vec ! [ "cargo" , "run" , "--quiet" , "--bin" , name, "--" ]
57
- . into_iter ( )
58
- . map ( String :: from)
59
- . collect ( ) ,
77
+ cmd : vec ! [ cargo_path] . into_iter ( ) . chain ( args) . collect ( ) ,
60
78
..Self :: default ( )
61
79
}
62
80
}
0 commit comments