From 001b97f4455b9e5b5e9a0508415bd73faa82870f Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 20 Aug 2024 22:23:04 +0200 Subject: [PATCH] adjust error for runcon & stdbuf to make tests/misc/invalid-opt.pl pass --- .../cspell.dictionaries/jargon.wordlist.txt | 1 + src/uu/runcon/src/errors.rs | 14 ++++++++ src/uu/runcon/src/runcon.rs | 23 +++---------- src/uu/stdbuf/src/stdbuf.rs | 4 +-- tests/by-util/test_runcon.rs | 15 ++++++--- tests/by-util/test_stdbuf.rs | 5 +++ util/gnu-patches/tests_invalid_opt.patch | 32 +++++++++++++++++++ 7 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 util/gnu-patches/tests_invalid_opt.patch diff --git a/.vscode/cspell.dictionaries/jargon.wordlist.txt b/.vscode/cspell.dictionaries/jargon.wordlist.txt index e0c520922c5..c2e01f508e3 100644 --- a/.vscode/cspell.dictionaries/jargon.wordlist.txt +++ b/.vscode/cspell.dictionaries/jargon.wordlist.txt @@ -45,6 +45,7 @@ flamegraph fsxattr fullblock getfacl +getopt gibi gibibytes glob diff --git a/src/uu/runcon/src/errors.rs b/src/uu/runcon/src/errors.rs index b2bfcad9598..9ffd24da482 100644 --- a/src/uu/runcon/src/errors.rs +++ b/src/uu/runcon/src/errors.rs @@ -117,3 +117,17 @@ impl Display for RunconError { write_full_error(f, &self.inner) } } + +impl UError for Error { + fn code(&self) -> i32 { + match self { + Error::MissingCommand => error_exit_status::ANOTHER_ERROR, + Error::SELinuxNotEnabled => error_exit_status::ANOTHER_ERROR, + Error::NotUTF8(_) => error_exit_status::ANOTHER_ERROR, + Error::CommandLine(e) => e.exit_code(), + Error::SELinux { .. } => error_exit_status::ANOTHER_ERROR, + Error::Io { .. } => error_exit_status::ANOTHER_ERROR, + Error::Io1 { .. } => error_exit_status::ANOTHER_ERROR, + } + } +} diff --git a/src/uu/runcon/src/runcon.rs b/src/uu/runcon/src/runcon.rs index a802542b2a2..c41dcdcf1dd 100644 --- a/src/uu/runcon/src/runcon.rs +++ b/src/uu/runcon/src/runcon.rs @@ -5,7 +5,7 @@ // spell-checker:ignore (vars) RFILE use clap::builder::ValueParser; -use uucore::error::{UResult, UUsageError}; +use uucore::error::{UClapError, UError, UResult}; use clap::{crate_version, Arg, ArgAction, Command}; use selinux::{OpaqueSecurityContext, SecurityClass, SecurityContext}; @@ -42,20 +42,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let options = match parse_command_line(config, args) { Ok(r) => r, Err(r) => { - if let Error::CommandLine(ref r) = r { - match r.kind() { - clap::error::ErrorKind::DisplayHelp - | clap::error::ErrorKind::DisplayVersion => { - println!("{r}"); - return Ok(()); - } - _ => {} - } - } - return Err(UUsageError::new( - error_exit_status::ANOTHER_ERROR, - format!("{r}"), - )); + return Err(r.into()); } }; @@ -198,8 +185,8 @@ struct Options { arguments: Vec, } -fn parse_command_line(config: Command, args: impl uucore::Args) -> Result { - let matches = config.try_get_matches_from(args)?; +fn parse_command_line(config: Command, args: impl uucore::Args) -> UResult { + let matches = config.try_get_matches_from(args).with_exit_code(125)?; let compute_transition_context = matches.get_flag(options::COMPUTE); @@ -233,7 +220,7 @@ fn parse_command_line(config: Command, args: impl uucore::Args) -> Result