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

Commit 835a843

Browse files
committed
switch to OsString
Signed-off-by: Freyskeyd <[email protected]>
1 parent 8cb7289 commit 835a843

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Assert CLI
2-
32
> **Test CLI Applications** - This crate checks the output of a child process is as expected.
43
54
[![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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ impl Assert {
144144
/// .stdout().contains("TEST_ENV=OK")
145145
/// .execute()
146146
/// .unwrap();
147+
///
148+
/// let env = assert_cli::Environment::empty()
149+
/// .insert("FOO", "BAR");
150+
///
151+
/// assert_cli::Assert::command(&["printenv"])
152+
/// .with_env(&env)
153+
/// .stdout().is("FOO=BAR")
154+
/// .execute()
155+
/// .unwrap();
147156
/// ```
148157
pub fn with_env<E: Into<Environment>>(mut self, env: E) -> Self {
149158
self.env = env.into();

src/environment.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use std::ffi::OsString;
12
/// Structure to deal with environment variables
23
#[derive(Clone, Debug, PartialEq, Eq)]
34
pub struct Environment {
45
/// Customized environment variables
5-
vars: Vec<(String, String)>,
6+
vars: Vec<(OsString, OsString)>,
67
/// Define if the structure must inherit
78
inherit: bool,
89
}
@@ -17,15 +18,17 @@ impl Default for Environment {
1718
}
1819

1920
impl Environment {
20-
/// Create a new Environment object with inherit
21+
/// Create a new Environment that inherits this process' environment.
2122
///
2223
/// # Examples
2324
///
2425
/// ```rust
2526
/// extern crate assert_cli;
2627
///
28+
/// use std::ffi::OsString;
29+
///
2730
/// let e = assert_cli::Environment::inherit().compile();
28-
/// let e_: Vec<(String, String)> = ::std::env::vars().collect();
31+
/// let e_: Vec<(OsString, OsString)> = ::std::env::vars_os().collect();
2932
///
3033
/// assert_eq!(e, e_);
3134
/// ```
@@ -36,7 +39,7 @@ impl Environment {
3639
}
3740
}
3841

39-
/// Create a new Environment object without inheriting
42+
/// Create a new Environment independent of the current process's Environment
4043
///
4144
/// # Examples
4245
///
@@ -57,18 +60,20 @@ impl Environment {
5760
/// ```rust
5861
/// extern crate assert_cli;
5962
///
63+
/// use std::ffi::OsString;
64+
///
6065
/// let e = assert_cli::Environment::empty().insert("foo", "bar").compile();
61-
/// assert_eq!(e, vec![("foo".to_string(), "bar".to_string())]);
66+
/// assert_eq!(e, vec![(OsString::from("foo"), OsString::from("bar"))]);
6267
/// ```
63-
pub fn insert<S1: Into<String>, S2: Into<String>>(mut self, key: S1, val: S2) -> Self {
68+
pub fn insert<S1: Into<OsString>, S2: Into<OsString>>(mut self, key: S1, val: S2) -> Self {
6469
self.vars.push((key.into(), val.into()));
6570
self
6671
}
6772

6873
/// Compile Environment object
69-
pub fn compile(self) -> Vec<(String, String)> {
74+
pub fn compile(self) -> Vec<(OsString, OsString)> {
7075
if self.inherit {
71-
::std::env::vars().chain(self.vars).collect()
76+
::std::env::vars_os().chain(self.vars).collect()
7277
} else {
7378
self.vars
7479
}
@@ -83,12 +88,15 @@ impl<'a> From<&'a Environment> for Environment {
8388
}
8489

8590
pub trait EnvironmentItem {
86-
fn to_environment_tuple(&self) -> (String, String);
91+
fn to_environment_tuple(&self) -> (OsString, OsString);
8792
}
8893

8994
impl<'s, T: ToString, Z: ToString> EnvironmentItem for &'s (T, Z) {
90-
fn to_environment_tuple(&self) -> (String, String) {
91-
(self.0.to_string(), self.1.to_string())
95+
fn to_environment_tuple(&self) -> (OsString, OsString) {
96+
(
97+
OsString::from(self.0.to_string()),
98+
OsString::from(self.1.to_string()),
99+
)
92100
}
93101
}
94102

@@ -128,7 +136,10 @@ mod test {
128136

129137
let y = y.insert("key", "value");
130138

131-
assert_eq!(y.compile(), vec![("key".to_string(), "value".to_string())]);
139+
assert_eq!(
140+
y.compile(),
141+
vec![(OsString::from("key"), OsString::from("value"))]
142+
);
132143
}
133144

134145
#[test]
@@ -154,7 +165,7 @@ mod test {
154165

155166
assert_eq!(
156167
y.clone().insert("key", "value").compile(),
157-
vec![("key".to_string(), "value".to_string())]
168+
vec![(OsString::from("key"), OsString::from("value"))]
158169
);
159170

160171
assert!(

0 commit comments

Comments
 (0)