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

Commit 042fec4

Browse files
committed
Tests, documentation, assert_cmd!
1 parent 67aa602 commit 042fec4

File tree

6 files changed

+201
-93
lines changed

6 files changed

+201
-93
lines changed

Cargo.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "assert_cli"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["Pascal Hertleif <[email protected]>"]
55
repository = "https://github.com/killercup/assert_cli.git"
66
homepage = "https://github.com/killercup/assert_cli"
@@ -10,15 +10,10 @@ documentation = "http://killercup.github.io/assert_cli/"
1010
description = "Test CLI Applications."
1111
build = "build.rs"
1212

13-
[features]
14-
default = []
15-
dev = ["clippy"]
16-
1713
[dependencies]
1814
colored = "1.4"
1915
difference = "1.0"
2016
error-chain = "0.10.0"
21-
clippy = {version = "*", optional = true}
2217

2318
[build-dependencies]
2419
skeptic = "0.5"

README.md

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Just add it to your `Cargo.toml`:
1515

1616
```toml
1717
[dependencies]
18-
assert_cli = "0.3"
18+
assert_cli = "0.4"
1919
```
2020

2121
## Example
@@ -24,59 +24,56 @@ Here's a trivial example:
2424

2525
```rust
2626
extern crate assert_cli;
27+
2728
fn main() {
28-
assert_cli::assert_cli_output("echo", &["42"], "42").unwrap();
29+
assert_cli::Assert::command(&["echo", "42"]).prints("42").unwrap();
2930
}
3031
```
3132

3233
Or if you'd rather use the macro:
3334

34-
```rust,ignore
35+
```rust
3536
#[macro_use] extern crate assert_cli;
36-
fn main() {
37-
assert_cli!("echo", &["42"] => Success, "42").unwrap();
38-
assert_cli!("black-box", &["--special"] => Error 42, "error no 42\n").unwrap()
39-
}
40-
```
41-
42-
And here is one that will fail:
4337

44-
```rust,should_panic
45-
extern crate assert_cli;
4638
fn main() {
47-
assert_cli::assert_cli_output("echo", &["42"], "1337").unwrap();
39+
assert_cmd!(echo 42).succeeds().and().prints("42").unwrap();
4840
}
4941
```
5042

51-
this will show a nice, colorful diff in your terminal, like this:
52-
53-
```diff
54-
-1337
55-
+42
56-
```
57-
58-
If you'd prefer to not check the output:
43+
And here is one that will fail (which also shows `execute` which returns a
44+
`Result` and can be used instead of `unwrap`):
5945

6046
```rust
6147
#[macro_use] extern crate assert_cli;
48+
6249
fn main() {
63-
assert_cli::assert_cli("echo", &["42"]).unwrap();
64-
assert_cli!("echo", &["42"] => Success).unwrap();
50+
let test = assert_cmd!(grep amet Cargo.toml)
51+
.fails_with(1)
52+
.execute();
53+
assert!(test.is_err());
6554
}
6655
```
6756

68-
All exported functions and the macro return a `Result` containing the
69-
`Output` of the process, allowing you to do further custom assertions:
57+
If you want to check for the program's output, you can use `print` or
58+
`print_exactly`:
7059

71-
```rust
60+
```rust,should_panic="Assert CLI failure"
7261
#[macro_use] extern crate assert_cli;
62+
7363
fn main() {
74-
let output = assert_cli!("echo", &["Number 42"] => Success).unwrap();
75-
let stdout = std::str::from_utf8(&output.stdout).unwrap();
76-
assert!(stdout.contains("42"));
64+
assert_cmd!("wc" "README.md")
65+
.prints_exactly("1337 README.md")
66+
.unwrap();
7767
}
7868
```
7969

70+
this will show a nice, colorful diff in your terminal, like this:
71+
72+
```diff
73+
-1337
74+
+92
75+
```
76+
8077
## License
8178

8279
Licensed under either of

src/diff.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@ use std::fmt::Write;
66

77
use errors::*;
88

