make unsetting env vars print as executable command
This commit is contained in:
parent
f2b139f23d
commit
396cbe6639
@ -543,8 +543,8 @@ fn debug_print() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let mut command_with_removed_env = Command::new("boring-name");
|
let mut command_with_removed_env = Command::new("boring-name");
|
||||||
command_with_removed_env.env_remove("BAR");
|
command_with_removed_env.env_remove("FOO").env_remove("BAR");
|
||||||
assert_eq!(format!("{command_with_removed_env:?}"), r#"unset(BAR) "boring-name""#);
|
assert_eq!(format!("{command_with_removed_env:?}"), r#"unset BAR FOO && "boring-name""#);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{command_with_removed_env:#?}"),
|
format!("{command_with_removed_env:#?}"),
|
||||||
format!(
|
format!(
|
||||||
@ -557,6 +557,7 @@ fn debug_print() {
|
|||||||
clear: false,
|
clear: false,
|
||||||
vars: {{
|
vars: {{
|
||||||
"BAR": None,
|
"BAR": None,
|
||||||
|
"FOO": None,
|
||||||
}},
|
}},
|
||||||
}},
|
}},
|
||||||
{PIDFD}}}"#
|
{PIDFD}}}"#
|
||||||
|
@ -558,11 +558,25 @@ impl fmt::Debug for Command {
|
|||||||
if let Some(ref cwd) = self.cwd {
|
if let Some(ref cwd) = self.cwd {
|
||||||
write!(f, "cd {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() {
|
for (key, value_opt) in self.get_envs() {
|
||||||
if let Some(value) = value_opt {
|
if let Some(value) = value_opt {
|
||||||
write!(f, "{}={value:?} ", key.to_string_lossy())?;
|
write!(f, "{}={value:?} ", key.to_string_lossy())?;
|
||||||
} else {
|
|
||||||
write!(f, "unset({}) ", key.to_string_lossy())?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.program != self.args[0] {
|
if self.program != self.args[0] {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user