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

Commit 8cb7289

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

File tree

5 files changed

+208
-88
lines changed

5 files changed

+208
-88
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ matrix:
1111
env: CLIPPY_VERS="0.0.165"
1212
before_script: |
1313
[[ "$(cargo +nightly-2017-10-09 clippy --version)" != "$CLIPPY_VERS" ]] && \
14-
cargo +nightly-2017-10-09 install clippy --vers "$CLIPPY_VERS" --force
14+
cargo +nightly-2017-10-09 install clippy --vers "$CLIPPY_VERS" --force || true
1515
script: |
1616
cargo +nightly-2017-10-09 clippy -- -D warnings
1717
- rust: nightly-2017-10-09
1818
env: RUSTFMT_VERS="0.2.8"
1919
before_script: |
2020
[[ "$(cargo +nightly-2017-10-09 fmt -- --version)" != "$RUSTFMT_VERS"* ]] && \
21-
cargo +nightly-2017-10-09 install rustfmt-nightly --vers "$RUSTFMT_VERS" --force
21+
cargo +nightly-2017-10-09 install rustfmt-nightly --vers "$RUSTFMT_VERS" --force || true
2222
script: |
2323
cargo +nightly-2017-10-09 fmt --all -- --write-mode=diff
2424
before_script:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Assert CLI
2-
2+
33
> **Test CLI Applications** - This crate checks the output of a child process is as expected.
44
55
[![Build Status](https://travis-ci.org/killercup/assert_cli.svg)](https://travis-ci.org/killercup/assert_cli) [![Documentation](https://img.shields.io/badge/docs-master-blue.svg)][Documentation]

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: 204 additions & 12 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,22 +86,210 @@ 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
}
8993
}
9094

9195
impl<'s, T> From<T> for Environment
92-
where T: IntoIterator, T::Item: EnvironmentItem
96+
where
97+
T: IntoIterator,
98+
T::Item: EnvironmentItem,
9399
{
94100
fn from(v: T) -> Self {
95101
Self {
96-
vars: v.into_iter()
97-
.map(|k| k.to_environment_tuple())
98-
.collect(),
102+
vars: v.into_iter().map(|k| k.to_environment_tuple()).collect(),
99103
inherit: false,
100104
}
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().with_env(x.clone()).with_env(&x).with_env(x);
123+
}
124+
125+
#[test]
126+
fn in_place_mod() {
127+
let y = Environment::empty();
128+
129+
let y = y.insert("key", "value");
130+
131+
assert_eq!(y.compile(), vec![("key".to_string(), "value".to_string())]);
132+
}
133+
134+
#[test]
135+
fn in_place_mod2() {
136+
let x = Environment::inherit();
137+
138+
assert!(
139+
command()
140+
.with_env(&x.insert("key", "value").insert("key", "vv"))
141+
.stdout()
142+
.contains("key=vv")
143+
.execute()
144+
.is_ok()
145+
);
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!(
156+
y.clone().insert("key", "value").compile(),
157+
vec![("key".to_string(), "value".to_string())]
158+
);
159+
160+
assert!(
161+
command()
162+
.with_env(y)
163+
.stdout()
164+
.not()
165+
.contains("key=value")
166+
.execute()
167+
.is_ok()
168+
);
169+
}
170+
171+
#[test]
172+
fn empty_env() {
173+
// In-place modification while allowing later accesses to the `Environment`
174+
let y = Environment::empty();
175+
176+
assert!(command().with_env(y).stdout().is("").execute().is_ok());
177+
}
178+
#[test]
179+
fn take_vec() {
180+
let v = vec![("bar".to_string(), "baz".to_string())];
181+
182+
assert!(
183+
command()
184+
.with_env(&vec![("bar", "baz")])
185+
.stdout()
186+
.contains("bar=baz")
187+
.execute()
188+
.is_ok()
189+
);
190+
191+
assert!(
192+
command()
193+
.with_env(&v)
194+
.stdout()
195+
.contains("bar=baz")
196+
.execute()
197+
.is_ok()
198+
);
199+
200+
assert!(
201+
command()
202+
.with_env(&vec![("bar", "baz")])
203+
.stdout()
204+
.isnt("")
205+
.execute()
206+
.is_ok()
207+
);
208+
}
209+
210+
#[test]
211+
fn take_slice_of_strs() {
212+
assert!(
213+
command()
214+
.with_env(&[("bar", "BAZ")])
215+
.stdout()
216+
.contains("bar=BAZ")
217+
.execute()
218+
.is_ok()
219+
);
220+
221+
assert!(
222+
command()
223+
.with_env(&[("bar", "BAZ")][..])
224+
.stdout()
225+
.contains("bar=BAZ")
226+
.execute()
227+
.is_ok()
228+
);
229+
230+
assert!(
231+
command()
232+
.with_env([("bar", "BAZ")].as_ref())
233+
.stdout()
234+
.contains("bar=BAZ")
235+
.execute()
236+
.is_ok()
237+
);
238+
}
239+
240+
#[test]
241+
fn take_slice_of_strings() {
242+
// same deal as above
243+
244+
assert!(
245+
command()
246+
.with_env(&[("bar".to_string(), "BAZ".to_string())])
247+
.stdout()
248+
.contains("bar=BAZ")
249+
.execute()
250+
.is_ok()
251+
);
252+
253+
assert!(
254+
command()
255+
.with_env(&[("bar".to_string(), "BAZ".to_string())][..])
256+
.stdout()
257+
.contains("bar=BAZ")
258+
.execute()
259+
.is_ok()
260+
);
261+
}
262+
263+
#[test]
264+
fn take_slice() {
265+
assert!(
266+
command()
267+
.with_env(&[("hey", "ho")])
268+
.stdout()
269+
.contains("hey=ho")
270+
.execute()
271+
.is_ok()
272+
);
273+
274+
assert!(
275+
command()
276+
.with_env(&[("hey", "ho".to_string())])
277+
.stdout()
278+
.contains("hey=ho")
279+
.execute()
280+
.is_ok()
281+
);
282+
}
283+
284+
#[test]
285+
fn take_string_i32() {
286+
assert!(
287+
command()
288+
.with_env(&[("bar", 3 as i32)])
289+
.stdout()
290+
.contains("bar=3")
291+
.execute()
292+
.is_ok()
293+
);
294+
}
295+
}

tests/environment.rs

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

0 commit comments

Comments
 (0)