]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/process-envs.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / test / run-pass / process-envs.rs
1 // Copyright 2014, 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 // ignore-cloudabi no processes
12 // ignore-emscripten no processes
13
14 use std::process::Command;
15 use std::env;
16 use std::collections::HashMap;
17
18 #[cfg(all(unix, not(target_os="android")))]
19 pub fn env_cmd() -> Command {
20 Command::new("env")
21 }
22 #[cfg(target_os="android")]
23 pub fn env_cmd() -> Command {
24 let mut cmd = Command::new("/system/bin/sh");
25 cmd.arg("-c").arg("set");
26 cmd
27 }
28
29 #[cfg(windows)]
30 pub fn env_cmd() -> Command {
31 let mut cmd = Command::new("cmd");
32 cmd.arg("/c").arg("set");
33 cmd
34 }
35
36 fn main() {
37 // save original environment
38 let old_env = env::var_os("RUN_TEST_NEW_ENV");
39
40 env::set_var("RUN_TEST_NEW_ENV", "123");
41
42 // create filtered environment vector
43 let filtered_env : HashMap<String, String> =
44 env::vars().filter(|&(ref k, _)| k == "PATH").collect();
45
46 let mut cmd = env_cmd();
47 cmd.env_clear();
48 cmd.envs(&filtered_env);
49
50 // restore original environment
51 match old_env {
52 None => env::remove_var("RUN_TEST_NEW_ENV"),
53 Some(val) => env::set_var("RUN_TEST_NEW_ENV", &val)
54 }
55
56 let result = cmd.output().unwrap();
57 let output = String::from_utf8_lossy(&result.stdout);
58
59 assert!(!output.contains("RUN_TEST_NEW_ENV"),
60 "found RUN_TEST_NEW_ENV inside of:\n\n{}", output);
61 }