Skip to content

Commit 396cbe6

Browse files
committed
make unsetting env vars print as executable command
1 parent f2b139f commit 396cbe6

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

library/std/src/process/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ fn debug_print() {
543543
);
544544

545545
let mut command_with_removed_env = Command::new("boring-name");
546-
command_with_removed_env.env_remove("BAR");
547-
assert_eq!(format!("{command_with_removed_env:?}"), r#"unset(BAR) "boring-name""#);
546+
command_with_removed_env.env_remove("FOO").env_remove("BAR");
547+
assert_eq!(format!("{command_with_removed_env:?}"), r#"unset BAR FOO && "boring-name""#);
548548
assert_eq!(
549549
format!("{command_with_removed_env:#?}"),
550550
format!(
@@ -557,6 +557,7 @@ fn debug_print() {
557557
clear: false,
558558
vars: {{
559559
"BAR": None,
560+
"FOO": None,
560561
}},
561562
}},
562563
{PIDFD}}}"#

library/std/src/sys/unix/process/process_common.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,25 @@ impl fmt::Debug for Command {
558558
if let Some(ref cwd) = self.cwd {
559559
write!(f, "cd {cwd:?} && ")?;
560560
}
561+
// Removed env vars need a separate command.
562+
// We use a single `unset` command for all of them.
563+
let mut any_removed = false;
564+
for (key, value_opt) in self.get_envs() {
565+
if value_opt.is_none() {
566+
if !any_removed {
567+
write!(f, "unset ")?;
568+
any_removed = true;
569+
}
570+
write!(f, "{} ", key.to_string_lossy())?;
571+
}
572+
}
573+
if any_removed {
574+
write!(f, "&& ")?;
575+
}
576+
// Altered env vars can just be added in front of the program.
561577
for (key, value_opt) in self.get_envs() {
562578
if let Some(value) = value_opt {
563579
write!(f, "{}={value:?} ", key.to_string_lossy())?;
564-
} else {
565-
write!(f, "unset({}) ", key.to_string_lossy())?;
566580
}
567581
}
568582
if self.program != self.args[0] {

0 commit comments

Comments
 (0)