2
2
//!
3
3
//! This crate's goal is to provide you some very easy tools to test your CLI
4
4
//! applications. It can currently execute child processes and validate their
5
- //! exit status as well as stdout output against your assertions.
5
+ //! exit status as well as stdout and stderr output against your assertions.
6
6
//!
7
- //! ## Examples
7
+ //! Include the crate like
8
8
//!
9
- //! Here's a trivial example:
9
+ //! ```rust
10
+ //! #[macro_use] // <-- import the convenience macro (optional)
11
+ //! extern crate assert_cli;
12
+ //! # fn main() { }
13
+ //! ```
10
14
//!
11
- //! ```rust extern crate assert_cli;
15
+ //! ## Basic Examples
16
+ //!
17
+ //! Here's a trivial example:
12
18
//!
19
+ //! ```rust
13
20
//! assert_cli::Assert::command(&["echo", "42"])
14
21
//! .succeeds()
15
22
//! .and().prints("42")
20
27
//!
21
28
//! ```rust,should_panic
22
29
//! assert_cli::Assert::command(&["echo", "42"])
23
- //! .prints ("1337")
30
+ //! .prints_exactly ("1337")
24
31
//! .unwrap();
25
32
//! ```
26
33
//!
31
38
//! +42
32
39
//! ```
33
40
//!
41
+ //! ## Assert CLI Crates
42
+ //!
34
43
//! If you are testing a Rust binary crate, you can start with
35
44
//! `Assert::main_binary()` to use `cargo run` as command. Or, if you want to
36
45
//! run a specific binary (if you have more than one), use
37
46
//! `Assert::cargo_binary`.
38
47
//!
39
- //! Alternatively, you can use the `assert_cmd!` macro to construct the command:
48
+ //! ## `assert_cmd!` Macro
40
49
//!
41
- //! ```rust
42
- //! #[macro_use] extern crate assert_cli;
50
+ //! Alternatively, you can use the `assert_cmd!` macro to construct the command more conveniently:
43
51
//!
52
+ //! ```rust
53
+ //! # #[macro_use] extern crate assert_cli;
44
54
//! # fn main() {
45
55
//! assert_cmd!(echo 42).succeeds().prints("42").unwrap();
46
56
//! # }
47
57
//! ```
48
58
//!
49
- //! (Make sure to include the crate as `#[macro_use] extern crate assert_cli;`!)
59
+ //! Don't forget to import the crate with `#[macro_use]`. ;-)
60
+ //!
61
+ //! ## Don't Panic!
50
62
//!
51
63
//! If you don't want it to panic when the assertions are not met, simply call
52
64
//! `.execute` instead of `.unwrap` to get a `Result`:
53
65
//!
54
66
//! ```rust
55
- //! #[macro_use] extern crate assert_cli;
56
- //!
67
+ //! # #[macro_use] extern crate assert_cli;
57
68
//! # fn main() {
58
69
//! let x = assert_cmd!(echo 1337).prints_exactly("42").execute();
59
70
//! assert!(x.is_err());
@@ -74,7 +85,7 @@ use errors::*;
74
85
75
86
mod diff;
76
87
77
- /// Assertions for a specific command
88
+ /// Assertions for a specific command.
78
89
#[ derive( Debug ) ]
79
90
pub struct Assert {
80
91
cmd : Vec < String > ,
@@ -88,6 +99,8 @@ pub struct Assert {
88
99
89
100
impl std:: default:: Default for Assert {
90
101
/// Construct an assert using `cargo run --` as command.
102
+ ///
103
+ /// Defaults to asserting _successful_ execution.
91
104
fn default ( ) -> Self {
92
105
Assert {
93
106
cmd : vec ! [ "cargo" , "run" , "--" ]
@@ -103,12 +116,16 @@ impl std::default::Default for Assert {
103
116
}
104
117
105
118
impl Assert {
106
- /// Use the crate's main binary as command
119
+ /// Run the crate's main binary.
120
+ ///
121
+ /// Defaults to asserting _successful_ execution.
107
122
pub fn main_binary ( ) -> Self {
108
123
Assert :: default ( )
109
124
}
110
125
111
- /// Use the crate's main binary as command
126
+ /// Run a specific binary of the current crate.
127
+ ///
128
+ /// Defaults to asserting _successful_ execution.
112
129
pub fn cargo_binary ( name : & str ) -> Self {
113
130
Assert {
114
131
cmd : vec ! [ "cargo" , "run" , "--bin" , name, "--" ]
@@ -117,7 +134,9 @@ impl Assert {
117
134
}
118
135
}
119
136
120
- /// Use custom command
137
+ /// Run a custom command.
138
+ ///
139
+ /// Defaults to asserting _successful_ execution.
121
140
///
122
141
/// # Examples
123
142
///
@@ -135,7 +154,7 @@ impl Assert {
135
154
}
136
155
}
137
156
138
- /// Add arguments to the command
157
+ /// Add arguments to the command.
139
158
///
140
159
/// # Examples
141
160
///
@@ -153,7 +172,7 @@ impl Assert {
153
172
self
154
173
}
155
174
156
- /// Small helper to make chains more readable
175
+ /// Small helper to make chains more readable.
157
176
///
158
177
/// # Examples
159
178
///
@@ -168,7 +187,9 @@ impl Assert {
168
187
self
169
188
}
170
189
171
- /// Expect the command to be executed successfully
190
+ /// Expect the command to be executed successfully.
191
+ ///
192
+ /// Note: This is already set by default, so you only need this for explicitness.
172
193
///
173
194
/// # Examples
174
195
///
@@ -184,7 +205,10 @@ impl Assert {
184
205
self
185
206
}
186
207
187
- /// Expect the command to fail
208
+ /// Expect the command to fail.
209
+ ///
210
+ /// Note: This does not include shell failures like `command not found`. I.e. the
211
+ /// command must _run_ and fail for this assertion to pass.
188
212
///
189
213
/// # Examples
190
214
///
@@ -200,7 +224,7 @@ impl Assert {
200
224
self
201
225
}
202
226
203
- /// Expect the command to fail and return a specific error code
227
+ /// Expect the command to fail and return a specific error code.
204
228
///
205
229
/// # Examples
206
230
///
@@ -217,7 +241,7 @@ impl Assert {
217
241
self
218
242
}
219
243
220
- /// Expect the command's output to contain `output`
244
+ /// Expect the command's output to contain `output`.
221
245
///
222
246
/// # Examples
223
247
///
@@ -234,7 +258,7 @@ impl Assert {
234
258
self
235
259
}
236
260
237
- /// Expect the command to output exactly this `output`
261
+ /// Expect the command to output exactly this `output`.
238
262
///
239
263
/// # Examples
240
264
///
@@ -251,7 +275,7 @@ impl Assert {
251
275
self
252
276
}
253
277
254
- /// Expect the command's stderr output to contain `output`
278
+ /// Expect the command's stderr output to contain `output`.
255
279
///
256
280
/// # Examples
257
281
///
@@ -269,7 +293,7 @@ impl Assert {
269
293
self
270
294
}
271
295
272
- /// Expect the command to output exactly this `output` to stderr
296
+ /// Expect the command to output exactly this `output` to stderr.
273
297
///
274
298
/// # Examples
275
299
///
@@ -287,7 +311,7 @@ impl Assert {
287
311
self
288
312
}
289
313
290
- /// Execute the command and check the assertions
314
+ /// Execute the command and check the assertions.
291
315
///
292
316
/// # Examples
293
317
///
@@ -362,7 +386,7 @@ impl Assert {
362
386
Ok ( ( ) )
363
387
}
364
388
365
- /// Execute the command, check the assertions, and panic when they fail
389
+ /// Execute the command, check the assertions, and panic when they fail.
366
390
///
367
391
/// # Examples
368
392
///
@@ -380,7 +404,7 @@ impl Assert {
380
404
}
381
405
}
382
406
383
- /// Easily construct an `Assert` with a custom command
407
+ /// Easily construct an `Assert` with a custom command.
384
408
///
385
409
/// Make sure to include the crate as `#[macro_use] extern crate assert_cli;` if
386
410
/// you want to use this macro.
@@ -394,7 +418,7 @@ impl Assert {
394
418
/// No errors whatsoever
395
419
/// ```
396
420
///
397
- /// you would call it like this:
421
+ /// ..., you would call it like this:
398
422
///
399
423
/// ```rust
400
424
/// #[macro_use] extern crate assert_cli;
0 commit comments