Skip to content
This repository was archived by the owner on Dec 29, 2021. It is now read-only.

Commit 68d7f2c

Browse files
committed
Auto merge of #7 - Lindenk:ignore_output, r=killercup
Option to ignore output Fixes #6. I was thinking about whether to implement it as interpreting an empty string as matching any output, but I can see how many situations might call for explicitly expecting a command not to output (such as when testing a quiet mode). Is this alright? Also, I think fuzzy matching should be a new issue. It would be a much different of an implementation than #6.
2 parents 738b41b + 4fb51df commit 68d7f2c

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "assert_cli"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
authors = ["Pascal Hertleif <killercup@gmail.com>"]
55
repository = "https://github.com/killercup/assert_cli.git"
66
homepage = "https://github.com/killercup/assert_cli"

Readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ this will show a nice, colorful diff in your terminal, like this:
4949
+42
5050
```
5151

52+
If you'd prefer to not check the output:
53+
54+
```rust
55+
#[macro_use] extern crate assert_cli;
56+
assert_cli::assert_cli("echo", &["42"]).unwrap();
57+
assert_cli!("echo", &["42"] => Success).unwrap();
58+
```
59+
5260
## License
5361

5462
Licensed under either of

src/lib.rs

+92
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,42 @@ mod diff;
5555

5656
use 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]
180263
macro_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
}

tests/macro.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ fn test_helper(exit_code: i32, output: &str) -> Vec<String> {
1010

1111
#[test]
1212
fn assert_success() {
13+
assert_cli!("true", &[""] => Success).unwrap();
1314
assert_cli!("echo", &["42"] => Success, "42").unwrap();
1415
assert!(assert_cli!("echo", &["1"] => Success, "42").is_err());
1516
}
1617

1718
#[test]
1819
fn assert_failure() {
20+
assert_cli!("bash", &test_helper(66, "sorry, my bad") => Error).unwrap();
1921
assert_cli!("bash", &test_helper(66, "sorry, my bad") => Error, "sorry, my bad").unwrap();
22+
assert_cli!("bash", &test_helper(42, "error no 42") => Error 42).unwrap();
2023
assert_cli!("bash", &test_helper(42, "error no 42") => Error 42, "error no 42").unwrap();
2124

2225
assert!(assert_cli!("echo", &["good"] => Error, "").is_err());

0 commit comments

Comments
 (0)