@@ -55,6 +55,42 @@ mod diff;
55
55
56
56
use cli_error:: CliError ;
57
57
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
+
58
94
/// Assert a CLI call returns the expected output.
59
95
///
60
96
/// To test that
@@ -103,6 +139,51 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
103
139
. map_err ( From :: from)
104
140
}
105
141
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
+
106
187
/// Assert a CLI call that fails the expected `stderr` output and error code.
107
188
///
108
189
/// To test that
@@ -170,14 +251,19 @@ pub fn assert_cli_output_error<S>(cmd: &str,
170
251
/// # >&2 echo "error no 66!"; return 66; }; test_helper"#;
171
252
///
172
253
/// fn main() {
254
+ /// assert_cli!("true", &[""] => Success).unwrap();
173
255
/// assert_cli!("echo", &["42"] => Success, "42").unwrap();
256
+ /// assert_cli!("bash", &["-c", BLACK_BOX] => Error 66).unwrap();
174
257
/// assert_cli!("bash", &["-c", BLACK_BOX] => Error 66, "error no 66!").unwrap();
175
258
/// }
176
259
/// ```
177
260
///
178
261
/// Make sure to include the crate as `#[macro_use] extern crate assert_cli;`.
179
262
#[ macro_export]
180
263
macro_rules! assert_cli {
264
+ ( $cmd: expr, $args: expr => Success ) => { {
265
+ $crate:: assert_cli( $cmd, $args)
266
+ } } ;
181
267
( $cmd: expr, $args: expr => Success , $output: expr) => { {
182
268
$crate:: assert_cli_output( $cmd, $args, $output)
183
269
} } ;
@@ -187,4 +273,10 @@ macro_rules! assert_cli {
187
273
( $cmd: expr, $args: expr => Error $err: expr, $output: expr) => { {
188
274
$crate:: assert_cli_output_error( $cmd, $args, Some ( $err) , $output)
189
275
} } ;
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
+ } } ;
190
282
}
0 commit comments