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

Commit

Permalink
Auto merge of #7 - Lindenk:ignore_output, r=killercup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
homu committed Jul 10, 2016
2 parents 738b41b + 4fb51df commit 68d7f2c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "assert_cli"
version = "0.2.1"
version = "0.2.2"
authors = ["Pascal Hertleif <[email protected]>"]
repository = "https://github.com/killercup/assert_cli.git"
homepage = "https://github.com/killercup/assert_cli"
Expand Down
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ this will show a nice, colorful diff in your terminal, like this:
+42
```

If you'd prefer to not check the output:

```rust
#[macro_use] extern crate assert_cli;
assert_cli::assert_cli("echo", &["42"]).unwrap();
assert_cli!("echo", &["42"] => Success).unwrap();
```

## License

Licensed under either of
Expand Down
92 changes: 92 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,42 @@ mod diff;

use cli_error::CliError;


/// Assert a CLI call
///
/// To test that
///
/// ```sh
/// bash -c $BLACK_BOX
/// ```
///
/// exits with the correct exit value. You would call it like this:
///
/// ```rust
/// # extern crate assert_cli;
/// # const BLACK_BOX: &'static str = r#"function test_helper() {\
/// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
/// assert_cli::assert_cli("bash", &["-c", BLACK_BOX]);
/// ```
pub fn assert_cli<S>(cmd: &str, args: &[S]) -> Result<(), Box<Error>>
where S: AsRef<OsStr>
{
let call: Result<Output, Box<Error>> = Command::new(cmd)
.args(args)
.output()
.map_err(From::from);

call.and_then(|output| {
if !output.status.success() {
return Err(From::from(CliError::WrongExitCode(output)));
}

Ok(())
})
.map_err(From::from)
}


/// Assert a CLI call returns the expected output.
///
/// To test that
Expand Down Expand Up @@ -103,6 +139,51 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
.map_err(From::from)
}

/// Assert a CLI call that fails with a given error code.
///
/// To test that
///
/// ```sh
/// bash -c $BLACK_BOX
/// ```
///
/// fails with an exit code of `42`.
///
/// you would call it like this:
///
/// ```rust
/// # extern crate assert_cli;
/// # const BLACK_BOX: &'static str = r#"function test_helper() {\
/// # >&2 echo "error no 42!"; return 42; }; test_helper"#;
/// assert_cli::assert_cli_error("bash", &["-c", BLACK_BOX], Some(42));
/// ```
pub fn assert_cli_error<S>(cmd: &str,
args: &[S],
error_code: Option<i32>)
-> Result<(), Box<Error>>
where S: AsRef<OsStr>
{
let call: Result<Output, Box<Error>> = Command::new(cmd)
.args(args)
.output()
.map_err(From::from);

call.and_then(|output| {
if output.status.success() {
return Err(From::from(CliError::WrongExitCode(output)));
}

match (error_code, output.status.code()) {
(Some(a), Some(b)) if a != b =>
return Err(From::from(CliError::WrongExitCode(output))),
_ => {}
}

Ok(())
})
.map_err(From::from)
}

/// Assert a CLI call that fails the expected `stderr` output and error code.
///
/// To test that
Expand Down Expand Up @@ -170,14 +251,19 @@ pub fn assert_cli_output_error<S>(cmd: &str,
/// # >&2 echo "error no 66!"; return 66; }; test_helper"#;
///
/// fn main() {
/// assert_cli!("true", &[""] => Success).unwrap();
/// assert_cli!("echo", &["42"] => Success, "42").unwrap();
/// assert_cli!("bash", &["-c", BLACK_BOX] => Error 66).unwrap();
/// assert_cli!("bash", &["-c", BLACK_BOX] => Error 66, "error no 66!").unwrap();
/// }
/// ```
///
/// Make sure to include the crate as `#[macro_use] extern crate assert_cli;`.
#[macro_export]
macro_rules! assert_cli {
($cmd:expr, $args:expr => Success) => {{
$crate::assert_cli($cmd, $args)
}};
($cmd:expr, $args:expr => Success, $output:expr) => {{
$crate::assert_cli_output($cmd, $args, $output)
}};
Expand All @@ -187,4 +273,10 @@ macro_rules! assert_cli {
($cmd:expr, $args:expr => Error $err:expr, $output:expr) => {{
$crate::assert_cli_output_error($cmd, $args, Some($err), $output)
}};
($cmd:expr, $args:expr => Error) => {{
$crate::assert_cli_error($cmd, $args, None)
}};
($cmd:expr, $args:expr => Error $err:expr) => {{
$crate::assert_cli_error($cmd, $args, Some($err))
}};
}
3 changes: 3 additions & 0 deletions tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ fn test_helper(exit_code: i32, output: &str) -> Vec<String> {

#[test]
fn assert_success() {
assert_cli!("true", &[""] => Success).unwrap();
assert_cli!("echo", &["42"] => Success, "42").unwrap();
assert!(assert_cli!("echo", &["1"] => Success, "42").is_err());
}

#[test]
fn assert_failure() {
assert_cli!("bash", &test_helper(66, "sorry, my bad") => Error).unwrap();
assert_cli!("bash", &test_helper(66, "sorry, my bad") => Error, "sorry, my bad").unwrap();
assert_cli!("bash", &test_helper(42, "error no 42") => Error 42).unwrap();
assert_cli!("bash", &test_helper(42, "error no 42") => Error 42, "error no 42").unwrap();

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

0 comments on commit 68d7f2c

Please sign in to comment.