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

Commit 9b3ffd3

Browse files
committed
move tests to environment.rs
Signed-off-by: Freyskeyd <[email protected]>
1 parent fda48ec commit 9b3ffd3

File tree

3 files changed

+112
-81
lines changed

3 files changed

+112
-81
lines changed

src/assert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl default::Default for Assert {
2929
.into_iter()
3030
.map(String::from)
3131
.collect(),
32-
env: Environment::empty(),
32+
env: Environment::inherit(),
3333
current_dir: None,
3434
expect_success: Some(true),
3535
expect_exit_code: None,

src/environment.rs

Lines changed: 111 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#[derive(Clone, Debug, PartialEq, Eq)]
33
pub struct Environment {
44
/// Customized environment variables
5-
pub vars: Vec<(String, String)>,
5+
vars: Vec<(String, String)>,
66
/// Define if the structure must inherit
7-
pub inherit: bool,
7+
inherit: bool,
88
}
99

1010
impl Default for Environment {
@@ -25,7 +25,7 @@ impl Environment {
2525
/// extern crate assert_cli;
2626
///
2727
/// let e = assert_cli::Environment::inherit().compile();
28-
/// let e_ = ::std::env::vars();
28+
/// let e_: Vec<(String, String)> = ::std::env::vars().collect();
2929
///
3030
/// assert_eq!(e, e_);
3131
/// ```
@@ -57,17 +57,21 @@ impl Environment {
5757
/// ```rust
5858
/// extern crate assert_cli;
5959
///
60-
/// let e = assert_cli::Environment::empty().insert("boo", "bar").compile();
61-
/// assert_eq!(e, vec![("foo", "bar")];
60+
/// let e = assert_cli::Environment::empty().insert("foo", "bar").compile();
61+
/// assert_eq!(e, vec![("foo".to_string(), "bar".to_string())]);
6262
/// ```
6363
pub fn insert<S1: Into<String>, S2: Into<String>>(mut self, key: S1, val: S2) -> Self {
6464
self.vars.push((key.into(), val.into()));
6565
self
6666
}
6767

6868
/// Compile Environment object
69-
pub fn compile(&self) -> Vec<(String, String)> {
70-
::std::env::vars().chain(self.vars.clone()).collect()
69+
pub fn compile(self) -> Vec<(String, String)> {
70+
if self.inherit {
71+
::std::env::vars().chain(self.vars).collect()
72+
} else {
73+
self.vars
74+
}
7175
}
7276
}
7377

@@ -82,7 +86,7 @@ pub trait EnvironmentItem {
8286
fn to_environment_tuple(&self) -> (String, String);
8387
}
8488

85-
impl<'s, T: ToString, Z:ToString> EnvironmentItem for &'s (T, Z) {
89+
impl<'s, T: ToString, Z: ToString> EnvironmentItem for &'s (T, Z) {
8690
fn to_environment_tuple(&self) -> (String, String) {
8791
(self.0.to_string(), self.1.to_string())
8892
}
@@ -101,3 +105,102 @@ impl<'s, T> From<T> for Environment
101105
}
102106
}
103107

108+
109+
#[cfg(test)]
110+
mod test {
111+
use super::*;
112+
use ::Assert;
113+
114+
fn command() -> Assert {
115+
Assert::command(&["printenv"])
116+
}
117+
118+
#[test]
119+
fn take_ownership() {
120+
let x = Environment::inherit();
121+
122+
command()
123+
.with_env(x.clone())
124+
.with_env(&x)
125+
.with_env(x);
126+
}
127+
128+
#[test]
129+
fn in_place_mod() {
130+
let y = Environment::empty();
131+
132+
let y = y.insert("key", "value");
133+
134+
assert_eq!(y.compile(), vec![("key".to_string(), "value".to_string())]);
135+
}
136+
137+
#[test]
138+
fn in_place_mod2() {
139+
let x = Environment::inherit();
140+
141+
assert!(command()
142+
.with_env(&x.insert("key", "value").insert("key", "vv"))
143+
.stdout().contains("key=vv")
144+
.execute()
145+
.is_ok());
146+
// Granted, `insert` moved `x`, so we can no longer reference it, even
147+
// though only a reference was passed to `with_env`
148+
}
149+
150+
#[test]
151+
fn in_place_mod3() {
152+
// In-place modification while allowing later accesses to the `Environment`
153+
let y = Environment::empty();
154+
155+
assert_eq!(y.clone().insert("key", "value").compile(), vec![("key".to_string(), "value".to_string())]);
156+
assert!(command()
157+
.with_env(y)
158+
.stdout().not().contains("key=value")
159+
.execute().is_ok());
160+
}
161+
162+
#[test]
163+
fn empty_env() {
164+
// In-place modification while allowing later accesses to the `Environment`
165+
let y = Environment::empty();
166+
167+
assert!(command()
168+
.with_env(y)
169+
.stdout().is("")
170+
.execute().is_ok());
171+
}
172+
#[test]
173+
fn take_vec() {
174+
let v = vec![("bar".to_string(), "baz".to_string())];
175+
176+
assert!(command().with_env(&vec![("bar", "baz")]).stdout().contains("bar=baz").execute().is_ok());
177+
assert!(command().with_env(&v).stdout().contains("bar=baz").execute().is_ok());
178+
assert!(command().with_env(&vec![("bar", "baz")]).stdout().isnt("").execute().is_ok());
179+
}
180+
181+
#[test]
182+
fn take_slice_of_strs() {
183+
assert!(command().with_env(&[("bar", "BAZ")]).stdout().contains("bar=BAZ").execute().is_ok());
184+
assert!(command().with_env(&[("bar", "BAZ")][..]).stdout().contains("bar=BAZ").execute().is_ok());
185+
assert!(command().with_env([("bar", "BAZ")].as_ref()).stdout().contains("bar=BAZ").execute().is_ok());
186+
}
187+
188+
#[test]
189+
fn take_slice_of_strings() {
190+
// same deal as above
191+
192+
assert!(command().with_env(&[("bar".to_string(), "BAZ".to_string())]).stdout().contains("bar=BAZ").execute().is_ok());
193+
assert!(command().with_env(&[("bar".to_string(), "BAZ".to_string())][..]).stdout().contains("bar=BAZ").execute().is_ok());
194+
}
195+
196+
#[test]
197+
fn take_slice() {
198+
assert!(command().with_env(&[("hey", "ho")]).stdout().contains("hey=ho").execute().is_ok());
199+
assert!(command().with_env(&[("hey", "ho".to_string())]).stdout().contains("hey=ho").execute().is_ok());
200+
}
201+
202+
#[test]
203+
fn take_string_i32() {
204+
assert!(command().with_env(&[("bar", 3 as i32)]).stdout().contains("bar=3").execute().is_ok());
205+
}
206+
}

tests/environment.rs

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)