2 use std::ffi::{OsStr, OsString};
4 use rand::distributions::{Alphanumeric, DistString};
6 /// Copied from `std::test_helpers::test_rng`, since these tests rely on the
7 /// seed not being the same for every RNG invocation too.
9 pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
10 use core::hash::{BuildHasher, Hash, Hasher};
11 let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
12 core::panic::Location::caller().hash(&mut hasher);
13 let hc64 = hasher.finish();
14 let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
15 let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
16 rand::SeedableRng::from_seed(seed)
20 fn make_rand_name() -> OsString {
21 let n = format!("TEST{}", Alphanumeric.sample_string(&mut test_rng(), 10));
22 let n = OsString::from(n);
23 assert!(var_os(&n).is_none());
27 fn eq(a: Option<OsString>, b: Option<&str>) {
28 assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::new).map(|s| &*s));
33 let n = make_rand_name();
35 eq(var_os(&n), Some("VALUE"));
39 fn test_remove_var() {
40 let n = make_rand_name();
47 fn test_set_var_overwrite() {
48 let n = make_rand_name();
51 eq(var_os(&n), Some("2"));
53 eq(var_os(&n), Some(""));
57 #[cfg_attr(target_os = "emscripten", ignore)]
59 let mut s = "".to_string();
62 s.push_str("aaaaaaaaaa");
65 let n = make_rand_name();
67 eq(var_os(&n), Some(&s));
71 #[cfg_attr(target_os = "emscripten", ignore)]
72 fn test_env_set_get_huge() {
73 let n = make_rand_name();
74 let s = "x".repeat(10000);
76 eq(var_os(&n), Some(&s));
82 fn test_env_set_var() {
83 let n = make_rand_name();
85 let mut e = vars_os();
87 assert!(!e.any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
89 assert!(vars_os().any(|(k, v)| { &*k == &*n && &*v == "VALUE" }));
93 #[cfg_attr(not(any(unix, windows)), ignore, allow(unused))]
96 use std::path::PathBuf;
98 fn var_to_os_string(var: Result<String, VarError>) -> Option<OsString> {
100 Ok(var) => Some(OsString::from(var)),
101 Err(VarError::NotUnicode(var)) => Some(var),
108 let oldhome = var_to_os_string(var("HOME"));
110 set_var("HOME", "/home/MountainView");
111 assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
114 if cfg!(target_os = "android") {
115 assert!(home_dir().is_none());
117 // When HOME is not set, some platforms return `None`,
118 // but others return `Some` with a default.
119 // Just check that it is not "/home/MountainView".
120 assert_ne!(home_dir(), Some(PathBuf::from("/home/MountainView")));
123 if let Some(oldhome) = oldhome { set_var("HOME", oldhome); }
124 } else if #[cfg(windows)] {
125 let oldhome = var_to_os_string(var("HOME"));
126 let olduserprofile = var_to_os_string(var("USERPROFILE"));
129 remove_var("USERPROFILE");
131 assert!(home_dir().is_some());
133 set_var("HOME", "/home/MountainView");
134 assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
138 set_var("USERPROFILE", "/home/MountainView");
139 assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
141 set_var("HOME", "/home/MountainView");
142 set_var("USERPROFILE", "/home/PaloAlto");
143 assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
146 remove_var("USERPROFILE");
148 if let Some(oldhome) = oldhome { set_var("HOME", oldhome); }
149 if let Some(olduserprofile) = olduserprofile { set_var("USERPROFILE", olduserprofile); }