1- use std:: borrow:: BorrowMut ;
21use std:: fs:: File ;
32use std:: io:: prelude:: * ;
43use std:: path:: Path ;
5- use std:: process:: Command ;
64use std:: time;
75
86use anyhow:: { bail, Context , Result } ;
97use rand:: Rng ;
8+ use xshell:: Cmd ;
109
1110// HTTP Server deps
1211use futures_util:: future;
@@ -16,30 +15,34 @@ use hyper_staticfile::Static;
1615use tokio:: runtime:: Runtime ;
1716
1817/// Run command and assert that its stderr contains pat
19- pub ( crate ) fn cmd_fails_with < C : BorrowMut < Command > > ( mut c : C , pat : & str ) -> Result < ( ) > {
20- let c = c. borrow_mut ( ) ;
21- let o = c. output ( ) ?;
22- if o. status . success ( ) {
23- bail ! ( "Command {:?} unexpectedly succeeded" , c) ;
18+ pub fn cmd_fails_with ( cmd : Cmd , pat : & str ) -> Result < ( ) > {
19+ let cmd_str = cmd. to_string ( ) ;
20+ let output = cmd. ignore_status ( ) . output ( ) ?;
21+
22+ if output. status . success ( ) {
23+ bail ! ( "Command {:?} unexpectedly succeeded" , cmd_str) ;
2424 }
25- if twoway:: find_bytes ( & o. stderr , pat. as_bytes ( ) ) . is_none ( ) {
26- dbg ! ( String :: from_utf8_lossy( & o. stdout) ) ;
27- dbg ! ( String :: from_utf8_lossy( & o. stderr) ) ;
28- bail ! ( "Command {:?} stderr did not match: {}" , c, pat) ;
25+
26+ if twoway:: find_bytes ( & output. stderr , pat. as_bytes ( ) ) . is_none ( ) {
27+ dbg ! ( String :: from_utf8_lossy( & output. stdout) ) ;
28+ dbg ! ( String :: from_utf8_lossy( & output. stderr) ) ;
29+ bail ! ( "Command {:?} stderr did not match: {}" , cmd_str, pat) ;
2930 }
31+
3032 Ok ( ( ) )
3133}
3234
33- /// Run command and assert that its stdout contains pat
34- pub ( crate ) fn cmd_has_output < C : BorrowMut < Command > > ( mut c : C , pat : & str ) -> Result < ( ) > {
35- let c = c. borrow_mut ( ) ;
36- let o = c. output ( ) ?;
37- if !o. status . success ( ) {
38- bail ! ( "Command {:?} failed" , c) ;
35+ /// Run command and assert that its stdout contains `pat`
36+ pub ( crate ) fn cmd_has_output ( cmd : Cmd , pat : & str ) -> Result < ( ) > {
37+ let cmd_str = cmd. to_string ( ) ;
38+ let output = cmd. output ( ) ?; // run, error if command fails
39+
40+ if !output. status . success ( ) {
41+ bail ! ( "Command {} failed" , cmd_str) ;
3942 }
40- if twoway:: find_bytes ( & o . stdout , pat. as_bytes ( ) ) . is_none ( ) {
41- dbg ! ( String :: from_utf8_lossy( & o . stdout) ) ;
42- bail ! ( "Command {:? } stdout did not match: {}" , c , pat) ;
43+ if twoway:: find_bytes ( & output . stdout , pat. as_bytes ( ) ) . is_none ( ) {
44+ dbg ! ( String :: from_utf8_lossy( & output . stdout) ) ;
45+ bail ! ( "Command {} stdout did not match: {}" , cmd_str , pat) ;
4346 }
4447 Ok ( ( ) )
4548}
@@ -201,36 +204,30 @@ pub(crate) fn prepare_reboot<M: AsRef<str>>(mark: M) -> Result<()> {
201204mod tests {
202205 use super :: * ;
203206
204- fn oops ( ) -> Command {
205- let mut c = Command :: new ( "/bin/bash" ) ;
206- c. args ( & [ "-c" , "echo oops 1>&2; exit 1" ] ) ;
207- c
207+ fn oops ( sh : & Shell ) -> Cmd {
208+ cmd ! ( sh, "bash -c 'echo oops 1>&2; exit 1'" )
208209 }
209210
210211 #[ test]
211212 fn test_fails_with_matches ( ) -> Result < ( ) > {
212- cmd_fails_with ( Command :: new ( "false" ) , "" ) ?;
213- cmd_fails_with ( oops ( ) , "oops" ) ?;
213+ cmd_fails_with ( cmd ! ( sh , "false" ) , "" ) ?;
214+ cmd_fails_with ( oops ( & sh ) , "oops" ) ?;
214215 Ok ( ( ) )
215216 }
216217
217218 #[ test]
218- fn test_fails_with_fails ( ) {
219- cmd_fails_with ( Command :: new ( "true" ) , "somepat" ) . expect_err ( "true" ) ;
220- cmd_fails_with ( oops ( ) , "nomatch" ) . expect_err ( "nomatch" ) ;
219+ fn test_fails_with_fails ( ) -> Result < ( ) > {
220+ let sh = Shell :: new ( ) ?;
221+ cmd_fails_with ( cmd ! ( sh, "true" ) , "somepat" ) . expect_err ( "true" ) ;
222+ cmd_fails_with ( oops ( & sh) , "nomatch" ) . expect_err ( "nomatch" ) ;
223+ Ok ( ( ) )
221224 }
222225
223226 #[ test]
224227 fn test_output ( ) -> Result < ( ) > {
225- cmd_has_output ( Command :: new ( "true" ) , "" ) ?;
226- assert ! ( cmd_has_output( Command :: new( "true" ) , "foo" ) . is_err( ) ) ;
227- cmd_has_output (
228- sh_inline:: bash_command!( "echo foobarbaz; echo fooblahbaz" ) . unwrap ( ) ,
229- "blah" ,
230- ) ?;
231- assert ! (
232- cmd_has_output( sh_inline:: bash_command!( "echo foobarbaz" ) . unwrap( ) , "blah" ) . is_err( )
233- ) ;
228+ let sh = Shell :: new ( ) ?;
229+ cmd_has_output ( cmd ! ( sh, "echo foobarbaz; echo fooblahbaz" ) , "blah" ) ?;
230+ assert ! ( cmd_has_output( cmd!( sh, "echo foobarbaz" ) , "blah" ) . is_err( ) ) ;
234231 Ok ( ( ) )
235232 }
236233
0 commit comments