1
Fork 0

More verbose Debug implementation of std::process:Command

based on commit: ccc019aabf from https://github.com/zackmdavis

close https://github.com/rust-lang/rust/issues/42200

Add env variables and cwd to the shell-like debug output.

Also use the alternate syntax to display a more verbose display, while not showing internal fields and hiding fields when they have their default value.
This commit is contained in:
kraktus 2022-05-19 15:50:27 +02:00
parent 35a99eef32
commit eb63dea57f
5 changed files with 173 additions and 29 deletions

View file

@ -417,6 +417,100 @@ fn env_empty() {
assert!(p.is_ok());
}
#[test]
#[cfg(not(windows))]
#[cfg_attr(any(target_os = "emscripten", target_env = "sgx"), ignore)]
fn main() {
const PIDFD: &'static str =
if cfg!(target_os = "linux") { " create_pidfd: false,\n" } else { "" };
let mut command = Command::new("some-boring-name");
assert_eq!(format!("{command:?}"), format!(r#""some-boring-name""#));
assert_eq!(
format!("{command:#?}"),
format!(
r#"Command {{
program: "some-boring-name",
args: [
"some-boring-name",
],
{PIDFD}}}"#
)
);
command.args(&["1", "2", "3"]);
assert_eq!(format!("{command:?}"), format!(r#""some-boring-name" "1" "2" "3""#));
assert_eq!(
format!("{command:#?}"),
format!(
r#"Command {{
program: "some-boring-name",
args: [
"some-boring-name",
"1",
"2",
"3",
],
{PIDFD}}}"#
)
);
crate::os::unix::process::CommandExt::arg0(&mut command, "exciting-name");
assert_eq!(
format!("{command:?}"),
format!(r#"["some-boring-name"] "exciting-name" "1" "2" "3""#)
);
assert_eq!(
format!("{command:#?}"),
format!(
r#"Command {{
program: "some-boring-name",
args: [
"exciting-name",
"1",
"2",
"3",
],
{PIDFD}}}"#
)
);
let mut command_with_env_and_cwd = Command::new("boring-name");
command_with_env_and_cwd.current_dir("/some/path").env("FOO", "bar");
assert_eq!(
format!("{command_with_env_and_cwd:?}"),
r#"cd "/some/path" && FOO="bar" "boring-name""#
);
assert_eq!(
format!("{command_with_env_and_cwd:#?}"),
format!(
r#"Command {{
program: "boring-name",
args: [
"boring-name",
],
env: CommandEnv {{
clear: false,
vars: {{
"FOO": Some(
"bar",
),
}},
}},
cwd: Some(
"/some/path",
),
{PIDFD}}}"#
)
);
}
// See issue #91991
#[test]
#[cfg(windows)]