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.
11 // compile-flags: --test
13 #![feature(rand, std_panic)]
16 use std::__rand as rand;
18 use std::iter::repeat;
19 use std::ffi::{OsString, OsStr};
22 fn make_rand_name() -> OsString {
23 let mut rng = rand::thread_rng();
24 let n = format!("TEST{}", rng.gen_ascii_chars().take(10)
25 .collect::<String>());
26 let n = OsString::from(n);
27 assert!(var_os(&n).is_none());
31 fn eq(a: Option<OsString>, b: Option<&str>) {
32 assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::new).map(|s| &*s));
37 let n = make_rand_name();
39 eq(var_os(&n), Some("VALUE"));
43 fn test_remove_var() {
44 let n = make_rand_name();
51 fn test_set_var_overwrite() {
52 let n = make_rand_name();
55 eq(var_os(&n), Some("2"));
57 eq(var_os(&n), Some(""));
61 #[cfg_attr(target_os = "emscripten", ignore)]
63 let mut s = "".to_string();
66 s.push_str("aaaaaaaaaa");
69 let n = make_rand_name();
71 eq(var_os(&n), Some(&s));
75 #[cfg_attr(target_os = "emscripten", ignore)]
76 fn test_env_set_get_huge() {
77 let n = make_rand_name();
78 let s = repeat("x").take(10000).collect::<String>();
80 eq(var_os(&n), Some(&s));
86 fn test_env_set_var() {
87 let n = make_rand_name();
89 let mut e = vars_os();
91 assert!(!e.any(|(k, v)| {
92 &*k == &*n && &*v == "VALUE"
95 assert!(vars_os().any(|(k, v)| {
96 &*k == &*n && &*v == "VALUE"