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

Commit f5ad27a

Browse files
committed
Assert CLI Error Docs, Tests
1 parent 5413d01 commit f5ad27a

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

Readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ extern crate assert_cli;
2525
assert_cli::assert_cli_output("echo", &["42"], "42").unwrap();
2626
```
2727

28+
Or if you'd rather use the macro:
29+
30+
```rust,ignore
31+
#[macro_use] extern crate assert_cli;
32+
assert_cli!("echo", &["42"] => Success, "42").unwrap();
33+
assert_cli!("black-box", &["--special"] => Error 42, "error no 42\n").unwrap()
34+
```
35+
2836
And here is one that will fail:
2937

3038
```rust,should_panic

src/lib.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@ use cli_error::CliError;
5757
/// To test that
5858
///
5959
/// ```sh
60-
/// ls -n1 src/
60+
/// bash -c $BLACK_BOX
6161
/// ```
6262
///
6363
/// returns
6464
///
6565
/// ```plain
66-
/// cli_error.rs
67-
/// diff.rs
68-
/// lib.rs
66+
/// Launch sequence initiated.
6967
/// ```
7068
///
7169
/// you would call it like this:
7270
///
73-
/// ```rust,no_run
71+
/// ```rust
7472
/// # extern crate assert_cli;
75-
/// assert_cli::assert_cli_output("ls", &["-n1", "src/"], "cli_error.rs\ndiff.rs\nlib.rs");
73+
/// # const BLACK_BOX: &'static str = r#"function test_helper() {\
74+
/// # echo "Launch sequence initiated."; return 0; }; test_helper"#;
75+
/// assert_cli::assert_cli_output("bash", &["-c", BLACK_BOX], "Launch sequence initiated.");
7676
/// ```
7777
pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Result<(), Box<Error>>
7878
where S: AsRef<OsStr>
@@ -100,7 +100,28 @@ pub fn assert_cli_output<S>(cmd: &str, args: &[S], expected_output: &str) -> Res
100100
.map_err(From::from)
101101
}
102102

103-
/// Assert a CLI call fails with the expected error code and output.
103+
/// Assert a CLI call that fails the expected `stderr` output and error code.
104+
///
105+
/// To test that
106+
///
107+
/// ```sh
108+
/// bash -c $BLACK_BOX
109+
/// ```
110+
///
111+
/// fails with an exit code of `42` after printing this to `stderr`
112+
///
113+
/// ```plain
114+
/// error no 42!
115+
/// ```
116+
///
117+
/// you would call it like this:
118+
///
119+
/// ```rust
120+
/// # extern crate assert_cli;
121+
/// # const BLACK_BOX: &'static str = r#"function test_helper() {\
122+
/// # >&2 echo "error no 42!"; return 42; }; test_helper"#;
123+
/// assert_cli::assert_cli_output_error("bash", &["-c", BLACK_BOX], Some(42), "error no 42!");
124+
/// ```
104125
pub fn assert_cli_output_error<S>(cmd: &str,
105126
args: &[S],
106127
error_code: Option<i32>,

tests/macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ fn assert_success() {
1616

1717
#[test]
1818
fn assert_failure() {
19-
assert_cli!("bash", &test_helper(66, "sorry, my bad") => Error, "sorry, my bad\n").unwrap();
20-
assert_cli!("bash", &test_helper(42, "error no 42") => Error 42, "error no 42\n").unwrap();
19+
assert_cli!("bash", &test_helper(66, "sorry, my bad") => Error, "sorry, my bad").unwrap();
20+
assert_cli!("bash", &test_helper(42, "error no 42") => Error 42, "error no 42").unwrap();
2121

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

0 commit comments

Comments
 (0)