1+ use crate :: { assert_not_contains, handle_failed_output} ;
12use std:: ffi:: OsStr ;
23use std:: io:: Write ;
34use std:: ops:: { Deref , DerefMut } ;
@@ -11,16 +12,39 @@ pub struct Command {
1112
1213impl Command {
1314 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 }
1816 }
1917
2018 pub fn set_stdin ( & mut self , stdin : Box < [ u8 ] > ) {
2119 self . stdin = Some ( stdin) ;
2220 }
2321
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+
2448 #[ track_caller]
2549 pub ( crate ) fn command_output ( & mut self ) -> CompletedProcess {
2650 // let's make sure we piped all the input and outputs
@@ -76,16 +100,47 @@ impl CompletedProcess {
76100 self . output . status
77101 }
78102
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+
79110 #[ 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 {
81137 assert ! ( self . output. status. code( ) == Some ( code) ) ;
138+ self
82139 }
83140}
84141
85142impl From < Output > for CompletedProcess {
86143 fn from ( output : Output ) -> Self {
87- Self {
88- output
89- }
144+ Self { output }
90145 }
91146}
0 commit comments