1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use std::ffi::{OsString, OsStr};
18 fn make_rand_name() -> OsString {
19 let mut rng = rand::thread_rng();
20 let n = format!("TEST{}", rng.gen_ascii_chars().take(10)
21 .collect::<String>());
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)| {
88 &*k == &*n && &*v == "VALUE"
91 assert!(vars_os().any(|(k, v)| {
92 &*k == &*n && &*v == "VALUE"