From 529c98085a638fabed51ac292f44e6e5fa7b899b Mon Sep 17 00:00:00 2001 From: nibon7 Date: Fri, 19 Aug 2022 00:58:51 +0800 Subject: [PATCH] Return error when `kill` didn't terminate successfully (#6354) * Return error when `kill` didn't terminate successfully Signed-off-by: nibon7 * add test Signed-off-by: nibon7 Signed-off-by: nibon7 --- crates/nu-command/src/platform/kill.rs | 31 +++++++++++++++++-- crates/nu-command/tests/commands/mod.rs | 1 + .../tests/commands/platform/kill.rs | 9 ++++++ .../nu-command/tests/commands/platform/mod.rs | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 crates/nu-command/tests/commands/platform/kill.rs create mode 100644 crates/nu-command/tests/commands/platform/mod.rs diff --git a/crates/nu-command/src/platform/kill.rs b/crates/nu-command/src/platform/kill.rs index 811ce0408e..14e52d1f55 100644 --- a/crates/nu-command/src/platform/kill.rs +++ b/crates/nu-command/src/platform/kill.rs @@ -132,10 +132,37 @@ impl Command for Kill { .stderr(Stdio::null()); } - let output = cmd.output().expect("failed to execute shell command"); + let output = cmd.output().map_err(|e| { + ShellError::GenericError( + "failed to execute shell command".into(), + e.to_string(), + Some(call.head), + None, + Vec::new(), + ) + })?; + + if !quiet && !output.status.success() { + return Err(ShellError::GenericError( + "process didn't terminate successfully".into(), + String::from_utf8(output.stderr).unwrap_or_default(), + Some(call.head), + None, + Vec::new(), + )); + } + let val = String::from( String::from_utf8(output.stdout) - .expect("failed to convert output to string") + .map_err(|e| { + ShellError::GenericError( + "failed to convert output to string".into(), + e.to_string(), + Some(call.head), + None, + Vec::new(), + ) + })? .trim_end(), ); if val.is_empty() { diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index de1dd89d00..4f35e1074c 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -48,6 +48,7 @@ mod open; mod p; mod parse; mod path; +mod platform; mod prepend; mod print; #[cfg(feature = "database")] diff --git a/crates/nu-command/tests/commands/platform/kill.rs b/crates/nu-command/tests/commands/platform/kill.rs new file mode 100644 index 0000000000..d74ffd372a --- /dev/null +++ b/crates/nu-command/tests/commands/platform/kill.rs @@ -0,0 +1,9 @@ +use nu_test_support::nu; + +#[test] +fn test_kill_invalid_pid() { + let pid = i32::MAX; + let actual = nu!(format!("kill {}", pid)); + + assert!(actual.err.contains("process didn't terminate successfully")); +} diff --git a/crates/nu-command/tests/commands/platform/mod.rs b/crates/nu-command/tests/commands/platform/mod.rs new file mode 100644 index 0000000000..f9d2ffbcc3 --- /dev/null +++ b/crates/nu-command/tests/commands/platform/mod.rs @@ -0,0 +1 @@ +mod kill;