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

Commit 491d648

Browse files
committed
Free fns for Output constructors and add prelude
Originally based on #74
1 parent 063b3ce commit 491d648

File tree

5 files changed

+112
-75
lines changed

5 files changed

+112
-75
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ And here is one that will fail (and demonstrates running arbitrary commands):
3232

3333
```rust
3434
extern crate assert_cli;
35+
use assert_cli::prelude::*;
3536

3637
fn main() {
37-
assert_cli::Assert::command(&["ls", "foo-bar-foo"])
38+
Assert::command(&["ls", "foo-bar-foo"])
3839
.fails()
3940
.and()
40-
.stderr(assert_cli::Output::contains("foo-bar-foo"))
41+
.stderr(contains("foo-bar-foo"))
4142
.unwrap();
4243
}
4344
```
@@ -47,10 +48,11 @@ If you want to match the program's output _exactly_, you can use
4748

4849
```rust,should_panic
4950
#[macro_use] extern crate assert_cli;
51+
use assert_cli::prelude::*;
5052
5153
fn main() {
5254
assert_cmd!(wc "README.md")
53-
.stdout(assert_cli::Output::is("1337 README.md"))
55+
.stdout(is("1337 README.md"))
5456
.unwrap();
5557
}
5658
```

src/assert.rs

