Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kill: accept all cases for signal names #6229

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/uu/kill/src/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ fn table() {

fn print_signal(signal_name_or_value: &str) -> UResult<()> {
for (value, &signal) in ALL_SIGNALS.iter().enumerate() {
if signal == signal_name_or_value || (format!("SIG{signal}")) == signal_name_or_value {
if signal.eq_ignore_ascii_case(signal_name_or_value)
|| format!("SIG{signal}").eq_ignore_ascii_case(signal_name_or_value)
{
println!("{value}");
return Ok(());
} else if signal_name_or_value == value.to_string() {
Expand Down Expand Up @@ -188,7 +190,8 @@ fn list(signals: &Vec<String>) {
}

fn parse_signal_value(signal_name: &str) -> UResult<usize> {
let optional_signal_value = signal_by_name_or_value(signal_name);
let signal_name_upcase = signal_name.to_uppercase();
let optional_signal_value = signal_by_name_or_value(&signal_name_upcase);
match optional_signal_value {
Some(x) => Ok(x),
None => Err(USimpleError::new(
Expand Down
53 changes: 53 additions & 0 deletions tests/by-util/test_kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ fn test_kill_list_one_signal_from_name() {
.stdout_matches(&Regex::new("\\b9\\b").unwrap());
}

#[test]
fn test_kill_list_one_signal_ignore_case() {
// Use SIGKILL because it is 9 on all unixes.
new_ucmd!()
.arg("-l")
.arg("KiLl")
.succeeds()
.stdout_matches(&Regex::new("\\b9\\b").unwrap());
}

#[test]
fn test_kill_list_unknown_must_match_input_case() {
new_ucmd!()
.arg("-l")
.arg("IaMnOtAsIgNaL") // spell-checker:disable-line
.fails()
.stderr_contains("IaMnOtAsIgNaL"); // spell-checker:disable-line
}

#[test]
fn test_kill_list_all_vertically() {
// Check for a few signals. Do not try to be comprehensive.
Expand Down Expand Up @@ -217,6 +236,17 @@ fn test_kill_with_signal_name_new_form() {
assert_eq!(target.wait_for_signal(), Some(libc::SIGKILL));
}

#[test]
fn test_kill_with_signal_name_new_form_ignore_case() {
let mut target = Target::new();
new_ucmd!()
.arg("-s")
.arg("KiLl")
.arg(format!("{}", target.pid()))
.succeeds();
assert_eq!(target.wait_for_signal(), Some(libc::SIGKILL));
}

#[test]
fn test_kill_with_signal_prefixed_name_new_form() {
let mut target = Target::new();
Expand All @@ -228,6 +258,29 @@ fn test_kill_with_signal_prefixed_name_new_form() {
assert_eq!(target.wait_for_signal(), Some(libc::SIGKILL));
}

#[test]
fn test_kill_with_signal_prefixed_name_new_form_ignore_case() {
let mut target = Target::new();
new_ucmd!()
.arg("-s")
.arg("SiGKiLl")
.arg(format!("{}", target.pid()))
.succeeds();
assert_eq!(target.wait_for_signal(), Some(libc::SIGKILL));
}

#[test]
fn test_kill_with_signal_name_new_form_unknown_must_match_input_case() {
let target = Target::new();
new_ucmd!()
.arg("-s")
.arg("IaMnOtAsIgNaL") // spell-checker:disable-line
.arg(format!("{}", target.pid()))
.fails()
.stderr_contains("unknown signal")
.stderr_contains("IaMnOtAsIgNaL"); // spell-checker:disable-line
}

#[test]
fn test_kill_no_pid_provided() {
// spell-checker:disable-line
Expand Down
Loading