Skip to content

Commit 4bae0d8

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#42884 - stepancheg:set-env-run-pass, r=alexcrichton
Move global vars changing tests into run-pass Should fix race rust-lang#42795
2 parents 5682494 + 45af6ee commit 4bae0d8

File tree

2 files changed

+99
-81
lines changed

2 files changed

+99
-81
lines changed

src/libstd/env.rs

+1-81
Original file line numberDiff line numberDiff line change
@@ -949,63 +949,9 @@ mod arch {
949949
mod tests {
950950
use super::*;
951951

952-
use iter::repeat;
953-
use rand::{self, Rng};
954-
use ffi::{OsString, OsStr};
952+
use ffi::OsStr;
955953
use path::{Path, PathBuf};
956954

957-
fn make_rand_name() -> OsString {
958-
let mut rng = rand::thread_rng();
959-
let n = format!("TEST{}", rng.gen_ascii_chars().take(10)
960-
.collect::<String>());
961-
let n = OsString::from(n);
962-
assert!(var_os(&n).is_none());
963-
n
964-
}
965-
966-
fn eq(a: Option<OsString>, b: Option<&str>) {
967-
assert_eq!(a.as_ref().map(|s| &**s), b.map(OsStr::new).map(|s| &*s));
968-
}
969-
970-
#[test]
971-
fn test_set_var() {
972-
let n = make_rand_name();
973-
set_var(&n, "VALUE");
974-
eq(var_os(&n), Some("VALUE"));
975-
}
976-
977-
#[test]
978-
fn test_remove_var() {
979-
let n = make_rand_name();
980-
set_var(&n, "VALUE");
981-
remove_var(&n);
982-
eq(var_os(&n), None);
983-
}
984-
985-
#[test]
986-
fn test_set_var_overwrite() {
987-
let n = make_rand_name();
988-
set_var(&n, "1");
989-
set_var(&n, "2");
990-
eq(var_os(&n), Some("2"));
991-
set_var(&n, "");
992-
eq(var_os(&n), Some(""));
993-
}
994-
995-
#[test]
996-
#[cfg_attr(target_os = "emscripten", ignore)]
997-
fn test_var_big() {
998-
let mut s = "".to_string();
999-
let mut i = 0;
1000-
while i < 100 {
1001-
s.push_str("aaaaaaaaaa");
1002-
i += 1;
1003-
}
1004-
let n = make_rand_name();
1005-
set_var(&n, &s);
1006-
eq(var_os(&n), Some(&s));
1007-
}
1008-
1009955
#[test]
1010956
#[cfg_attr(target_os = "emscripten", ignore)]
1011957
fn test_self_exe_path() {
@@ -1017,32 +963,6 @@ mod tests {
1017963
assert!(path.is_absolute());
1018964
}
1019965

1020-
#[test]
1021-
#[cfg_attr(target_os = "emscripten", ignore)]
1022-
fn test_env_set_get_huge() {
1023-
let n = make_rand_name();
1024-
let s = repeat("x").take(10000).collect::<String>();
1025-
set_var(&n, &s);
1026-
eq(var_os(&n), Some(&s));
1027-
remove_var(&n);
1028-
eq(var_os(&n), None);
1029-
}
1030-
1031-
#[test]
1032-
fn test_env_set_var() {
1033-
let n = make_rand_name();
1034-
1035-
let mut e = vars_os();
1036-
set_var(&n, "VALUE");
1037-
assert!(!e.any(|(k, v)| {
1038-
&*k == &*n && &*v == "VALUE"
1039-
}));
1040-
1041-
assert!(vars_os().any(|(k, v)| {
1042-
&*k == &*n && &*v == "VALUE"
1043-
}));
1044-
}
1045-
1046966
#[test]
1047967
fn test() {
1048968
assert!((!Path::new("test-path").is_absolute()));

src/test/run-pass/env.rs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

0 commit comments

Comments
 (0)