]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/env.rs
Move global vars changing tests into run-pass
[rust.git] / src / test / run-pass / 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 // compile-flags: --test
12
13 #![feature(rand, std_panic)]
14
15 use std::env::*;
16 use std::__rand as rand;
17 use std::__rand::Rng;
18 use std::iter::repeat;
19 use std::ffi::{OsString, OsStr};
20
21
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());
28     n
29 }
30
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));
33 }
34
35 #[test]
36 fn test_set_var() {
37     let n = make_rand_name();
38     set_var(&n, "VALUE");
39     eq(var_os(&n), Some("VALUE"));
40 }
41
42 #[test]
43 fn test_remove_var() {
44     let n = make_rand_name();
45     set_var(&n, "VALUE");
46     remove_var(&n);
47     eq(var_os(&n), None);
48 }
49
50 #[test]
51 fn test_set_var_overwrite() {
52     let n = make_rand_name();
53     set_var(&n, "1");
54     set_var(&n, "2");
55     eq(var_os(&n), Some("2"));
56     set_var(&n, "");
57     eq(var_os(&n), Some(""));
58 }
59
60 #[test]
61 #[cfg_attr(target_os = "emscripten", ignore)]
62 fn test_var_big() {
63     let mut s = "".to_string();
64     let mut i = 0;
65     while i < 100 {
66         s.push_str("aaaaaaaaaa");
67         i += 1;
68     }
69     let n = make_rand_name();
70     set_var(&n, &s);
71     eq(var_os(&n), Some(&s));
72 }
73
74 #[test]
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>();
79     set_var(&n, &s);
80     eq(var_os(&n), Some(&s));
81     remove_var(&n);
82     eq(var_os(&n), None);
83 }
84
85 #[test]
86 fn test_env_set_var() {
87     let n = make_rand_name();
88
89     let mut e = vars_os();
90     set_var(&n, "VALUE");
91     assert!(!e.any(|(k, v)| {
92         &*k == &*n && &*v == "VALUE"
93     }));
94
95     assert!(vars_os().any(|(k, v)| {
96         &*k == &*n && &*v == "VALUE"
97     }));
98 }