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

Commit e307b80

Browse files
committed
some doc changes
- explain asserting the exit codes - better reflect the fact that `success` is expected by default - tip/hint about the macro limitation
1 parent 8a01125 commit e307b80

File tree

2 files changed

+73
-33
lines changed

2 files changed

+73
-33
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ fn main() {
2525
}
2626
```
2727

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

3030
```rust
3131
#[macro_use] extern crate assert_cli;
3232

3333
fn main() {
34-
assert_cmd!(echo 42).succeeds().and().prints("42").unwrap();
34+
assert_cmd!(echo "42").prints("42").unwrap();
3535
}
3636
```
3737

@@ -42,8 +42,10 @@ And here is one that will fail (which also shows `execute` which returns a
4242
#[macro_use] extern crate assert_cli;
4343

4444
fn main() {
45-
let test = assert_cmd!(grep amet "Cargo.toml")
46-
.fails_with(1)
45+
let test = assert_cmd!(ls "foo-bar-foo")
46+
.fails()
47+
.and()
48+
.prints_error("foo-bar-foo")
4749
.execute();
4850
assert!(test.is_ok());
4951
}
@@ -56,7 +58,7 @@ If you want to match the program's output _exactly_, you can use
5658
#[macro_use] extern crate assert_cli;
5759
5860
fn main() {
59-
assert_cmd!("wc" "README.md")
61+
assert_cmd!(wc "README.md")
6062
.prints_exactly("1337 README.md")
6163
.unwrap();
6264
}
@@ -70,6 +72,10 @@ like this:
7072
+92
7173
```
7274

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+
7379
More detailed information is available in the [documentation]. :-)
7480

7581
## License

