From d060134d9754d182548a8af258f2e9032e3c2696 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 25 Mar 2024 09:40:36 +0100 Subject: [PATCH] `uniq`: print version and help on stdout again (#6123) * uniq: print version and help on stdout again * uniq: format test * uniq: replace redundant closure with fn --------- Co-authored-by: Daniel Hofstetter --- src/uu/uniq/src/uniq.rs | 6 +++--- tests/by-util/test_uniq.rs | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/uu/uniq/src/uniq.rs b/src/uu/uniq/src/uniq.rs index e074ebe42..2c4f9b781 100644 --- a/src/uu/uniq/src/uniq.rs +++ b/src/uu/uniq/src/uniq.rs @@ -518,7 +518,7 @@ fn handle_extract_obs_skip_chars( /// Unfortunately these overrides are necessary, since several GNU tests /// for `uniq` hardcode and require the exact wording of the error message /// and it is not compatible with how Clap formats and displays those error messages. -fn map_clap_errors(clap_error: &Error) -> Box { +fn map_clap_errors(clap_error: Error) -> Box { let footer = "Try 'uniq --help' for more information."; let override_arg_conflict = "--group is mutually exclusive with -c/-d/-D/-u\n".to_string() + footer; @@ -547,7 +547,7 @@ fn map_clap_errors(clap_error: &Error) -> Box { { override_all_repeated_badoption } - _ => clap_error.to_string(), + _ => return clap_error.into(), }; USimpleError::new(1, error_message) } @@ -558,7 +558,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app() .try_get_matches_from(args) - .map_err(|e| map_clap_errors(&e))?; + .map_err(map_clap_errors)?; let files = matches.get_many::(ARG_FILES); diff --git a/tests/by-util/test_uniq.rs b/tests/by-util/test_uniq.rs index dd055402f..911f14490 100644 --- a/tests/by-util/test_uniq.rs +++ b/tests/by-util/test_uniq.rs @@ -18,6 +18,20 @@ fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails().code_is(1); } +#[test] +fn test_help_and_version_on_stdout() { + new_ucmd!() + .arg("--help") + .succeeds() + .no_stderr() + .stdout_contains("Usage"); + new_ucmd!() + .arg("--version") + .succeeds() + .no_stderr() + .stdout_contains("uniq"); +} + #[test] fn test_stdin_default() { new_ucmd!()