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

Commit 434303a

Browse files
committed
Merge Colin's glorious review feedback into master
2 parents 05a88f2 + 4ba14ca commit 434303a

File tree

5 files changed

+353
-173
lines changed

5 files changed

+353
-173
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ homepage = "https://github.com/killercup/assert_cli"
99
documentation = "http://docs.rs/assert_cli/"
1010
readme = "README.md"
1111
categories = ["development-tools::testing"]
12-
keywords = ["cli", "testing"]
12+
keywords = ["cli", "testing", "assert"]
1313
build = "build.rs"
1414

1515
[dependencies]
1616
colored = "1.4"
1717
difference = "1.0"
1818
error-chain = "0.10.0"
19+
rustc-serialize = "0.3"
1920

2021
[build-dependencies]
2122
skeptic = "0.5"

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
# Assert CLI
22

3-
> Test CLI Applications.
3+
> **Test CLI Applications** - This crate checks the output of a child process is as expected.
44
5-
Currently, this crate only includes basic functionality to check the output of a child process
6-
is as expected.
7-
8-
[![Build Status](https://travis-ci.org/killercup/assert_cli.svg)](https://travis-ci.org/killercup/assert_cli) [![Coverage Status](https://coveralls.io/repos/killercup/assert_cli/badge.svg?branch=master&service=github)](https://coveralls.io/github/killercup/assert_cli?branch=master)
9-
10-
**[Documentation](http://killercup.github.io/assert_cli/)**
5+
[![Build Status](https://travis-ci.org/killercup/assert_cli.svg)](https://travis-ci.org/killercup/assert_cli) [![Documentation](https://img.shields.io/badge/docs-master-blue.svg)][Documentation]
116

127
## Install
138

@@ -30,13 +25,13 @@ fn main() {
3025
}
3126
```
3227

33-
Or if you'd rather use the macro:
28+
Or if you'd rather use the macro, to save you some writing:
3429

3530
```rust
3631
#[macro_use] extern crate assert_cli;
3732

3833
fn main() {
39-
assert_cmd!(echo 42).succeeds().and().prints("42").unwrap();
34+
assert_cmd!(echo "42").prints("42").unwrap();
4035
}
4136
```
4237

@@ -47,33 +42,42 @@ And here is one that will fail (which also shows `execute` which returns a
4742
#[macro_use] extern crate assert_cli;
4843

4944
fn main() {
50-
let test = assert_cmd!(grep amet Cargo.toml)
51-
.fails_with(1)
45+
let test = assert_cmd!(ls "foo-bar-foo")
46+
.fails()
47+
.and()
48+
.prints_error("foo-bar-foo")
5249
.execute();
53-
assert!(test.is_err());
50+
assert!(test.is_ok());
5451
}
5552
```
5653

57-
If you want to check for the program's output, you can use `print` or
58-
`print_exactly`:
54+
If you want to match the program's output _exactly_, you can use
55+
`prints_exactly`:
5956

60-
```rust,should_panic="Assert CLI failure"
57+
```rust,should_panic
6158
#[macro_use] extern crate assert_cli;
6259
6360
fn main() {
64-
assert_cmd!("wc" "README.md")
61+
assert_cmd!(wc "README.md")
6562
.prints_exactly("1337 README.md")
6663
.unwrap();
6764
}
6865
```
6966

70-
this will show a nice, colorful diff in your terminal, like this:
67+
... which has the benefit to show a nice, colorful diff in your terminal,
68+
like this:
7169

7270
```diff
7371
-1337
7472
+92
7573
```
7674

75+
**Tip**: Enclose arguments in the `assert_cmd!` macro in quotes `"`,
76+
if there are special characters, which the macro doesn't accept, e.g.
77+
`assert_cmd!(cat "foo.txt")`.
78+
79+
More detailed information is available in the [documentation]. :-)
80+
7781
## License
7882

7983
Licensed under either of
@@ -89,3 +93,5 @@ Unless you explicitly state otherwise, any contribution intentionally
8993
submitted for inclusion in the work by you, as defined in the Apache-2.0
9094
license, shall be dual licensed as above, without any additional terms or
9195
conditions.
96+
97+
[Documentation]: http://killercup.github.io/assert_cli/

src/errors.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const ERROR_PREFIX: &'static str = "CLI assertion failed";
2+
13
error_chain! {
24
foreign_links {
35
Io(::std::io::Error);
@@ -7,42 +9,43 @@ error_chain! {
79
StatusMismatch(cmd: Vec<String>, expected: bool) {
810
description("Wrong status")
911
display(
10-
"Command {:?} {got} but expected it to {expected}",
12+
"{}: `(command `{}` expected to {})` (command {})",
13+
ERROR_PREFIX,
1114
cmd.join(" "),
12-
got = if *expected { "failed" } else { "succeed" },
13-
expected = if *expected { "succeed" } else { "failed" },
15+
expected = if *expected { "succeed" } else { "fail" },
16+
got = if *expected { "failed" } else { "succeeded" },
1417
)
1518
}
1619
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>) {
1720
description("Wrong exit code")
1821
display(
19-
"Command {:?} exited with code {:?} but expected it to be {:?}",
20-
cmd.join(" "), got, expected,
22+
"{}: `(exit code of `{}` expected to be `{:?}`)` (exit code was: `{:?}`)",
23+
ERROR_PREFIX,
24+
cmd.join(" "),
25+
expected,
26+
got,
2127
)
2228
}
23-
OutputMismatch(expected: String, got: String) {
29+
OutputMismatch(output_name: String, cmd: Vec<String>, expected: String, got: String) {
2430
description("Output was not as expected")
2531
display(
26-
"Expected output to contain\n{}\nbut could not find it in\n{}",
32+
"{}: `({} of `{}` expected to contain `{:?}`)` (output was: `{:?}`)",
33+
ERROR_PREFIX,
34+
output_name,
35+
cmd.join(" "),
2736
expected,
2837
got,
2938
)
3039
}
31-
ExactOutputMismatch(diff: String) {
40+
ExactOutputMismatch(output_name: String, cmd: Vec<String>, diff: String) {
3241
description("Output was not as expected")
33-
display("{}", diff)
34-
}
35-
ErrorOutputMismatch(expected: String, got: String) {
36-
description("Stderr output was not as expected")
3742
display(
38-
"Expected stderr output to contain\n{}\nbut could not find it in\n{}",
39-
expected,
40-
got,
43+
"{}: `({} of `{}` was not as expected)`\n{}\n",
44+
ERROR_PREFIX,
45+
output_name,
46+
cmd.join(" "),
47+
diff.trim()
4148
)
4249
}
43-
ExactErrorOutputMismatch(diff: String) {
44-
description("Stderr output was not as expected")
45-
display("{}", diff)
46-
}
4750
}
4851
}

0 commit comments

Comments
 (0)