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

Commit 16aaa61

Browse files
committed
improve doc + test
Signed-off-by: Freyskeyd <[email protected]>
1 parent 43bbc3d commit 16aaa61

File tree

3 files changed

+95
-109
lines changed

3 files changed

+95
-109
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ like this:
7676
if there are special characters, which the macro doesn't accept, e.g.
7777
`assert_cmd!(cat "foo.txt")`.
7878

79+
Assert Cli use [Environment][Environment] underneath to deal with environment variables.
80+
7981
More detailed information is available in the [documentation]. :-)
8082

8183
## License
@@ -95,3 +97,4 @@ license, shall be dual licensed as above, without any additional terms or
9597
conditions.
9698

9799
[Documentation]: https://docs.rs/assert_cli
100+
[Environment]: https://github.com/Freyskeyd/environment

src/assert.rs

Lines changed: 88 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ impl Assert {
153153
/// .stdout().is("FOO=BAR")
154154
/// .execute()
155155
/// .unwrap();
156+
///
157+
/// ::std::env::set_var("BAZ", "BAR");
158+
///
159+
/// assert_cli::Assert::command(&["printenv"])
160+
/// .stdout().contains("BAZ=BAR")
161+
/// .execute()
162+
/// .unwrap();
156163
/// ```
157164
pub fn with_env<E: Into<Environment>>(mut self, env: E) -> Self {
158165
self.env = env.into();
@@ -495,14 +502,12 @@ mod test {
495502
fn in_place_mod2() {
496503
let x = Environment::inherit();
497504

498-
assert!(
499-
command()
500-
.with_env(&x.insert("key", "value").insert("key", "vv"))
501-
.stdout()
502-
.contains("key=vv")
503-
.execute()
504-
.is_ok()
505-
);
505+
command()
506+
.with_env(&x.insert("key", "value").insert("key", "vv"))
507+
.stdout()
508+
.contains("key=vv")
509+
.execute()
510+
.unwrap();
506511
// Granted, `insert` moved `x`, so we can no longer reference it, even
507512
// though only a reference was passed to `with_env`
508513
}
@@ -517,15 +522,13 @@ mod test {
517522
vec![(OsString::from("key"), OsString::from("value"))]
518523
);
519524

520-
assert!(
521-
command()
522-
.with_env(y)
523-
.stdout()
524-
.not()
525-
.contains("key=value")
526-
.execute()
527-
.is_ok()
528-
);
525+
command()
526+
.with_env(y)
527+
.stdout()
528+
.not()
529+
.contains("key=value")
530+
.execute()
531+
.unwrap();
529532
}
530533

531534
#[test]
@@ -539,117 +542,95 @@ mod test {
539542
fn take_vec() {
540543
let v = vec![("bar".to_string(), "baz".to_string())];
541544

542-
assert!(
543-
command()
544-
.with_env(&vec![("bar", "baz")])
545-
.stdout()
546-
.contains("bar=baz")
547-
.execute()
548-
.is_ok()
549-
);
545+
command()
546+
.with_env(&vec![("bar", "baz")])
547+
.stdout()
548+
.contains("bar=baz")
549+
.execute()
550+
.unwrap();
550551

551-
assert!(
552-
command()
553-
.with_env(&v)
554-
.stdout()
555-
.contains("bar=baz")
556-
.execute()
557-
.is_ok()
558-
);
552+
command()
553+
.with_env(&v)
554+
.stdout()
555+
.contains("bar=baz")
556+
.execute()
557+
.unwrap();
559558

560-
assert!(
561-
command()
562-
.with_env(&vec![("bar", "baz")])
563-
.stdout()
564-
.isnt("")
565-
.execute()
566-
.is_ok()
567-
);
559+
command()
560+
.with_env(&vec![("bar", "baz")])
561+
.stdout()
562+
.isnt("")
563+
.execute()
564+
.unwrap();
568565
}
569566

570567
#[test]
571568
fn take_slice_of_strs() {
572-
assert!(
573-
command()
574-
.with_env(&[("bar", "BAZ")])
575-
.stdout()
576-
.contains("bar=BAZ")
577-
.execute()
578-
.is_ok()
579-
);
580-
581-
assert!(
582-
command()
583-
.with_env(&[("bar", "BAZ")][..])
584-
.stdout()
585-
.contains("bar=BAZ")
586-
.execute()
587-
.is_ok()
588-
);
589-
590-
assert!(
591-
command()
592-
.with_env([("bar", "BAZ")].as_ref())
593-
.stdout()
594-
.contains("bar=BAZ")
595-
.execute()
596-
.is_ok()
597-
);
569+
command()
570+
.with_env(&[("bar", "BAZ")])
571+
.stdout()
572+
.contains("bar=BAZ")
573+
.execute()
574+
.unwrap();
575+
576+
command()
577+
.with_env(&[("bar", "BAZ")][..])
578+
.stdout()
579+
.contains("bar=BAZ")
580+
.execute()
581+
.unwrap();
582+
583+
command()
584+
.with_env([("bar", "BAZ")].as_ref())
585+
.stdout()
586+
.contains("bar=BAZ")
587+
.execute()
588+
.unwrap();
598589
}
599590

600591
#[test]
601592
fn take_slice_of_strings() {
602593
// same deal as above
603594

604-
assert!(
605-
command()
606-
.with_env(&[("bar".to_string(), "BAZ".to_string())])
607-
.stdout()
608-
.contains("bar=BAZ")
609-
.execute()
610-
.is_ok()
611-
);
595+
command()
596+
.with_env(&[("bar".to_string(), "BAZ".to_string())])
597+
.stdout()
598+
.contains("bar=BAZ")
599+
.execute()
600+
.unwrap();
612601

613-
assert!(
614-
command()
615-
.with_env(&[("bar".to_string(), "BAZ".to_string())][..])
616-
.stdout()
617-
.contains("bar=BAZ")
618-
.execute()
619-
.is_ok()
620-
);
602+
command()
603+
.with_env(&[("bar".to_string(), "BAZ".to_string())][..])
604+
.stdout()
605+
.contains("bar=BAZ")
606+
.execute()
607+
.unwrap();
621608
}
622609

623610
#[test]
624611
fn take_slice() {
625-
assert!(
626-
command()
627-
.with_env(&[("hey", "ho")])
628-
.stdout()
629-
.contains("hey=ho")
630-
.execute()
631-
.is_ok()
632-
);
612+
command()
613+
.with_env(&[("hey", "ho")])
614+
.stdout()
615+
.contains("hey=ho")
616+
.execute()
617+
.unwrap();
633618

634-
assert!(
635-
command()
636-
.with_env(&[("hey", "ho".to_string())])
637-
.stdout()
638-
.contains("hey=ho")
639-
.execute()
640-
.is_ok()
641-
);
619+
command()
620+
.with_env(&[("hey", "ho".to_string())])
621+
.stdout()
622+
.contains("hey=ho")
623+
.execute()
624+
.unwrap();
642625
}
643626

644627
#[test]
645628
fn take_string_i32() {
646-
assert!(
647-
command()
648-
.with_env(&[("bar", 3 as i32)])
649-
.stdout()
650-
.contains("bar=3")
651-
.execute()
652-
.is_ok()
653-
);
629+
command()
630+
.with_env(&[("bar", 3 as i32)])
631+
.stdout()
632+
.contains("bar=3")
633+
.execute()
634+
.unwrap();
654635
}
655636
}

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,11 @@
118118
#![deny(missing_docs)]
119119

120120
extern crate difference;
121+
extern crate environment;
121122
#[macro_use]
122123
extern crate error_chain;
123124
extern crate serde_json;
124125

125-
extern crate environment;
126-
127126
mod errors;
128127

129128
#[macro_use]
@@ -137,4 +136,7 @@ mod diff;
137136
mod assert;
138137
pub use assert::Assert;
139138
pub use assert::OutputAssertionBuilder;
139+
/// Environment is a re-export of the Environment crate
140+
///
141+
/// It allow you to define/override environment variables for one or more assertions.
140142
pub use environment::Environment;

0 commit comments

Comments
 (0)