1
+ use crate :: { assert_not_contains, handle_failed_output} ;
1
2
use std:: ffi:: OsStr ;
2
3
use std:: io:: Write ;
3
4
use std:: ops:: { Deref , DerefMut } ;
@@ -11,16 +12,39 @@ pub struct Command {
11
12
12
13
impl Command {
13
14
pub fn new < S : AsRef < OsStr > > ( program : S ) -> Self {
14
- Self {
15
- cmd : StdCommand :: new ( program) ,
16
- stdin : None ,
17
- }
15
+ Self { cmd : StdCommand :: new ( program) , stdin : None }
18
16
}
19
17
20
18
pub fn set_stdin ( & mut self , stdin : Box < [ u8 ] > ) {
21
19
self . stdin = Some ( stdin) ;
22
20
}
23
21
22
+ /// Run the constructed command and assert that it is successfully run.
23
+ #[ track_caller]
24
+ pub fn run ( & mut self ) -> CompletedProcess {
25
+ let caller_location = :: std:: panic:: Location :: caller ( ) ;
26
+ let caller_line_number = caller_location. line ( ) ;
27
+
28
+ let output = self . command_output ( ) ;
29
+ if !output. status ( ) . success ( ) {
30
+ handle_failed_output ( & self , output, caller_line_number) ;
31
+ }
32
+ output
33
+ }
34
+
35
+ /// Run the constructed command and assert that it does not successfully run.
36
+ #[ track_caller]
37
+ pub fn run_fail ( & mut self ) -> CompletedProcess {
38
+ let caller_location = :: std:: panic:: Location :: caller ( ) ;
39
+ let caller_line_number = caller_location. line ( ) ;
40
+
41
+ let output = self . command_output ( ) ;
42
+ if output. status ( ) . success ( ) {
43
+ handle_failed_output ( & self , output, caller_line_number) ;
44
+ }
45
+ output
46
+ }
47
+
24
48
#[ track_caller]
25
49
pub ( crate ) fn command_output ( & mut self ) -> CompletedProcess {
26
50
// let's make sure we piped all the input and outputs
@@ -76,16 +100,47 @@ impl CompletedProcess {
76
100
self . output . status
77
101
}
78
102
103
+ /// Checks that trimmed `stdout` matches trimmed `content`.
104
+ #[ track_caller]
105
+ pub fn assert_stdout_equals ( self , content : & str ) -> Self {
106
+ assert_eq ! ( self . stdout_utf8( ) . trim( ) , content. trim( ) ) ;
107
+ self
108
+ }
109
+
79
110
#[ track_caller]
80
- pub fn assert_exit_code ( & self , code : i32 ) {
111
+ pub fn assert_stdout_not_contains ( self , needle : & str ) -> Self {
112
+ assert_not_contains ( & self . stdout_utf8 ( ) , needle) ;
113
+ self
114
+ }
115
+
116
+ /// Checks that trimmed `stderr` matches trimmed `content`.
117
+ #[ track_caller]
118
+ pub fn assert_stderr_equals ( self , content : & str ) -> Self {
119
+ assert_eq ! ( self . stderr_utf8( ) . trim( ) , content. trim( ) ) ;
120
+ self
121
+ }
122
+
123
+ #[ track_caller]
124
+ pub fn assert_stderr_contains ( self , needle : & str ) -> Self {
125
+ assert ! ( self . stderr_utf8( ) . contains( needle) ) ;
126
+ self
127
+ }
128
+
129
+ #[ track_caller]
130
+ pub fn assert_stderr_not_contains ( self , needle : & str ) -> Self {
131
+ assert_not_contains ( & self . stdout_utf8 ( ) , needle) ;
132
+ self
133
+ }
134
+
135
+ #[ track_caller]
136
+ pub fn assert_exit_code ( self , code : i32 ) -> Self {
81
137
assert ! ( self . output. status. code( ) == Some ( code) ) ;
138
+ self
82
139
}
83
140
}
84
141
85
142
impl From < Output > for CompletedProcess {
86
143
fn from ( output : Output ) -> Self {
87
- Self {
88
- output
89
- }
144
+ Self { output }
90
145
}
91
146
}
0 commit comments