1
- use std:: default;
2
1
use std:: ffi:: { OsStr , OsString } ;
3
2
use std:: io:: Write ;
4
3
use std:: path:: PathBuf ;
@@ -11,6 +10,35 @@ use failure::Fail;
11
10
12
11
use errors:: * ;
13
12
use output:: { Content , Output , OutputKind , OutputPredicate } ;
13
+ use cargo;
14
+
15
+ #[ derive( Deserialize ) ]
16
+ struct MessageTarget < ' a > {
17
+ #[ serde( borrow) ]
18
+ crate_types : Vec < & ' a str > ,
19
+ #[ serde( borrow) ]
20
+ kind : Vec < & ' a str > ,
21
+ }
22
+
23
+ #[ derive( Deserialize ) ]
24
+ struct MessageFilter < ' a > {
25
+ #[ serde( borrow) ]
26
+ reason : & ' a str ,
27
+ target : MessageTarget < ' a > ,
28
+ #[ serde( borrow) ]
29
+ filenames : Vec < & ' a str > ,
30
+ }
31
+
32
+ fn filenames ( msg : cargo:: Message , kind : & str ) -> Option < String > {
33
+ let filter: MessageFilter = msg. convert ( ) . ok ( ) ?;
34
+ if filter. reason != "compiler-artifact" || filter. target . crate_types != [ "bin" ]
35
+ || filter. target . kind != [ kind]
36
+ {
37
+ None
38
+ } else {
39
+ Some ( filter. filenames [ 0 ] . to_owned ( ) )
40
+ }
41
+ }
14
42
15
43
/// Assertions for a specific command.
16
44
#[ derive( Debug ) ]
@@ -25,80 +53,72 @@ pub struct Assert {
25
53
stdin_contents : Option < Vec < u8 > > ,
26
54
}
27
55
28
- impl default :: Default for Assert {
29
- /// Construct an assert using `cargo run --` as command .
56
+ impl Assert {
57
+ /// Run the crate's main binary .
30
58
///
31
59
/// Defaults to asserting _successful_ execution.
32
- fn default ( ) -> Self {
33
- Assert {
34
- cmd : vec ! [
35
- "cargo" ,
36
- "run" ,
37
- #[ cfg( not( debug_assertions) ) ]
38
- "--release" ,
39
- "--quiet" ,
40
- "--" ,
41
- ] . into_iter ( )
42
- . map ( OsString :: from)
43
- . collect ( ) ,
60
+ pub fn main_binary ( ) -> Result < Self , failure:: Error > {
61
+ let cargo = cargo:: Cargo :: new ( ) . build ( ) . current_release ( ) ;
62
+ let bins: Vec < _ > = cargo. exec ( ) ?. filter_map ( |m| filenames ( m, "bin" ) ) . collect ( ) ;
63
+ if bins. is_empty ( ) {
64
+ bail ! ( "No binaries in crate" ) ;
65
+ } else if bins. len ( ) != 1 {
66
+ bail ! ( "Ambiguous which binary is intended: {:?}" , bins) ;
67
+ }
68
+ let bin = bins[ 0 ] . as_str ( ) ;
69
+ let cmd = Self {
70
+ cmd : vec ! [ bin] . into_iter ( ) . map ( OsString :: from) . collect ( ) ,
44
71
env : Environment :: inherit ( ) ,
45
72
current_dir : None ,
46
73
expect_success : Some ( true ) ,
47
74
expect_exit_code : None ,
48
75
expect_output : vec ! [ ] ,
49
76
stdin_contents : None ,
50
- }
51
- }
52
- }
53
-
54
- impl Assert {
55
- /// Run the crate's main binary.
56
- ///
57
- /// Defaults to asserting _successful_ execution.
58
- pub fn main_binary ( ) -> Self {
59
- Assert :: default ( )
77
+ } ;
78
+ Ok ( cmd)
60
79
}
61
80
62
81
/// Run a specific binary of the current crate.
63
82
///
64
83
/// Defaults to asserting _successful_ execution.
65
- pub fn cargo_binary < S : AsRef < OsStr > > ( name : S ) -> Self {
66
- Assert {
67
- cmd : vec ! [
68
- OsStr :: new( "cargo" ) ,
69
- OsStr :: new( "run" ) ,
70
- #[ cfg( not( debug_assertions) ) ]
71
- OsStr :: new( "--release" ) ,
72
- OsStr :: new( "--quiet" ) ,
73
- OsStr :: new( "--bin" ) ,
74
- name. as_ref( ) ,
75
- OsStr :: new( "--" ) ,
76
- ] . into_iter ( )
77
- . map ( OsString :: from)
78
- . collect ( ) ,
79
- ..Self :: default ( )
80
- }
84
+ pub fn cargo_binary < S : AsRef < OsStr > > ( name : S ) -> Result < Self , failure:: Error > {
85
+ let cargo = cargo:: Cargo :: new ( ) . build ( ) . bin ( name) . current_release ( ) ;
86
+ let bins: Vec < _ > = cargo. exec ( ) ?. filter_map ( |m| filenames ( m, "bin" ) ) . collect ( ) ;
87
+ assert_eq ! ( bins. len( ) , 1 ) ;
88
+ let bin = bins[ 0 ] . as_str ( ) ;
89
+ let cmd = Self {
90
+ cmd : vec ! [ bin] . into_iter ( ) . map ( OsString :: from) . collect ( ) ,
91
+ env : Environment :: inherit ( ) ,
92
+ current_dir : None ,
93
+ expect_success : Some ( true ) ,
94
+ expect_exit_code : None ,
95
+ expect_output : vec ! [ ] ,
96
+ stdin_contents : None ,
97
+ } ;
98
+ Ok ( cmd)
81
99
}
82
100
83
101
/// Run a specific example of the current crate.
84
102
///
85
103
/// Defaults to asserting _successful_ execution.
86
- pub fn example < S : AsRef < OsStr > > ( name : S ) -> Self {
87
- Assert {
88
- cmd : vec ! [
89
- OsStr :: new( "cargo" ) ,
90
- OsStr :: new( "run" ) ,
91
- #[ cfg( not( debug_assertions) ) ]
92
- OsStr :: new( "--release" ) ,
93
- OsStr :: new( "--quiet" ) ,
94
- OsStr :: new( "--example" ) ,
95
- name. as_ref( ) ,
96
- OsStr :: new( "--" ) ,
97
- ] . into_iter ( )
98
- . map ( OsString :: from)
99
- . collect ( ) ,
100
- ..Self :: default ( )
101
- }
104
+ pub fn example < S : AsRef < OsStr > > ( name : S ) -> Result < Self , failure:: Error > {
105
+ let cargo = cargo:: Cargo :: new ( ) . build ( ) . example ( name) . current_release ( ) ;
106
+ let bins: Vec < _ > = cargo
107
+ . exec ( ) ?
108
+ . filter_map ( |m| filenames ( m, "example" ) )
109
+ . collect ( ) ;
110
+ assert_eq ! ( bins. len( ) , 1 ) ;
111
+ let bin = bins[ 0 ] . as_str ( ) ;
112
+ let cmd = Self {
113
+ cmd : vec ! [ bin] . into_iter ( ) . map ( OsString :: from) . collect ( ) ,
114
+ env : Environment :: inherit ( ) ,
115
+ current_dir : None ,
116
+ expect_success : Some ( true ) ,
117
+ expect_exit_code : None ,
118
+ expect_output : vec ! [ ] ,
119
+ stdin_contents : None ,
120
+ } ;
121
+ Ok ( cmd)
102
122
}
103
123
104
124
/// Run a custom command.
@@ -116,7 +136,12 @@ impl Assert {
116
136
pub fn command < S : AsRef < OsStr > > ( cmd : & [ S ] ) -> Self {
117
137
Assert {
118
138
cmd : cmd. into_iter ( ) . map ( OsString :: from) . collect ( ) ,
119
- ..Self :: default ( )
139
+ env : Environment :: inherit ( ) ,
140
+ current_dir : None ,
141
+ expect_success : Some ( true ) ,
142
+ expect_exit_code : None ,
143
+ expect_output : vec ! [ ] ,
144
+ stdin_contents : None ,
120
145
}
121
146
}
122
147
@@ -559,52 +584,6 @@ mod test {
559
584
Assert :: command ( & [ "printenv" ] )
560
585
}
561
586
562
- #[ test]
563
- fn main_binary_default_uses_active_profile ( ) {
564
- let assert = Assert :: main_binary ( ) ;
565
-
566
- let expected = if cfg ! ( debug_assertions) {
567
- OsString :: from ( "cargo run --quiet -- " )
568
- } else {
569
- OsString :: from ( "cargo run --release --quiet -- " )
570
- } ;
571
-
572
- assert_eq ! (
573
- expected,
574
- assert
575
- . cmd
576
- . into_iter( )
577
- . fold( OsString :: from( "" ) , |mut cmd, token| {
578
- cmd. push( token) ;
579
- cmd. push( " " ) ;
580
- cmd
581
- } )
582
- ) ;
583
- }
584
-
585
- #[ test]
586
- fn cargo_binary_default_uses_active_profile ( ) {
587
- let assert = Assert :: cargo_binary ( "hello" ) ;
588
-
589
- let expected = if cfg ! ( debug_assertions) {
590
- OsString :: from ( "cargo run --quiet --bin hello -- " )
591
- } else {
592
- OsString :: from ( "cargo run --release --quiet --bin hello -- " )
593
- } ;
594
-
595
- assert_eq ! (
596
- expected,
597
- assert
598
- . cmd
599
- . into_iter( )
600
- . fold( OsString :: from( "" ) , |mut cmd, token| {
601
- cmd. push( token) ;
602
- cmd. push( " " ) ;
603
- cmd
604
- } )
605
- ) ;
606
- }
607
-
608
587
#[ test]
609
588
fn take_ownership ( ) {
610
589
let x = Environment :: inherit ( ) ;
0 commit comments