2
2
#[ derive( Clone , Debug , PartialEq , Eq ) ]
3
3
pub struct Environment {
4
4
/// Customized environment variables
5
- pub vars : Vec < ( String , String ) > ,
5
+ vars : Vec < ( String , String ) > ,
6
6
/// Define if the structure must inherit
7
- pub inherit : bool ,
7
+ inherit : bool ,
8
8
}
9
9
10
10
impl Default for Environment {
@@ -25,7 +25,7 @@ impl Environment {
25
25
/// extern crate assert_cli;
26
26
///
27
27
/// let e = assert_cli::Environment::inherit().compile();
28
- /// let e_ = ::std::env::vars();
28
+ /// let e_: Vec<(String, String)> = ::std::env::vars().collect ();
29
29
///
30
30
/// assert_eq!(e, e_);
31
31
/// ```
@@ -57,17 +57,21 @@ impl Environment {
57
57
/// ```rust
58
58
/// extern crate assert_cli;
59
59
///
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())]) ;
62
62
/// ```
63
63
pub fn insert < S1 : Into < String > , S2 : Into < String > > ( mut self , key : S1 , val : S2 ) -> Self {
64
64
self . vars . push ( ( key. into ( ) , val. into ( ) ) ) ;
65
65
self
66
66
}
67
67
68
68
/// 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
+ }
71
75
}
72
76
}
73
77
@@ -82,7 +86,7 @@ pub trait EnvironmentItem {
82
86
fn to_environment_tuple ( & self ) -> ( String , String ) ;
83
87
}
84
88
85
- impl < ' s , T : ToString , Z : ToString > EnvironmentItem for & ' s ( T , Z ) {
89
+ impl < ' s , T : ToString , Z : ToString > EnvironmentItem for & ' s ( T , Z ) {
86
90
fn to_environment_tuple ( & self ) -> ( String , String ) {
87
91
( self . 0 . to_string ( ) , self . 1 . to_string ( ) )
88
92
}
@@ -101,3 +105,102 @@ impl<'s, T> From<T> for Environment
101
105
}
102
106
}
103
107
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
+ }
0 commit comments