@@ -75,7 +75,7 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
7575
7676 call. and_then ( |output| {
7777 if !output. status . success ( ) {
78- return Err ( From :: from ( CliError :: NoSuccess ( output) ) ) ;
78+ return Err ( From :: from ( CliError :: WrongExitCode ( output) ) ) ;
7979 }
8080
8181 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
@@ -90,3 +90,40 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
9090 } )
9191 . map_err ( From :: from)
9292}
93+
94+ /// Assert a CLI call fails with the expected error code and output.
95+ pub fn assert_cli_output_error < S > ( cmd : & str ,
96+ args : & [ S ] ,
97+ error_code : Option < i32 > ,
98+ expected_output : & str )
99+ -> Result < ( ) , Box < Error > >
100+ where S : AsRef < OsStr >
101+ {
102+ let call: Result < Output , Box < Error > > = Command :: new ( cmd)
103+ . args ( args)
104+ . output ( )
105+ . map_err ( From :: from) ;
106+
107+ call. and_then ( |output| {
108+ if output. status . success ( ) {
109+ return Err ( From :: from ( CliError :: WrongExitCode ( output) ) ) ;
110+ }
111+
112+ match ( error_code, output. status . code ( ) ) {
113+ ( Some ( a) , Some ( b) ) if a != b =>
114+ return Err ( From :: from ( CliError :: WrongExitCode ( output) ) ) ,
115+ _ => { }
116+ }
117+
118+ let stdout = String :: from_utf8_lossy ( & output. stderr ) ;
119+ let ( distance, changes) = difference:: diff ( expected_output. trim ( ) ,
120+ & stdout. trim ( ) ,
121+ "\n " ) ;
122+ if distance > 0 {
123+ return Err ( From :: from ( CliError :: OutputMissmatch ( changes) ) ) ;
124+ }
125+
126+ Ok ( ( ) )
127+ } )
128+ . map_err ( From :: from)
129+ }
0 commit comments