@@ -55,6 +55,42 @@ mod diff;
5555
5656use cli_error:: CliError ;
5757
58+
59+ /// Assert a CLI call
60+ ///
61+ /// To test that
62+ ///
63+ /// ```sh
64+ /// bash -c $BLACK_BOX
65+ /// ```
66+ ///
67+ /// exits with the correct exit value. You would call it like this:
68+ ///
69+ /// ```rust
70+ /// # extern crate assert_cli;
71+ /// # const BLACK_BOX: &'static str = r#"function test_helper() {\
72+ /// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
73+ /// assert_cli::assert_cli("bash", &["-c", BLACK_BOX]);
74+ /// ```
75+ pub fn assert_cli < S > ( cmd : & str , args : & [ S ] ) -> Result < ( ) , Box < Error > >
76+ where S : AsRef < OsStr >
77+ {
78+ let call: Result < Output , Box < Error > > = Command :: new ( cmd)
79+ . args ( args)
80+ . output ( )
81+ . map_err ( From :: from) ;
82+
83+ call. and_then ( |output| {
84+ if !output. status . success ( ) {
85+ return Err ( From :: from ( CliError :: WrongExitCode ( output) ) ) ;
86+ }
87+
88+ Ok ( ( ) )
89+ } )
90+ . map_err ( From :: from)
91+ }
92+
93+
5894/// Assert a CLI call returns the expected output.
5995///
6096/// To test that
@@ -103,6 +139,51 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
103139 . map_err ( From :: from)
104140}
105141
142+ /// Assert a CLI call that fails with a given error code.
143+ ///
144+ /// To test that
145+ ///
146+ /// ```sh
147+ /// bash -c $BLACK_BOX
148+ /// ```
149+ ///
150+ /// fails with an exit code of `42`.
151+ ///
152+ /// you would call it like this:
153+ ///
154+ /// ```rust
155+ /// # extern crate assert_cli;
156+ /// # const BLACK_BOX: &'static str = r#"function test_helper() {\
157+ /// # >&2 echo "error no 42!"; return 42; }; test_helper"#;
158+ /// assert_cli::assert_cli_error("bash", &["-c", BLACK_BOX], Some(42));
159+ /// ```
160+ pub fn assert_cli_error < S > ( cmd : & str ,
161+ args : & [ S ] ,
162+ error_code : Option < i32 > )
163+ -> Result < ( ) , Box < Error > >
164+ where S : AsRef < OsStr >
165+ {
166+ let call: Result < Output , Box < Error > > = Command :: new ( cmd)
167+ . args ( args)
168+ . output ( )
169+ . map_err ( From :: from) ;
170+
171+ call. and_then ( |output| {
172+ if output. status . success ( ) {
173+ return Err ( From :: from ( CliError :: WrongExitCode ( output) ) ) ;
174+ }
175+
176+ match ( error_code, output. status . code ( ) ) {
177+ ( Some ( a) , Some ( b) ) if a != b =>
178+ return Err ( From :: from ( CliError :: WrongExitCode ( output) ) ) ,
179+ _ => { }
180+ }
181+
182+ Ok ( ( ) )
183+ } )
184+ . map_err ( From :: from)
185+ }
186+
106187/// Assert a CLI call that fails the expected `stderr` output and error code.
107188///
108189/// To test that
@@ -170,14 +251,19 @@ pub fn assert_cli_output_error<S>(cmd: &str,
170251/// # >&2 echo "error no 66!"; return 66; }; test_helper"#;
171252///
172253/// fn main() {
254+ /// assert_cli!("true", &[""] => Success).unwrap();
173255/// assert_cli!("echo", &["42"] => Success, "42").unwrap();
256+ /// assert_cli!("bash", &["-c", BLACK_BOX] => Error 66).unwrap();
174257/// assert_cli!("bash", &["-c", BLACK_BOX] => Error 66, "error no 66!").unwrap();
175258/// }
176259/// ```
177260///
178261/// Make sure to include the crate as `#[macro_use] extern crate assert_cli;`.
179262#[ macro_export]
180263macro_rules! assert_cli {
264+ ( $cmd: expr, $args: expr => Success ) => { {
265+ $crate:: assert_cli( $cmd, $args)
266+ } } ;
181267 ( $cmd: expr, $args: expr => Success , $output: expr) => { {
182268 $crate:: assert_cli_output( $cmd, $args, $output)
183269 } } ;
@@ -187,4 +273,10 @@ macro_rules! assert_cli {
187273 ( $cmd: expr, $args: expr => Error $err: expr, $output: expr) => { {
188274 $crate:: assert_cli_output_error( $cmd, $args, Some ( $err) , $output)
189275 } } ;
276+ ( $cmd: expr, $args: expr => Error ) => { {
277+ $crate:: assert_cli_error( $cmd, $args, None )
278+ } } ;
279+ ( $cmd: expr, $args: expr => Error $err: expr) => { {
280+ $crate:: assert_cli_error( $cmd, $args, Some ( $err) )
281+ } } ;
190282}
0 commit comments