2017-06-24 17:23:39 +02:00
|
|
|
use std::env::*;
|
2019-11-27 19:29:00 +01:00
|
|
|
use std::ffi::{OsStr, OsString};
|
2017-06-24 17:23:39 +02:00
|
|
|
|
2018-08-05 00:24:39 +02:00
|
|
|
use rand::distributions::Alphanumeric;
|
2019-11-27 19:29:00 +01:00
|
|
|
use rand::{thread_rng, Rng};
|
2017-06-24 17:23:39 +02:00
|
|
|
|
|
|
|
fn make_rand_name() -> OsString {
|
2019-08-22 12:28:03 +02:00
|
|
|
let rng = thread_rng();
|
2019-11-27 19:29:00 +01:00
|
|
|
let n = format!("TEST{}", rng.sample_iter(&Alphanumeric).take(10).collect::<String>());
|
2017-06-24 17:23:39 +02:00
|
|
|
let n = OsString::from(n);
|
|
|
|
assert!(var_os(&n).is_none());
|
|
|
|
n
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eq(a: Option<OsString>, b: Option<&str>) {
|
|
|
|
assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::new).map(|s| &*s));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_var() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
set_var(&n, "VALUE");
|
|
|
|
eq(var_os(&n), Some("VALUE"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove_var() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
set_var(&n, "VALUE");
|
|
|
|
remove_var(&n);
|
|
|
|
eq(var_os(&n), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set_var_overwrite() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
set_var(&n, "1");
|
|
|
|
set_var(&n, "2");
|
|
|
|
eq(var_os(&n), Some("2"));
|
|
|
|
set_var(&n, "");
|
|
|
|
eq(var_os(&n), Some(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg_attr(target_os = "emscripten", ignore)]
|
|
|
|
fn test_var_big() {
|
|
|
|
let mut s = "".to_string();
|
|
|
|
let mut i = 0;
|
|
|
|
while i < 100 {
|
|
|
|
s.push_str("aaaaaaaaaa");
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
let n = make_rand_name();
|
|
|
|
set_var(&n, &s);
|
|
|
|
eq(var_os(&n), Some(&s));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg_attr(target_os = "emscripten", ignore)]
|
|
|
|
fn test_env_set_get_huge() {
|
|
|
|
let n = make_rand_name();
|
2018-04-01 06:48:15 +02:00
|
|
|
let s = "x".repeat(10000);
|
2017-06-24 17:23:39 +02:00
|
|
|
set_var(&n, &s);
|
|
|
|
eq(var_os(&n), Some(&s));
|
|
|
|
remove_var(&n);
|
|
|
|
eq(var_os(&n), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_env_set_var() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
|
|
|
|
let mut e = vars_os();
|
|
|
|
set_var(&n, "VALUE");
|
2019-11-27 19:29:00 +01:00
|
|
|
assert!(!e.any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
|
2017-06-24 17:23:39 +02:00
|
|
|
|
2019-11-27 19:29:00 +01:00
|
|
|
assert!(vars_os().any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
|
2017-06-24 17:23:39 +02:00
|
|
|
}
|