1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#![deny(missing_docs)]
use std::collections::HashMap;
use std::env;
use std::ffi::OsStr;
use std::hash::Hash;
use std::panic::{self, RefUnwindSafe, UnwindSafe};
use std::sync::Mutex;
use once_cell::sync::Lazy;
static SERIAL_TEST: Lazy<Mutex<()>> = Lazy::new(Default::default);
pub fn with_var<K, V, F>(key: K, value: Option<V>, closure: F)
where
K: AsRef<OsStr> + Clone + Eq + Hash,
V: AsRef<OsStr> + Clone,
F: Fn() + UnwindSafe + RefUnwindSafe,
{
with_vars(vec![(key, value)], closure)
}
pub fn with_var_unset<K, F>(key: K, closure: F)
where
K: AsRef<OsStr> + Clone + Eq + Hash,
F: Fn() + UnwindSafe + RefUnwindSafe,
{
with_var(key, None::<&str>, closure);
}
pub fn with_vars<K, V, F>(kvs: Vec<(K, Option<V>)>, closure: F)
where
K: AsRef<OsStr> + Clone + Eq + Hash,
V: AsRef<OsStr> + Clone,
F: Fn() + UnwindSafe + RefUnwindSafe,
{
let guard = SERIAL_TEST.lock().unwrap();
let mut old_kvs: HashMap<K, Option<String>> = HashMap::new();
for (key, value) in kvs {
if !old_kvs.contains_key(&key) {
let old_value = env::var(&key).ok();
old_kvs.insert(key.clone(), old_value);
}
update_env(&key, value);
}
match panic::catch_unwind(|| {
closure();
}) {
Ok(_) => {
for (key, value) in old_kvs {
update_env(key, value);
}
}
Err(err) => {
for (key, value) in old_kvs {
update_env(key, value);
}
drop(guard);
panic::resume_unwind(err);
}
};
}
pub fn with_vars_unset<K, F>(keys: Vec<K>, closure: F)
where
K: AsRef<OsStr> + Clone + Eq + Hash,
F: Fn() + UnwindSafe + RefUnwindSafe,
{
let kvs = keys.iter().map(|key| (key, None::<&str>)).collect();
with_vars(kvs, closure);
}
fn update_env<K, V>(key: K, value: Option<V>)
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
match value {
Some(v) => env::set_var(key, v),
None => env::remove_var(key),
}
}
#[cfg(test)]
mod tests {
use std::env::VarError;
use std::{env, panic};
#[test]
fn test_with_var_set() {
let hello_not_set = env::var("HELLO");
assert!(hello_not_set.is_err(), "`HELLO` must not be set.");
crate::with_var("HELLO", Some("world!"), || {
let hello_is_set = env::var("HELLO").unwrap();
assert_eq!(hello_is_set, "world!", "`HELLO` must be set to \"world!\".");
});
let hello_not_set_after = env::var("HELLO");
assert!(hello_not_set_after.is_err(), "`HELLO` must not be set.");
}
#[test]
fn test_with_var_set_to_none() {
env::set_var("FOO", "bar");
let foo_is_set = env::var("FOO").unwrap();
assert_eq!(foo_is_set, "bar", "`FOO` must be set to \"bar\".");
crate::with_var("FOO", None::<&str>, || {
let foo_not_set = env::var("FOO");
assert!(foo_not_set.is_err(), "`FOO` must not be set.");
});
let foo_is_set_after = env::var("FOO").unwrap();
assert_eq!(foo_is_set_after, "bar", "`FOO` must be set to \"bar\".");
}
#[test]
fn test_with_var_unset() {
env::set_var("BAR", "foo");
let foo_is_set = env::var("BAR").unwrap();
assert_eq!(foo_is_set, "foo", "`BAR` must be set to \"foo\".");
crate::with_var_unset("BAR", || {
let foo_not_set = env::var("BAR");
assert!(foo_not_set.is_err(), "`BAR` must not be set.");
});
let foo_is_set_after = env::var("BAR").unwrap();
assert_eq!(foo_is_set_after, "foo", "`BAR` must be set to \"foo\".");
}
#[test]
fn test_with_var_override() {
env::set_var("BLAH", "blub");
let blah_is_set = env::var("BLAH").unwrap();
assert_eq!(blah_is_set, "blub", "`BLAH` must be set to \"blah\".");
crate::with_var("BLAH", Some("new"), || {
let blah_is_set_new = env::var("BLAH").unwrap();
assert_eq!(blah_is_set_new, "new", "`BLAH` must be set to \"newb\".");
});
let blah_is_set_after = env::var("BLAH").unwrap();
assert_eq!(
blah_is_set_after, "blub",
"`BLAH` must be set to \"blubr\"."
);
}
#[test]
fn test_with_var_panic() {
env::set_var("PANIC", "panic");
let panic_is_set = env::var("PANIC").unwrap();
assert_eq!(panic_is_set, "panic", "`PANIC` must be set to \"panic\".");
let did_panic = panic::catch_unwind(|| {
crate::with_var("PANIC", Some("don't panic"), || {
let panic_is_set_new = env::var("PANIC").unwrap();
assert_eq!(
panic_is_set_new, "don't panic",
"`PANIC` must be set to \"don't panic\"."
);
panic!("abort this closure with a panic.");
});
});
assert!(did_panic.is_err(), "The closure must panic.");
let panic_is_set_after = env::var("PANIC").unwrap();
assert_eq!(
panic_is_set_after, "panic",
"`PANIC` must be set to \"panic\"."
);
}
#[test]
fn test_with_vars_set() {
let one_not_set = env::var("ONE");
assert!(one_not_set.is_err(), "`ONE` must not be set.");
let two_not_set = env::var("TWO");
assert!(two_not_set.is_err(), "`TWO` must not be set.");
crate::with_vars(vec![("ONE", Some("1")), ("TWO", Some("2"))], || {
let one_is_set = env::var("ONE").unwrap();
assert_eq!(one_is_set, "1", "`ONE` must be set to \"1\".");
let two_is_set = env::var("TWO").unwrap();
assert_eq!(two_is_set, "2", "`TWO` must be set to \"2\".");
});
let one_not_set_after = env::var("ONE");
assert!(one_not_set_after.is_err(), "`ONE` must not be set.");
let two_not_set_after = env::var("TWO");
assert!(two_not_set_after.is_err(), "`TWO` must not be set.");
}
#[test]
fn test_with_vars_unset() {
env::set_var("SET_TO_BE_UNSET", "val");
env::remove_var("UNSET_TO_BE_UNSET");
assert_eq!(env::var("SET_TO_BE_UNSET"), Ok("val".to_string()));
assert_eq!(env::var("UNSET_TO_BE_UNSET"), Err(VarError::NotPresent));
crate::with_vars_unset(vec!["SET_TO_BE_UNSET", "UNSET_TO_BE_UNSET"], || {
assert_eq!(env::var("SET_TO_BE_UNSET"), Err(VarError::NotPresent));
assert_eq!(env::var("UNSET_TO_BE_UNSET"), Err(VarError::NotPresent));
});
assert_eq!(env::var("SET_TO_BE_UNSET"), Ok("val".to_string()));
assert_eq!(env::var("UNSET_TO_BE_UNSET"), Err(VarError::NotPresent));
}
#[test]
fn test_with_vars_partially_unset() {
let to_be_set_not_set = env::var("TO_BE_SET");
assert!(to_be_set_not_set.is_err(), "`TO_BE_SET` must not be set.");
env::set_var("TO_BE_UNSET", "unset");
let to_be_unset_is_set = env::var("TO_BE_UNSET").unwrap();
assert_eq!(
to_be_unset_is_set, "unset",
"`TO_BE_UNSET` must be set to \"unset\"."
);
crate::with_vars(
vec![("TO_BE_SET", Some("set")), ("TO_BE_UNSET", None::<&str>)],
|| {
let to_be_set_is_set = env::var("TO_BE_SET").unwrap();
assert_eq!(
to_be_set_is_set, "set",
"`TO_BE_SET` must be set to \"set\"."
);
let to_be_unset_not_set = env::var("TO_BE_UNSET");
assert!(
to_be_unset_not_set.is_err(),
"`TO_BE_UNSET` must not be set."
);
},
);
let to_be_set_not_set_after = env::var("TO_BE_SET");
assert!(
to_be_set_not_set_after.is_err(),
"`TO_BE_SET` must not be set."
);
let to_be_unset_is_set_after = env::var("TO_BE_UNSET").unwrap();
assert_eq!(
to_be_unset_is_set_after, "unset",
"`TO_BE_UNSET` must be set to \"unset\"."
);
}
#[test]
fn test_with_vars_override() {
env::set_var("DOIT", "doit");
let doit_is_set = env::var("DOIT").unwrap();
assert_eq!(doit_is_set, "doit", "`DOIT` must be set to \"doit\".");
env::set_var("NOW", "now");
let now_is_set = env::var("NOW").unwrap();
assert_eq!(now_is_set, "now", "`NOW` must be set to \"now\".");
crate::with_vars(
vec![("DOIT", Some("other")), ("NOW", Some("value"))],
|| {
let doit_is_set_new = env::var("DOIT").unwrap();
assert_eq!(doit_is_set_new, "other", "`DOIT` must be set to \"other\".");
let now_is_set_new = env::var("NOW").unwrap();
assert_eq!(now_is_set_new, "value", "`NOW` must be set to \"value\".");
},
);
let doit_is_set_after = env::var("DOIT").unwrap();
assert_eq!(doit_is_set_after, "doit", "`DOIT` must be set to \"doit\".");
let now_is_set_after = env::var("NOW").unwrap();
assert_eq!(now_is_set_after, "now", "`NOW` must be set to \"now\".");
}
#[test]
fn test_with_vars_same_vars() {
let override_not_set = env::var("OVERRIDE");
assert!(override_not_set.is_err(), "`OVERRIDE` must not be set.");
crate::with_vars(
vec![
("OVERRIDE", Some("initial")),
("OVERRIDE", Some("override")),
],
|| {
let override_is_set = env::var("OVERRIDE").unwrap();
assert_eq!(
override_is_set, "override",
"`OVERRIDE` must be set to \"override\"."
);
},
);
let override_not_set_after = env::var("OVERRIDE");
assert!(
override_not_set_after.is_err(),
"`OVERRIDE` must not be set."
);
}
#[test]
fn test_with_vars_unset_set() {
env::set_var("MY_VAR", "my_var");
let my_var_is_set = env::var("MY_VAR").unwrap();
assert_eq!(
my_var_is_set, "my_var",
"`MY_VAR` must be set to \"my_var`\"."
);
crate::with_vars(
vec![("MY_VAR", None::<&str>), ("MY_VAR", Some("new value"))],
|| {
let my_var_is_set_new = env::var("MY_VAR").unwrap();
assert_eq!(
my_var_is_set_new, "new value",
"`MY_VAR` must be set to \"new value\"."
);
},
);
let my_var_is_set_after = env::var("MY_VAR").unwrap();
assert_eq!(
my_var_is_set_after, "my_var",
"`MY_VAR` must be set to \"my_var\"."
);
}
#[test]
fn test_with_vars_set_unset() {
let not_my_var_not_set = env::var("NOT_MY_VAR");
assert!(not_my_var_not_set.is_err(), "`NOT_MY_VAR` must not be set.");
crate::with_vars(
vec![
("NOT_MY_VAR", Some("it is set")),
("NOT_MY_VAR", None::<&str>),
],
|| {
let not_my_var_not_set_new = env::var("NOT_MY_VAR");
assert!(
not_my_var_not_set_new.is_err(),
"`NOT_MY_VAR` must not be set."
);
},
);
let not_my_var_not_set_after = env::var("NOT_MY_VAR");
assert!(
not_my_var_not_set_after.is_err(),
"`NOT_MY_VAR` must not be set."
);
}
}