18
18
//!
19
19
//! ```rust
20
20
//! assert_cli::Assert::command(&["echo", "42"])
21
- //! .succeeds()
22
- //! .and().prints("42")
21
+ //! .prints("42")
23
22
//! .unwrap();
24
23
//! ```
25
24
//!
38
37
//! +42
39
38
//! ```
40
39
//!
41
- //! ## Assert CLI Crates
40
+ //! ## `assert_cmd!` Macro
42
41
//!
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.
47
44
//!
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
49
60
//!
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:
51
64
//!
52
65
//! ```rust
53
66
//! # #[macro_use] extern crate assert_cli;
54
67
//! # 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();
56
73
//! # }
57
74
//! ```
58
75
//!
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`.
60
91
//!
61
92
//! ## Don't Panic!
62
93
//!
66
97
//! ```rust
67
98
//! # #[macro_use] extern crate assert_cli;
68
99
//! # fn main() {
69
- //! let x = assert_cmd!(echo 1337).prints_exactly("42").execute();
100
+ //! let x = assert_cmd!(echo " 1337" ).prints_exactly("42").execute();
70
101
//! assert!(x.is_err());
71
102
//! # }
72
103
//! ```
@@ -175,7 +206,6 @@ impl Assert {
175
206
/// extern crate assert_cli;
176
207
///
177
208
/// assert_cli::Assert::command(&["echo", "1337"])
178
- /// .succeeds()
179
209
/// .unwrap();
180
210
/// ```
181
211
pub fn command ( cmd : & [ & str ] ) -> Self {
@@ -194,7 +224,6 @@ impl Assert {
194
224
///
195
225
/// assert_cli::Assert::command(&["echo"])
196
226
/// .with_args(&["42"])
197
- /// .succeeds()
198
227
/// .prints("42")
199
228
/// .unwrap();
200
229
/// ```
@@ -211,7 +240,7 @@ impl Assert {
211
240
/// extern crate assert_cli;
212
241
///
213
242
/// assert_cli::Assert::command(&["echo", "42"])
214
- /// .succeeds().and(). prints("42")
243
+ /// .prints("42")
215
244
/// .unwrap();
216
245
/// ```
217
246
pub fn and ( self ) -> Self {
@@ -226,7 +255,6 @@ impl Assert {
226
255
/// extern crate assert_cli;
227
256
///
228
257
/// assert_cli::Assert::command(&["echo", "42"])
229
- /// .succeeds()
230
258
/// .unwrap();
231
259
/// ```
232
260
pub fn succeeds ( mut self ) -> Self {
@@ -245,8 +273,10 @@ impl Assert {
245
273
/// ```rust
246
274
/// extern crate assert_cli;
247
275
///
248
- /// assert_cli::Assert::command(&["cat", "non-exisiting -file"])
276
+ /// assert_cli::Assert::command(&["cat", "non-existing -file"])
249
277
/// .fails()
278
+ /// .and()
279
+ /// .prints_error("non-existing-file")
250
280
/// .unwrap();
251
281
/// ```
252
282
pub fn fails ( mut self ) -> Self {
@@ -261,8 +291,10 @@ impl Assert {
261
291
/// ```rust
262
292
/// extern crate assert_cli;
263
293
///
264
- /// assert_cli::Assert::command(&["cat", "non-exisiting -file"])
294
+ /// assert_cli::Assert::command(&["cat", "non-existing -file"])
265
295
/// .fails_with(1)
296
+ /// .and()
297
+ /// .prints_error_exactly("cat: non-existing-file: No such file or directory")
266
298
/// .unwrap();
267
299
/// ```
268
300
pub fn fails_with ( mut self , expect_exit_code : i32 ) -> Self {
@@ -271,7 +303,7 @@ impl Assert {
271
303
self
272
304
}
273
305
274
- /// Expect the command's output to contain `output`.
306
+ /// Expect the command's output to ** contain** `output`.
275
307
///
276
308
/// # Examples
277
309
///
@@ -290,7 +322,7 @@ impl Assert {
290
322
self
291
323
}
292
324
293
- /// Expect the command to output exactly this `output`.
325
+ /// Expect the command to output ** exactly** this `output`.
294
326
///
295
327
/// # Examples
296
328
///
@@ -309,16 +341,17 @@ impl Assert {
309
341
self
310
342
}
311
343
312
- /// Expect the command's stderr output to contain `output`.
344
+ /// Expect the command's stderr output to ** contain** `output`.
313
345
///
314
346
/// # Examples
315
347
///
316
348
/// ```rust
317
349
/// extern crate assert_cli;
318
350
///
319
- /// assert_cli::Assert::command(&["cat", "non-exisiting -file"])
351
+ /// assert_cli::Assert::command(&["cat", "non-existing -file"])
320
352
/// .fails()
321
- /// .prints_error("non-exisiting-file")
353
+ /// .and()
354
+ /// .prints_error("non-existing-file")
322
355
/// .unwrap();
323
356
/// ```
324
357
pub fn prints_error < O : Into < String > > ( mut self , output : O ) -> Self {
@@ -329,16 +362,17 @@ impl Assert {
329
362
self
330
363
}
331
364
332
- /// Expect the command to output exactly this `output` to stderr.
365
+ /// Expect the command to output ** exactly** this `output` to stderr.
333
366
///
334
367
/// # Examples
335
368
///
336
369
/// ```rust
337
370
/// extern crate assert_cli;
338
371
///
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")
342
376
/// .unwrap();
343
377
/// ```
344
378
pub fn prints_error_exactly < O : Into < String > > ( mut self , output : O ) -> Self {
@@ -357,7 +391,7 @@ impl Assert {
357
391
/// extern crate assert_cli;
358
392
///
359
393
/// let test = assert_cli::Assert::command(&["echo", "42"])
360
- /// .succeeds( )
394
+ /// .prints("42" )
361
395
/// .execute();
362
396
/// assert!(test.is_ok());
363
397
/// ```
0 commit comments