Skip to content

Commit ef69388

Browse files
committed
Fix a race condition between remove_from_env and other io::process tests.
1 parent 9a2286d commit ef69388

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

src/libstd/io/process.rs

-26
Original file line numberDiff line numberDiff line change
@@ -1024,32 +1024,6 @@ mod tests {
10241024
"didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
10251025
}
10261026

1027-
#[test]
1028-
fn test_remove_from_env() {
1029-
use os;
1030-
1031-
// save original environment
1032-
let old_env = os::getenv("RUN_TEST_NEW_ENV");
1033-
1034-
os::setenv("RUN_TEST_NEW_ENV", "123");
1035-
let prog = env_cmd().env_remove("RUN_TEST_NEW_ENV").spawn().unwrap();
1036-
let result = prog.wait_with_output().unwrap();
1037-
let output = str::from_utf8_lossy(result.output.as_slice()).into_string();
1038-
1039-
// restore original environment
1040-
match old_env {
1041-
None => {
1042-
os::unsetenv("RUN_TEST_NEW_ENV");
1043-
}
1044-
Some(val) => {
1045-
os::setenv("RUN_TEST_NEW_ENV", val.as_slice());
1046-
}
1047-
}
1048-
1049-
assert!(!output.as_slice().contains("RUN_TEST_NEW_ENV"),
1050-
"found RUN_TEST_NEW_ENV inside of:\n\n{}", output);
1051-
}
1052-
10531027
#[cfg(unix)]
10541028
pub fn sleeper() -> Process {
10551029
Command::new("sleep").arg("1000").spawn().unwrap()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2014 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+
use std::io::Command;
12+
use std::os;
13+
14+
#[cfg(all(unix, not(target_os="android")))]
15+
pub fn env_cmd() -> Command {
16+
Command::new("env")
17+
}
18+
#[cfg(target_os="android")]
19+
pub fn env_cmd() -> Command {
20+
let mut cmd = Command::new("/system/bin/sh");
21+
cmd.arg("-c").arg("set");
22+
cmd
23+
}
24+
25+
#[cfg(windows)]
26+
pub fn env_cmd() -> Command {
27+
let mut cmd = Command::new("cmd");
28+
cmd.arg("/c").arg("set");
29+
cmd
30+
}
31+
32+
fn main() {
33+
// save original environment
34+
let old_env = os::getenv("RUN_TEST_NEW_ENV");
35+
36+
os::setenv("RUN_TEST_NEW_ENV", "123");
37+
38+
let mut cmd = env_cmd();
39+
cmd.env_remove("RUN_TEST_NEW_ENV");
40+
41+
// restore original environment
42+
match old_env {
43+
None => os::unsetenv("RUN_TEST_NEW_ENV"),
44+
Some(val) => os::setenv("RUN_TEST_NEW_ENV", val.as_slice())
45+
}
46+
47+
let prog = cmd.spawn().unwrap();
48+
let result = prog.wait_with_output().unwrap();
49+
let output = String::from_utf8_lossy(result.output.as_slice());
50+
51+
assert!(!output.as_slice().contains("RUN_TEST_NEW_ENV"),
52+
"found RUN_TEST_NEW_ENV inside of:\n\n{}", output);
53+
}

0 commit comments

Comments
 (0)