9-
pub fn render(Changeset { diffs, .. }: Changeset) -> Result<String> {
9+
pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result<String> {
1010
let mut t = String::new();
1111

1212
for i in 0..diffs.len() {
1313
match diffs[i] {
1414
Difference::Same(ref x) => {
1515
writeln!(t, " {}", x)?;
1616
}
17+
Difference::Rem(ref x) => {
18+
writeln!(t, "{}", format!("-{}", x).red())?;
19+
}
1720
Difference::Add(ref x) => {
1821
match diffs[i - 1] {
1922
Difference::Rem(ref y) => {
2023
write!(t, "{}", "+".green())?;
2124
let Changeset { diffs, .. } = Changeset::new(y, x, " ");
2225
for c in diffs {
2326
match c {
24-
Difference::Same(ref z) => {
27+
Difference::Same(ref z) if !z.is_empty() => {
2528
write!(t, "{}", z.green())?;
2629
write!(t, " ")?;
2730
}
28-
Difference::Add(ref z) => {
29-
write!(t, "{}", z.white().on_green())?;
31+
Difference::Add(ref z) if !z.is_empty() => {
32+
write!(t, "{}", z.green().reverse())?;
3033
write!(t, " ")?;
3134
}
3235
_ => (),
@@ -39,9 +42,6 @@ pub fn render(Changeset { diffs, .. }: Changeset) -> Result<String> {
3942
}
4043
};
4144
}
42-
Difference::Rem(ref x) => {
43-
writeln!(t, "{}", format!("-{}", x).red())?;
44-
}
4545
}
4646
}
4747

@@ -50,20 +50,19 @@ pub fn render(Changeset { diffs, .. }: Changeset) -> Result<String> {
5050

5151
#[cfg(test)]
5252
mod tests {
53-
use difference::diff;
5453
use super::*;
5554

5655
#[test]
5756
fn basic_diff() {
58-
let (_, diff) = diff("lol", "yay", "\n");
57+
let diff = Changeset::new("lol", "yay", "\n");
58+
println!("{}", render(&diff).unwrap());
5959
assert_eq!(render(&diff).unwrap(),
60-
"\u{1b}[31m-\u{1b}[0m\u{1b}[31mlol\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[32myay\
61-
\u{1b}[0m\n")
60+
" \n\u{1b}[31m-lol\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[7;32myay\u{1b}[0m \n")
6261
}
6362

6463
#[test]
6564
fn multiline_diff() {
66-
let (_, diff) = diff("Lorem ipsum dolor sit amet, consectetur adipisicing elit,
65+
let diff = Changeset::new("Lorem ipsum dolor sit amet, consectetur adipisicing elit,
6766
sed do eiusmod tempor incididunt ut labore et dolore magna
6867
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
6968
ullamco laboris nisi ut aliquip ex ea commodo consequat.",
@@ -72,6 +71,7 @@ sed do eiusmod tempor **incididunt** ut labore et dolore magna
7271
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
7372
ullamco laboris nisi ut aliquip ex ea commodo consequat.",
7473
"\n");
75-
assert_eq!(render(&diff).unwrap(), " Lorem ipsum dolor sit amet, consectetur adipisicing elit,\n\u{1b}[31m-\u{1b}[0m\u{1b}[31msed do eiusmod tempor incididunt ut labore et dolore magna\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[32msed do eiusmod tempor **incididunt** ut labore et dolore magna\u{1b}[0m\n aliqua. Ut enim ad minim veniam, quis nostrud exercitation\n ullamco laboris nisi ut aliquip ex ea commodo consequat.\n");
74+
println!("{}", render(&diff).unwrap());
75+
assert_eq!(render(&diff).unwrap(), " Lorem ipsum dolor sit amet, consectetur adipisicing elit,\n\u{1b}[31m-sed do eiusmod tempor incididunt ut labore et dolore magna\u{1b}[0m\n\u{1b}[32m+\u{1b}[0m\u{1b}[32msed do eiusmod tempor\u{1b}[0m \u{1b}[7;32m**incididunt**\u{1b}[0m \u{1b}[32mut labore et dolore magna\u{1b}[0m \n aliqua. Ut enim ad minim veniam, quis nostrud exercitation\nullamco laboris nisi ut aliquip ex ea commodo consequat.\n");
7676
}
7777
}

src/errors.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,34 @@ error_chain! {
33
Io(::std::io::Error);
44
Fmt(::std::fmt::Error);
55
}
6+
errors {
7+
StatusMismatch(cmd: Vec<String>, expected: bool) {
8+
description("Wrong status")
9+
display(
10+
"Command {:?} {got} but expected it to {expected}",
11+
cmd.join(" "),
12+
got = if *expected { "failed" } else { "succeed" },
13+
expected = if *expected { "succeed" } else { "failed" },
14+
)
15+
}
16+
ExitCodeMismatch(cmd: Vec<String>, expected: Option<i32>, got: Option<i32>) {
17+
description("Wrong exit code")
18+
display(
19+
"Command {:?} exited with code {:?} but expected it to be {:?}",
20+
cmd.join(" "), got, expected,
21+
)
22+
}
23+
OutputMismatch(expected: String, got: String) {
24+
description("Output was not as expected")
25+
display(
26+
"Expected output to contain\n{}\nbut could not find it in\n{}",
27+
expected,
28+
got,
29+
)
30+
}
31+
ExactOutputMismatch(diff: String) {
32+
description("Output was not as expected")
33+
display("{}", diff)
34+
}
35+
}
636
}

0 commit comments

Comments
 (0)