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

Commit f3b6e26

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

File tree

3 files changed

+98
-79
lines changed

3 files changed

+98
-79
lines changed

src/assert.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::process::{Command, Stdio};
33
use std::path::PathBuf;
44
use std::vec::Vec;
55
use std::io::Write;
6-
use std::convert::Into;
76

87
use errors::*;
98
use output::{OutputAssertion, OutputKind};
@@ -29,7 +28,7 @@ impl default::Default for Assert {
2928
Assert {
3029
cmd: vec!["cargo", "run", "--"]
3130
.into_iter().map(String::from).collect(),
32-
env: Environment::empty(),
31+
env: Environment::inherit(),
3332
current_dir: None,
3433
expect_success: Some(true),
3534
expect_exit_code: None,
@@ -281,7 +280,6 @@ impl Assert {
281280
.stdin(Stdio::piped())
282281
.stdout(Stdio::piped())
283282
.stderr(Stdio::piped())
284-
.env_clear()
285283
.envs(self.env.clone().compile())
286284
.args(&args);
287285

src/environment.rs

Lines changed: 97 additions & 4 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 {
@@ -67,7 +67,11 @@ impl Environment {
6767

6868
/// Compile Environment object
6969
pub fn compile(&self) -> Vec<(String, String)> {
70-
::std::env::vars().chain(self.vars.clone()).collect()
70+
if self.inherit {
71+
::std::env::vars().chain(self.vars.clone()).collect()
72+
} else {
73+
self.vars.clone()
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,92 @@ 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+
assert_eq!(y.clone().insert("key", "value").compile(), vec![("key".to_string(), "value".to_string())]);
155+
assert!(command()
156+
.with_env(y)
157+
.stdout().not().contains("key=value")
158+
.execute().is_ok());
159+
160+
}
161+
162+
#[test]
163+
fn take_vec() {
164+
let v = vec![("bar".to_string(), "baz".to_string())];
165+
166+
assert!(command().with_env(&vec![("bar", "baz")]).stdout().contains("bar=baz").execute().is_ok());
167+
assert!(command().with_env(&v).stdout().contains("bar=baz").execute().is_ok());
168+
assert!(command().with_env(&vec![("bar", "baz")]).stdout().isnt("").execute().is_ok());
169+
}
170+
171+
#[test]
172+
fn take_slice_of_strs() {
173+
assert!(command().with_env(&[("bar", "BAZ")]).stdout().contains("bar=BAZ").execute().is_ok());
174+
assert!(command().with_env(&[("bar", "BAZ")][..]).stdout().contains("bar=BAZ").execute().is_ok());
175+
assert!(command().with_env([("bar", "BAZ")].as_ref()).stdout().contains("bar=BAZ").execute().is_ok());
176+
}
177+
178+
#[test]
179+
fn take_slice_of_strings() {
180+
// same deal as above
181+
182+
assert!(command().with_env(&[("bar".to_string(), "BAZ".to_string())]).stdout().contains("bar=BAZ").execute().is_ok());
183+
assert!(command().with_env(&[("bar".to_string(), "BAZ".to_string())][..]).stdout().contains("bar=BAZ").execute().is_ok());
184+
}
185+
186+
#[test]
187+
fn take_slice() {
188+
assert!(command().with_env(&[("hey", "ho")]).stdout().contains("hey=ho").execute().is_ok());
189+
assert!(command().with_env(&[("hey", "ho".to_string())]).stdout().contains("hey=ho").execute().is_ok());
190+
}
191+
192+
#[test]
193+
fn take_string_i32() {
194+
assert!(command().with_env(&[("bar", 3 as i32)]).stdout().contains("bar=3").execute().is_ok());
195+
}
196+
}

tests/environment.rs

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

0 commit comments

Comments
 (0)