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,22 +86,210 @@ 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
}
89
93
}
90
94
91
95
impl < ' s , T > From < T > for Environment
92
- where T : IntoIterator , T :: Item : EnvironmentItem
96
+ where
97
+ T : IntoIterator ,
98
+ T :: Item : EnvironmentItem ,
93
99
{
94
100
fn from ( v : T ) -> Self {
95
101
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 ( ) ,
99
103
inherit : false ,
100
104
}
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 ( ) . 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
+ }
0 commit comments