From a69de2dcd285f695731a20645c4ceeaec2984be0 Mon Sep 17 00:00:00 2001 From: Krysztal Huang Date: Wed, 10 Jul 2024 16:00:26 +0800 Subject: [PATCH] pidof: Add test for no name and no pid found case. --- src/uu/pidof/src/pidof.rs | 6 ++++++ tests/by-util/test_pidof.rs | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/uu/pidof/src/pidof.rs b/src/uu/pidof/src/pidof.rs index 4fbda42..30fa08a 100644 --- a/src/uu/pidof/src/pidof.rs +++ b/src/uu/pidof/src/pidof.rs @@ -25,6 +25,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { }; let mut collected = collect_matched_pids(&matches); + + if collected.is_empty() { + uucore::error::set_exit_code(1); + return Ok(()); + }; + collected.sort_by(|a, b| b.pid.cmp(&a.pid)); let output = collected diff --git a/tests/by-util/test_pidof.rs b/tests/by-util/test_pidof.rs index 373bf8d..4c15eb9 100644 --- a/tests/by-util/test_pidof.rs +++ b/tests/by-util/test_pidof.rs @@ -21,3 +21,15 @@ fn test_find_init() { fn test_find_kthreadd() { new_ucmd!().arg("kthreadd").succeeds(); } + +#[test] +#[cfg(target_os = "linux")] +fn test_no_program() { + new_ucmd!().fails().code_is(1); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_no_pid_found() { + new_ucmd!().arg("NO_THIS_PROGRAM").fails().code_is(1); +}