]> git.proxmox.com Git - rustc.git/blob - src/libstd/tests/env.rs
New upstream version 1.26.0+dfsg1
[rustc.git] / src / libstd / tests / env.rs
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.
4 //
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.
10
11 extern crate rand;
12
13 use std::env::*;
14 use std::iter::repeat;
15 use std::ffi::{OsString, OsStr};
16
17 use rand::Rng;
18
19 fn make_rand_name() -> OsString {
20 let mut rng = rand::thread_rng();
21 let n = format!("TEST{}", rng.gen_ascii_chars().take(10)
22 .collect::<String>());
23 let n = OsString::from(n);
24 assert!(var_os(&n).is_none());
25 n
26 }
27
28 fn eq(a: Option<OsString>, b: Option<&str>) {
29 assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::new).map(|s| &*s));
30 }
31
32 #[test]
33 fn test_set_var() {
34 let n = make_rand_name();
35 set_var(&n, "VALUE");
36 eq(var_os(&n), Some("VALUE"));
37 }
38
39 #[test]
40 fn test_remove_var() {
41 let n = make_rand_name();
42 set_var(&n, "VALUE");
43 remove_var(&n);
44 eq(var_os(&n), None);
45 }
46
47 #[test]
48 fn test_set_var_overwrite() {
49 let n = make_rand_name();
50 set_var(&n, "1");
51 set_var(&n, "2");
52 eq(var_os(&n), Some("2"));
53 set_var(&n, "");
54 eq(var_os(&n), Some(""));
55 }
56
57 #[test]
58 #[cfg_attr(target_os = "emscripten", ignore)]
59 fn test_var_big() {
60 let mut s = "".to_string();
61 let mut i = 0;
62 while i < 100 {
63 s.push_str("aaaaaaaaaa");
64 i += 1;
65 }
66 let n = make_rand_name();
67 set_var(&n, &s);
68 eq(var_os(&n), Some(&s));
69 }
70
71 #[test]
72 #[cfg_attr(target_os = "emscripten", ignore)]
73 fn test_env_set_get_huge() {
74 let n = make_rand_name();
75 let s = repeat("x").take(10000).collect::<String>();
76 set_var(&n, &s);
77 eq(var_os(&n), Some(&s));
78 remove_var(&n);
79 eq(var_os(&n), None);
80 }
81
82 #[test]
83 fn test_env_set_var() {
84 let n = make_rand_name();
85
86 let mut e = vars_os();
87 set_var(&n, "VALUE");
88 assert!(!e.any(|(k, v)| {
89 &*k == &*n && &*v == "VALUE"
90 }));
91
92 assert!(vars_os().any(|(k, v)| {
93 &*k == &*n && &*v == "VALUE"
94 }));
95 }