diff options
| author | Ralf Jung <post@ralfj.de> | 2023-08-03 09:22:54 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2023-08-24 08:08:46 +0200 |
| commit | 396cbe66397ba77184e4ed896f4ba17f623c00f7 (patch) | |
| tree | a4120467a7af3381780a0bfa3b9de82427dac4ed /library/std/src | |
| parent | f2b139f23d574ca1d2c764b581a3b993e9af1570 (diff) | |
| download | rust-396cbe66397ba77184e4ed896f4ba17f623c00f7.tar.gz rust-396cbe66397ba77184e4ed896f4ba17f623c00f7.zip | |
make unsetting env vars print as executable command
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/process/tests.rs | 5 | ||||
| -rw-r--r-- | library/std/src/sys/unix/process/process_common.rs | 18 |
2 files changed, 19 insertions, 4 deletions
diff --git a/library/std/src/process/tests.rs b/library/std/src/process/tests.rs index 2c331eb0104..0d5321c2eea 100644 --- a/library/std/src/process/tests.rs +++ b/library/std/src/process/tests.rs @@ -543,8 +543,8 @@ fn debug_print() { ); let mut command_with_removed_env = Command::new("boring-name"); - command_with_removed_env.env_remove("BAR"); - assert_eq!(format!("{command_with_removed_env:?}"), r#"unset(BAR) "boring-name""#); + command_with_removed_env.env_remove("FOO").env_remove("BAR"); + assert_eq!(format!("{command_with_removed_env:?}"), r#"unset BAR FOO && "boring-name""#); assert_eq!( format!("{command_with_removed_env:#?}"), format!( @@ -557,6 +557,7 @@ fn debug_print() { clear: false, vars: {{ "BAR": None, + "FOO": None, }}, }}, {PIDFD}}}"# diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 9362fc7f205..3a02a6c20d9 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -558,11 +558,25 @@ impl fmt::Debug for Command { if let Some(ref cwd) = self.cwd { write!(f, "cd {cwd:?} && ")?; } + // Removed env vars need a separate command. + // We use a single `unset` command for all of them. + let mut any_removed = false; + for (key, value_opt) in self.get_envs() { + if value_opt.is_none() { + if !any_removed { + write!(f, "unset ")?; + any_removed = true; + } + write!(f, "{} ", key.to_string_lossy())?; + } + } + if any_removed { + write!(f, "&& ")?; + } + // Altered env vars can just be added in front of the program. for (key, value_opt) in self.get_envs() { if let Some(value) = value_opt { write!(f, "{}={value:?} ", key.to_string_lossy())?; - } else { - write!(f, "unset({}) ", key.to_string_lossy())?; } } if self.program != self.args[0] { |
