diff --git a/src/uu/nohup/src/nohup.rs b/src/uu/nohup/src/nohup.rs index 28292ac4154..38b5e5ceb30 100644 --- a/src/uu/nohup/src/nohup.rs +++ b/src/uu/nohup/src/nohup.rs @@ -55,10 +55,21 @@ impl UError for NohupError { } } +fn failure_code() -> i32 { + if env::var("POSIXLY_CORRECT").is_ok() { + POSIX_NOHUP_FAILURE + } else { + EXIT_CANCELED + } +} + #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let matches = - uucore::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 125)?; + let matches = uucore::clap_localization::handle_clap_result_with_exit_code( + uu_app(), + args, + failure_code(), + )?; replace_fds()?; @@ -124,10 +135,7 @@ fn replace_fds() -> UResult<()> { } fn find_stdout() -> UResult { - let internal_failure_code = match env::var("POSIXLY_CORRECT") { - Ok(_) => POSIX_NOHUP_FAILURE, - Err(_) => EXIT_CANCELED, - }; + let internal_failure_code = failure_code(); match OpenOptions::new() .create(true) diff --git a/tests/by-util/test_nohup.rs b/tests/by-util/test_nohup.rs index 2349b2dc2a8..f3fa0bc948c 100644 --- a/tests/by-util/test_nohup.rs +++ b/tests/by-util/test_nohup.rs @@ -14,8 +14,17 @@ use uutests::util_name; // All that can be tested is the side-effects. #[test] -fn test_invalid_arg() { - new_ucmd!().arg("--definitely-invalid").fails_with_code(125); +fn test_nohup_exit_codes() { + // No args: 125 default, 127 with POSIXLY_CORRECT + new_ucmd!().fails_with_code(125); + new_ucmd!().env("POSIXLY_CORRECT", "1").fails_with_code(127); + + // Invalid arg: 125 default, 127 with POSIXLY_CORRECT + new_ucmd!().arg("--invalid").fails_with_code(125); + new_ucmd!() + .env("POSIXLY_CORRECT", "1") + .arg("--invalid") + .fails_with_code(127); } #[test]