src/lib.rs

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
//!
1919
//! ```rust
2020
//! assert_cli::Assert::command(&["echo", "42"])
21-
//! .succeeds()
22-
//! .and().prints("42")
21+
//! .prints("42")
2322
//! .unwrap();
2423
//! ```
2524
//!
@@ -38,25 +37,57 @@
3837
//! +42
3938
//! ```
4039
//!
41-
//! ## Assert CLI Crates
40+
//! ## `assert_cmd!` Macro
4241
//!
43-
//! If you are testing a Rust binary crate, you can start with
44-
//! `Assert::main_binary()` to use `cargo run` as command. Or, if you want to
45-
//! run a specific binary (if you have more than one), use
46-
//! `Assert::cargo_binary`.
42+
//! Alternatively, you can use the `assert_cmd!` macro to construct the command more conveniently,
43+
//! but please carefully read the limitations below, or this may seriously go wrong.
4744
//!
48-
//! ## `assert_cmd!` Macro
45+
//! ```rust
46+
//! # #[macro_use] extern crate assert_cli;
47+
//! # fn main() {
48+
//! assert_cmd!(echo "42").prints("42").unwrap();
49+
//! # }
50+
//! ```
51+
//!
52+
//! **Tips**
53+
//!
54+
//! - Don't forget to import the crate with `#[macro_use]`. ;-)
55+
//! - Enclose arguments in the `assert_cmd!` macro in quotes `"`,
56+
//! if there are special characters, which the macro doesn't accept, e.g.
57+
//! `assert_cmd!(cat "foo.txt")`.
58+
//!
59+
//! ## Exit Status
4960
//!
50-
//! Alternatively, you can use the `assert_cmd!` macro to construct the command more conveniently:
61+
//! All assertion default to checking that the command exited with success.
62+
//!
63+
//! However, when you expect a command to fail, you can express it like this:
5164
//!
5265
//! ```rust
5366
//! # #[macro_use] extern crate assert_cli;
5467
//! # fn main() {
55-
//! assert_cmd!(echo 42).succeeds().prints("42").unwrap();
68+
//! assert_cmd!(cat "non-existing-file")
69+
//! .fails()
70+
//! .and()
71+
//! .prints_error("non-existing-file")
72+
//! .unwrap();
5673
//! # }
5774
//! ```
5875
//!
59-
//! Don't forget to import the crate with `#[macro_use]`. ;-)
76+
//! Some notes on this:
77+
//!
78+
//! - Use `fails_with` to assert a specific exit status.
79+
//! - There is also a `succeeds` method, but this is already the implicit default
80+
//! and can usually be omitted.
81+
//! - We can inspect the output of **stderr** with `prints_error` and `prints_error_exactly`.
82+
//! - The `and` method has no effect, other than to make everything more readable.
83+
//! Feel free to use it. :-)
84+
//!
85+
//! ## Assert CLI Crates
86+
//!
87+
//! If you are testing a Rust binary crate, you can start with
88+
//! `Assert::main_binary()` to use `cargo run` as command. Or, if you want to
89+
//! run a specific binary (if you have more than one), use
90+
//! `Assert::cargo_binary`.
6091
//!
6192
//! ## Don't Panic!
6293
//!
@@ -66,7 +97,7 @@
6697
//! ```rust
6798
//! # #[macro_use] extern crate assert_cli;
6899
//! # fn main() {
69-
//! let x = assert_cmd!(echo 1337).prints_exactly("42").execute();
100+
//! let x = assert_cmd!(echo "1337").prints_exactly("42").execute();
70101
//! assert!(x.is_err());
71102
//! # }
72103
//! ```
@@ -175,7 +206,6 @@ impl Assert {
175206
/// extern crate assert_cli;
176207
///
177208
/// assert_cli::Assert::command(&["echo", "1337"])
178-
/// .succeeds()
179209
/// .unwrap();
180210
/// ```
181211
pub fn command(cmd: &[&str]) -> Self {
@@ -194,7 +224,6 @@ impl Assert {
194224
///
195225
/// assert_cli::Assert::command(&["echo"])
196226
/// .with_args(&["42"])
197-
/// .succeeds()
198227
/// .prints("42")
199228
/// .unwrap();
200229
/// ```
@@ -211,7 +240,7 @@ impl Assert {
211240
/// extern crate assert_cli;
212241
///
213242
/// assert_cli::Assert::command(&["echo", "42"])
214-
/// .succeeds().and().prints("42")
243+
/// .prints("42")
215244
/// .unwrap();
216245
/// ```
217246
pub fn and(self) -> Self {
@@ -226,7 +255,6 @@ impl Assert {
226255
/// extern crate assert_cli;
227256
///
228257
/// assert_cli::Assert::command(&["echo", "42"])
229-
/// .succeeds()
230258
/// .unwrap();
231259
/// ```
232260
pub fn succeeds(mut self) -> Self {
@@ -245,8 +273,10 @@ impl Assert {
245273
/// ```rust
246274
/// extern crate assert_cli;
247275
///
248-
/// assert_cli::Assert::command(&["cat", "non-exisiting-file"])
276+
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
249277
/// .fails()
278+
/// .and()
279+
/// .prints_error("non-existing-file")
250280
/// .unwrap();
251281
/// ```
252282
pub fn fails(mut self) -> Self {
@@ -261,8 +291,10 @@ impl Assert {
261291
/// ```rust
262292
/// extern crate assert_cli;
263293
///
264-
/// assert_cli::Assert::command(&["cat", "non-exisiting-file"])
294+
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
265295
/// .fails_with(1)
296+
/// .and()
297+
/// .prints_error_exactly("cat: non-existing-file: No such file or directory")
266298
/// .unwrap();
267299
/// ```
268300
pub fn fails_with(mut self, expect_exit_code: i32) -> Self {
@@ -271,7 +303,7 @@ impl Assert {
271303
self
272304
}
273305

274-
/// Expect the command's output to contain `output`.
306+
/// Expect the command's output to **contain** `output`.
275307
///
276308
/// # Examples
277309
///
@@ -290,7 +322,7 @@ impl Assert {
290322
self
291323
}
292324

293-
/// Expect the command to output exactly this `output`.
325+
/// Expect the command to output **exactly** this `output`.
294326
///
295327
/// # Examples
296328
///
@@ -309,16 +341,17 @@ impl Assert {
309341
self
310342
}
311343

312-
/// Expect the command's stderr output to contain `output`.
344+
/// Expect the command's stderr output to **contain** `output`.
313345
///
314346
/// # Examples
315347
///
316348
/// ```rust
317349
/// extern crate assert_cli;
318350
///
319-
/// assert_cli::Assert::command(&["cat", "non-exisiting-file"])
351+
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
320352
/// .fails()
321-
/// .prints_error("non-exisiting-file")
353+
/// .and()
354+
/// .prints_error("non-existing-file")
322355
/// .unwrap();
323356
/// ```
324357
pub fn prints_error<O: Into<String>>(mut self, output: O) -> Self {
@@ -329,16 +362,17 @@ impl Assert {
329362
self
330363
}
331364

332-
/// Expect the command to output exactly this `output` to stderr.
365+
/// Expect the command to output **exactly** this `output` to stderr.
333366
///
334367
/// # Examples
335368
///
336369
/// ```rust
337370
/// extern crate assert_cli;
338371
///
339-
/// assert_cli::Assert::command(&["cat", "non-exisiting-file"])
340-
/// .fails()
341-
/// .prints_error_exactly("cat: non-exisiting-file: No such file or directory")
372+
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
373+
/// .fails_with(1)
374+
/// .and()
375+
/// .prints_error_exactly("cat: non-existing-file: No such file or directory")
342376
/// .unwrap();
343377
/// ```
344378
pub fn prints_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
@@ -357,7 +391,7 @@ impl Assert {
357391
/// extern crate assert_cli;
358392
///
359393
/// let test = assert_cli::Assert::command(&["echo", "42"])
360-
/// .succeeds()
394+
/// .prints("42")
361395
/// .execute();
362396
/// assert!(test.is_ok());
363397
/// ```

0 commit comments

Comments
 (0)