diff --git a/Cargo.toml b/Cargo.toml index c7df29e..af954fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "assert_cli" -version = "0.2.1" +version = "0.2.2" authors = ["Pascal Hertleif "] repository = "https://github.com/killercup/assert_cli.git" homepage = "https://github.com/killercup/assert_cli" diff --git a/Readme.md b/Readme.md index dace647..81ebaad 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 6a99334..418c05d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(cmd: &str, args: &[S]) -> Result<(), Box> + where S: AsRef +{ + let call: Result> = 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 @@ -103,6 +139,51 @@ pub fn assert_cli_output(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(cmd: &str, + args: &[S], + error_code: Option) + -> Result<(), Box> + where S: AsRef +{ + let call: Result> = 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 @@ -170,7 +251,9 @@ pub fn assert_cli_output_error(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(); /// } /// ``` @@ -178,6 +261,9 @@ pub fn assert_cli_output_error(cmd: &str, /// 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) }}; @@ -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)) + }}; } diff --git a/tests/macro.rs b/tests/macro.rs index 97700e2..2535a5f 100644 --- a/tests/macro.rs +++ b/tests/macro.rs @@ -10,13 +10,16 @@ fn test_helper(exit_code: i32, output: &str) -> Vec { #[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());