|
| 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