]> git.lizzy.rs Git - rust.git/blob - tests/ui/process/process-remove-from-env.rs
Rollup merge of #106670 - albertlarsan68:check-docs-in-pr-ci, r=Mark-Simulacrum
[rust.git] / tests / ui / process / process-remove-from-env.rs
1 // run-pass
2 // ignore-emscripten no processes
3 // ignore-sgx no processes
4 // ignore-vxworks no 'env'
5 // ignore-fuchsia no 'env'
6
7 use std::process::Command;
8 use std::env;
9
10 #[cfg(all(unix, not(target_os="android")))]
11 pub fn env_cmd() -> Command {
12     Command::new("env")
13 }
14 #[cfg(target_os="android")]
15 pub fn env_cmd() -> Command {
16     let mut cmd = Command::new("/system/bin/sh");
17     cmd.arg("-c").arg("set");
18     cmd
19 }
20
21 #[cfg(windows)]
22 pub fn env_cmd() -> Command {
23     let mut cmd = Command::new("cmd");
24     cmd.arg("/c").arg("set");
25     cmd
26 }
27
28 fn main() {
29     // save original environment
30     let old_env = env::var_os("RUN_TEST_NEW_ENV");
31
32     env::set_var("RUN_TEST_NEW_ENV", "123");
33
34     let mut cmd = env_cmd();
35     cmd.env_remove("RUN_TEST_NEW_ENV");
36
37     // restore original environment
38     match old_env {
39         None => env::remove_var("RUN_TEST_NEW_ENV"),
40         Some(val) => env::set_var("RUN_TEST_NEW_ENV", &val)
41     }
42
43     let result = cmd.output().unwrap();
44     let output = String::from_utf8_lossy(&result.stdout);
45
46     assert!(!output.contains("RUN_TEST_NEW_ENV"),
47             "found RUN_TEST_NEW_ENV inside of:\n\n{}", output);
48 }