diff options
| author | Diggory Blake <diggsey@googlemail.com> | 2017-12-17 15:21:47 +0000 |
|---|---|---|
| committer | Diggory Blake <diggsey@googlemail.com> | 2017-12-24 14:24:31 +0000 |
| commit | ccc91d7b4873a50678b3f65c895290915c54f6f5 (patch) | |
| tree | 5884d3c794a88178b6853df2dde3bcb3ee67de1b /src/libstd/process.rs | |
| parent | b058dc0107b734b0a1a664ca0209366bb59eb3e9 (diff) | |
| download | rust-ccc91d7b4873a50678b3f65c895290915c54f6f5.tar.gz rust-ccc91d7b4873a50678b3f65c895290915c54f6f5.zip | |
Capture environment at spawn
Diffstat (limited to 'src/libstd/process.rs')
| -rw-r--r-- | src/libstd/process.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 2335695ae42..1f6ddde0027 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -513,7 +513,7 @@ impl Command { pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command where K: AsRef<OsStr>, V: AsRef<OsStr> { - self.inner.env(key.as_ref(), val.as_ref()); + self.inner.env_mut().set(key.as_ref(), val.as_ref()); self } @@ -546,7 +546,7 @@ impl Command { where I: IntoIterator<Item=(K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr> { for (ref key, ref val) in vars { - self.inner.env(key.as_ref(), val.as_ref()); + self.inner.env_mut().set(key.as_ref(), val.as_ref()); } self } @@ -567,7 +567,7 @@ impl Command { /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command { - self.inner.env_remove(key.as_ref()); + self.inner.env_mut().remove(key.as_ref()); self } @@ -587,7 +587,7 @@ impl Command { /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn env_clear(&mut self) -> &mut Command { - self.inner.env_clear(); + self.inner.env_mut().clear(); self } @@ -1715,6 +1715,27 @@ mod tests { "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); } + #[test] + fn test_capture_env_at_spawn() { + use env; + + let mut cmd = env_cmd(); + cmd.env("RUN_TEST_NEW_ENV1", "123"); + + // This variable will not be present if the environment has already + // been captured above. + env::set_var("RUN_TEST_NEW_ENV2", "456"); + let result = cmd.output().unwrap(); + env::remove_var("RUN_TEST_NEW_ENV2"); + + let output = String::from_utf8_lossy(&result.stdout).to_string(); + + assert!(output.contains("RUN_TEST_NEW_ENV1=123"), + "didn't find RUN_TEST_NEW_ENV1 inside of:\n\n{}", output); + assert!(output.contains("RUN_TEST_NEW_ENV2=456"), + "didn't find RUN_TEST_NEW_ENV2 inside of:\n\n{}", output); + } + // Regression tests for #30858. #[test] fn test_interior_nul_in_progname_is_error() { |
