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