adjust error for runcon & stdbuf to make tests/misc/invalid-opt.pl pass

This commit is contained in:
Sylvestre Ledru 2024-08-20 22:23:04 +02:00
parent 3cad85f05a
commit 001b97f445
7 changed files with 70 additions and 24 deletions

View file

@ -45,6 +45,7 @@ flamegraph
fsxattr
fullblock
getfacl
getopt
gibi
gibibytes
glob

View file

@ -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,
}
}
}

View file

@ -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<OsString>,
}
fn parse_command_line(config: Command, args: impl uucore::Args) -> Result<Options> {
let matches = config.try_get_matches_from(args)?;
fn parse_command_line(config: Command, args: impl uucore::Args) -> UResult<Options> {
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<Option
// runcon CONTEXT COMMAND [args]
args.next()
.ok_or(Error::MissingCommand)
.ok_or_else(|| Box::new(Error::MissingCommand) as Box<dyn UError>)
.map(move |command| Options {
mode: CommandLineMode::PlainContext { context, command },
arguments: args.collect(),

View file

@ -13,7 +13,7 @@ use std::path::PathBuf;
use std::process;
use tempfile::tempdir;
use tempfile::TempDir;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::error::{FromIo, UClapError, UResult, USimpleError, UUsageError};
use uucore::parse_size::parse_size_u64;
use uucore::{format_usage, help_about, help_section, help_usage};
@ -141,7 +141,7 @@ fn get_preload_env(tmp_dir: &TempDir) -> UResult<(String, PathBuf)> {
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let matches = uu_app().try_get_matches_from(args).with_exit_code(125)?;
let options = ProgramOptions::try_from(&matches).map_err(|e| UUsageError::new(125, e.0))?;

View file

@ -22,6 +22,11 @@ fn help() {
new_ucmd!().arg("-h").succeeds();
}
#[test]
fn invalid_input() {
new_ucmd!().arg("-/").fails().code_is(125);
}
#[test]
fn print() {
new_ucmd!().succeeds();
@ -46,7 +51,7 @@ fn invalid() {
"unconfined_u:unconfined_r:unconfined_t:s0",
"inexistent-file",
];
new_ucmd!().args(args).fails().code_is(127);
new_ucmd!().args(args).fails().code_is(1);
let args = &["invalid", "/bin/true"];
new_ucmd!().args(args).fails().code_is(1);
@ -55,7 +60,7 @@ fn invalid() {
new_ucmd!().args(args).fails().code_is(1);
let args = &["--compute", "--compute"];
new_ucmd!().args(args).fails().code_is(1);
new_ucmd!().args(args).fails().code_is(125);
// clap has an issue that makes this test fail: https://github.com/clap-rs/clap/issues/1543
// TODO: Enable this code once the issue is fixed in the clap version we're using.
@ -64,14 +69,15 @@ fn invalid() {
for flag in [
"-t", "--type", "-u", "--user", "-r", "--role", "-l", "--range",
] {
new_ucmd!().arg(flag).fails().code_is(1);
new_ucmd!().arg(flag).fails().code_is(125);
let args = &[flag, "example", flag, "example"];
new_ucmd!().args(args).fails().code_is(1);
new_ucmd!().args(args).fails().code_is(125);
}
}
#[test]
#[cfg(feature = "feat_selinux")]
fn plain_context() {
let ctx = "unconfined_u:unconfined_r:unconfined_t:s0-s0";
new_ucmd!().args(&[ctx, "/bin/true"]).succeeds();
@ -90,6 +96,7 @@ fn plain_context() {
}
#[test]
#[cfg(feature = "feat_selinux")]
fn custom_context() {
let t_ud = "unconfined_t";
let u_ud = "unconfined_u";

View file

@ -5,6 +5,11 @@
#[cfg(not(target_os = "windows"))]
use crate::common::util::TestScenario;
#[test]
fn invalid_input() {
new_ucmd!().arg("-/").fails().code_is(125);
}
#[cfg(all(not(target_os = "windows"), not(target_os = "openbsd")))]
#[test]
fn test_stdbuf_unbuffered_stdout() {

View file

@ -0,0 +1,32 @@
diff --git a/tests/misc/invalid-opt.pl b/tests/misc/invalid-opt.pl
index 4b9c4c184..4ccd89482 100755
--- a/tests/misc/invalid-opt.pl
+++ b/tests/misc/invalid-opt.pl
@@ -74,23 +74,13 @@ foreach my $prog (@built_programs)
defined $out
or $out = '';
- my $err = $expected_err{$prog};
- defined $err
- or $err = $x == 0 ? '' : "$prog: invalid option -- /\n$try";
-
- # Accommodate different syntax in glibc's getopt
- # diagnostics by filtering out single quotes.
- # Also accommodate BSD getopt.
- my $err_subst = "s,'/',/,; s,unknown,invalid,";
-
- # Depending on how this script is run, stty emits different
- # diagnostics. Don't bother checking them.
- $prog eq 'stty'
- and $err_subst = 's/(.|\n)*//ms';
+ # Strip all stderr output
+ # Our output is better and more consistent
+ my $err_subst = 's/(.|\n)*//ms';
my @Tests = (["$prog-invalid-opt", '-/', {OUT=>$out},
{ERR_SUBST => $err_subst},
- {EXIT=>$x}, {ERR=>$err}]);
+ {EXIT=>$x}]);
my $save_temps = $ENV{DEBUG};
my $verbose = $ENV{VERBOSE};