+60-45
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ impl Assert {
7777
///
7878
/// ```rust
7979
/// extern crate assert_cli;
80+
/// use assert_cli::prelude::*;
8081
///
81-
/// assert_cli::Assert::command(&["echo", "1337"])
82+
/// Assert::command(&["echo", "1337"])
8283
/// .unwrap();
8384
/// ```
8485
pub fn command<S: AsRef<OsStr>>(cmd: &[S]) -> Self {
@@ -94,12 +95,12 @@ impl Assert {
9495
///
9596
/// ```rust
9697
/// extern crate assert_cli;
98+
/// use assert_cli::prelude::*;
9799
///
98-
/// assert_cli::Assert::command(&["echo"])
100+
/// Assert::command(&["echo"])
99101
/// .with_args(&["42"])
100-
/// .stdout(assert_cli::Output::contains("42"))
102+
/// .stdout(contains("42"))
101103
/// .unwrap();
102-
///
103104
/// ```
104105
pub fn with_args<S: AsRef<OsStr>>(mut self, args: &[S]) -> Self {
105106
self.cmd.extend(args.into_iter().map(OsString::from));
@@ -112,10 +113,11 @@ impl Assert {
112113
///
113114
/// ```rust
114115
/// extern crate assert_cli;
116+
/// use assert_cli::prelude::*;
115117
///
116-
/// assert_cli::Assert::command(&["cat"])
118+
/// Assert::command(&["cat"])
117119
/// .stdin("42")
118-
/// .stdout(assert_cli::Output::contains("42"))
120+
/// .stdout(contains("42"))
119121
/// .unwrap();
120122
/// ```
121123
pub fn stdin(mut self, contents: &str) -> Self {
@@ -129,10 +131,11 @@ impl Assert {
129131
///
130132
/// ```rust
131133
/// extern crate assert_cli;
134+
/// use assert_cli::prelude::*;
132135
///
133-
/// assert_cli::Assert::command(&["wc", "lib.rs"])
136+
/// Assert::command(&["wc", "lib.rs"])
134137
/// .current_dir(std::path::Path::new("src"))
135-
/// .stdout(assert_cli::Output::contains("lib.rs"))
138+
/// .stdout(contains("lib.rs"))
136139
/// .execute()
137140
/// .unwrap();
138141
/// ```
@@ -147,26 +150,28 @@ impl Assert {
147150
///
148151
/// ```rust
149152
/// extern crate assert_cli;
153+
/// use assert_cli::prelude::*;
154+
/// use assert_cli::prelude::*;
150155
///
151-
/// assert_cli::Assert::command(&["printenv"])
156+
/// Assert::command(&["printenv"])
152157
/// .with_env(&[("TEST_ENV", "OK")])
153-
/// .stdout(assert_cli::Output::is("TEST_ENV=OK"))
158+
/// .stdout(is("TEST_ENV=OK"))
154159
/// .execute()
155160
/// .unwrap();
156161
///
157162
/// let env = assert_cli::Environment::empty()
158163
/// .insert("FOO", "BAR");
159164
///
160-
/// assert_cli::Assert::command(&["printenv"])
165+
/// Assert::command(&["printenv"])
161166
/// .with_env(&env)
162-
/// .stdout(assert_cli::Output::is("FOO=BAR"))
167+
/// .stdout(is("FOO=BAR"))
163168
/// .execute()
164169
/// .unwrap();
165170
///
166171
/// ::std::env::set_var("BAZ", "BAR");
167172
///
168-
/// assert_cli::Assert::command(&["printenv"])
169-
/// .stdout(assert_cli::Output::contains("BAZ=BAR"))
173+
/// Assert::command(&["printenv"])
174+
/// .stdout(contains("BAZ=BAR"))
170175
/// .execute()
171176
/// .unwrap();
172177
/// ```
@@ -182,11 +187,12 @@ impl Assert {
182187
///
183188
/// ```rust
184189
/// extern crate assert_cli;
190+
/// use assert_cli::prelude::*;
185191
///
186-
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
192+
/// Assert::command(&["cat", "non-existing-file"])
187193
/// .fails()
188194
/// .and()
189-
/// .stderr(assert_cli::Output::contains("non-existing-file"))
195+
/// .stderr(contains("non-existing-file"))
190196
/// .unwrap();
191197
/// ```
192198
pub fn and(self) -> Self {
@@ -199,8 +205,9 @@ impl Assert {
199205
///
200206
/// ```rust
201207
/// extern crate assert_cli;
208+
/// use assert_cli::prelude::*;
202209
///
203-
/// assert_cli::Assert::command(&["echo", "42"])
210+
/// Assert::command(&["echo", "42"])
204211
/// .succeeds()
205212
/// .unwrap();
206213
/// ```
@@ -219,11 +226,12 @@ impl Assert {
219226
///
220227
/// ```rust
221228
/// extern crate assert_cli;
229+
/// use assert_cli::prelude::*;
222230
///
223-
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
231+
/// Assert::command(&["cat", "non-existing-file"])
224232
/// .fails()
225233
/// .and()
226-
/// .stderr(assert_cli::Output::contains("non-existing-file"))
234+
/// .stderr(contains("non-existing-file"))
227235
/// .unwrap();
228236
/// ```
229237
pub fn fails(mut self) -> Self {
@@ -237,11 +245,12 @@ impl Assert {
237245
///
238246
/// ```rust
239247
/// extern crate assert_cli;
248+
/// use assert_cli::prelude::*;
240249
///
241-
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
250+
/// Assert::command(&["cat", "non-existing-file"])
242251
/// .fails_with(1)
243252
/// .and()
244-
/// .stderr(assert_cli::Output::contains("non-existing-file"))
253+
/// .stderr(contains("non-existing-file"))
245254
/// .unwrap();
246255
/// ```
247256
pub fn fails_with(mut self, expect_exit_code: i32) -> Self {
@@ -259,11 +268,12 @@ impl Assert {
259268
///
260269
/// ```rust
261270
/// extern crate assert_cli;
271+
/// use assert_cli::prelude::*;
262272
///
263-
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
273+
/// Assert::command(&["cat", "non-existing-file"])
264274
/// .ignore_status()
265275
/// .and()
266-
/// .stderr(assert_cli::Output::contains("non-existing-file"))
276+
/// .stderr(contains("non-existing-file"))
267277
/// .unwrap();
268278
/// ```
269279
///
@@ -280,9 +290,10 @@ impl Assert {
280290
///
281291
/// ```rust
282292
/// extern crate assert_cli;
293+
/// use assert_cli::prelude::*;
283294
///
284-
/// assert_cli::Assert::command(&["echo", "42"])
285-
/// .stdout(assert_cli::Output::contains("42"))
295+
/// Assert::command(&["echo", "42"])
296+
/// .stdout(contains("42"))
286297
/// .unwrap();
287298
/// ```
288299
pub fn stdout(mut self, pred: Output) -> Self {
@@ -296,11 +307,12 @@ impl Assert {
296307
///
297308
/// ```rust
298309
/// extern crate assert_cli;
310+
/// use assert_cli::prelude::*;
299311
///
300-
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
312+
/// Assert::command(&["cat", "non-existing-file"])
301313
/// .fails_with(1)
302314
/// .and()
303-
/// .stderr(assert_cli::Output::contains("non-existing-file"))
315+
/// .stderr(contains("non-existing-file"))
304316
/// .unwrap();
305317
/// ```
306318
pub fn stderr(mut self, pred: Output) -> Self {
@@ -314,9 +326,10 @@ impl Assert {
314326
///
315327
/// ```rust
316328
/// extern crate assert_cli;
329+
/// use assert_cli::prelude::*;
317330
///
318-
/// let test = assert_cli::Assert::command(&["echo", "42"])
319-
/// .stdout(assert_cli::Output::contains("42"))
331+
/// let test = Assert::command(&["echo", "42"])
332+
/// .stdout(contains("42"))
320333
/// .execute();
321334
/// assert!(test.is_ok());
322335
/// ```
@@ -386,8 +399,9 @@ impl Assert {
386399
///
387400
/// ```rust,should_panic="Assert CLI failure"
388401
/// extern crate assert_cli;
402+
/// use assert_cli::prelude::*;
389403
///
390-
/// assert_cli::Assert::command(&["echo", "42"])
404+
/// Assert::command(&["echo", "42"])
391405
/// .fails()
392406
/// .unwrap(); // panics
393407
/// ```
@@ -400,8 +414,9 @@ impl Assert {
400414

401415
#[cfg(test)]
402416
mod test {
403-
use super::*;
404417
use std::ffi::OsString;
418+
use super::*;
419+
use output::predicates::*;
405420

406421
fn command() -> Assert {
407422
Assert::command(&["printenv"])
@@ -436,7 +451,7 @@ mod test {
436451

437452
command()
438453
.with_env(&x.insert("key", "value").insert("key", "vv"))
439-
.stdout(Output::contains("key=vv"))
454+
.stdout(contains("key=vv"))
440455
.execute()
441456
.unwrap();
442457
// Granted, `insert` moved `x`, so we can no longer reference it, even
@@ -455,7 +470,7 @@ mod test {
455470

456471
command()
457472
.with_env(y)
458-
.stdout(Output::doesnt_contain("key=value"))
473+
.stdout(doesnt_contain("key=value"))
459474
.execute()
460475
.unwrap();
461476
}
@@ -468,7 +483,7 @@ mod test {
468483
assert!(
469484
command()
470485
.with_env(y)
471-
.stdout(Output::is(""))
486+
.stdout(is(""))
472487
.execute()
473488
.is_ok()
474489
);
@@ -479,19 +494,19 @@ mod test {
479494

480495
command()
481496
.with_env(&vec![("bar", "baz")])
482-
.stdout(Output::contains("bar=baz"))
497+
.stdout(contains("bar=baz"))
483498
.execute()
484499
.unwrap();
485500

486501
command()
487502
.with_env(&v)
488-
.stdout(Output::contains("bar=baz"))
503+
.stdout(contains("bar=baz"))
489504
.execute()
490505
.unwrap();
491506

492507
command()
493508
.with_env(&vec![("bar", "baz")])
494-
.stdout(Output::isnt(""))
509+
.stdout(isnt(""))
495510
.execute()
496511
.unwrap();
497512
}
@@ -500,19 +515,19 @@ mod test {
500515
fn take_slice_of_strs() {
501516
command()
502517
.with_env(&[("bar", "BAZ")])
503-
.stdout(Output::contains("bar=BAZ"))
518+
.stdout(contains("bar=BAZ"))
504519
.execute()
505520
.unwrap();
506521

507522
command()
508523
.with_env(&[("bar", "BAZ")][..])
509-
.stdout(Output::contains("bar=BAZ"))
524+
.stdout(contains("bar=BAZ"))
510525
.execute()
511526
.unwrap();
512527

513528
command()
514529
.with_env([("bar", "BAZ")].as_ref())
515-
.stdout(Output::contains("bar=BAZ"))
530+
.stdout(contains("bar=BAZ"))
516531
.execute()
517532
.unwrap();
518533
}
@@ -523,13 +538,13 @@ mod test {
523538

524539
command()
525540
.with_env(&[("bar".to_string(), "BAZ".to_string())])
526-
.stdout(Output::contains("bar=BAZ"))
541+
.stdout(contains("bar=BAZ"))
527542
.execute()
528543
.unwrap();
529544

530545
command()
531546
.with_env(&[("bar".to_string(), "BAZ".to_string())][..])
532-
.stdout(Output::contains("bar=BAZ"))
547+
.stdout(contains("bar=BAZ"))
533548
.execute()
534549
.unwrap();
535550
}
@@ -538,13 +553,13 @@ mod test {
538553
fn take_slice() {
539554
command()
540555
.with_env(&[("hey", "ho")])
541-
.stdout(Output::contains("hey=ho"))
556+
.stdout(contains("hey=ho"))
542557
.execute()
543558
.unwrap();
544559

545560
command()
546561
.with_env(&[("hey", "ho".to_string())])
547-
.stdout(Output::contains("hey=ho"))
562+
.stdout(contains("hey=ho"))
548563
.execute()
549564
.unwrap();
550565
}
@@ -553,7 +568,7 @@ mod test {
553568
fn take_string_i32() {
554569
command()
555570
.with_env(&[("bar", 3 as i32)])
556-
.stdout(Output::contains("bar=3"))
571+
.stdout(contains("bar=3"))
557572
.execute()
558573
.unwrap();
559574
}

src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,17 @@ pub use assert::Assert;
139139
/// It allow you to define/override environment variables for one or more assertions.
140140
pub use environment::Environment;
141141
pub use output::Output;
142+
143+
/// Convenience module to get all the good stuff
144+
///
145+
/// Glob import this module like this to quickly get all the important parts of
146+
/// this crate:
147+
///
148+
/// ```rust
149+
/// use assert_cli::prelude::*;
150+
/// ```
151+
pub mod prelude {
152+
pub use assert::Assert;
153+
pub use environment::Environment;
154+
pub use output::predicates::*;
155+
}

0 commit comments

Comments
 (0)