Skip to content

Commit

Permalink
adjust error for runcon & stdbuf to make tests/misc/invalid-opt.pl pass
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Aug 20, 2024
1 parent 3cad85f commit 001b97f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 24 deletions.
1 change: 1 addition & 0 deletions .vscode/cspell.dictionaries/jargon.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ flamegraph
fsxattr
fullblock
getfacl
getopt
gibi
gibibytes
glob
Expand Down
14 changes: 14 additions & 0 deletions src/uu/runcon/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,17 @@ impl Display for RunconError {
write_full_error(f, &self.inner)
}
}

impl UError for Error {
fn code(&self) -> i32 {

Check warning on line 122 in src/uu/runcon/src/errors.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/runcon/src/errors.rs#L122

Added line #L122 was not covered by tests
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,

Check warning on line 130 in src/uu/runcon/src/errors.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/runcon/src/errors.rs#L124-L130

Added lines #L124 - L130 were not covered by tests
}
}

Check warning on line 132 in src/uu/runcon/src/errors.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/runcon/src/errors.rs#L132

Added line #L132 was not covered by tests
}
23 changes: 5 additions & 18 deletions src/uu/runcon/src/runcon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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());
}
};

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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>)

Check warning on line 223 in src/uu/runcon/src/runcon.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/runcon/src/runcon.rs#L223

Added line #L223 was not covered by tests
.map(move |command| Options {
mode: CommandLineMode::PlainContext { context, command },
arguments: args.collect(),
Expand Down
4 changes: 2 additions & 2 deletions src/uu/stdbuf/src/stdbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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))?;

Expand Down
15 changes: 11 additions & 4 deletions tests/by-util/test_runcon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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();
Expand All @@ -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";
Expand Down
5 changes: 5 additions & 0 deletions tests/by-util/test_stdbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
32 changes: 32 additions & 0 deletions util/gnu-patches/tests_invalid_opt.patch
Original file line number Diff line number Diff line change
@@ -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};

0 comments on commit 001b97f

Please sign in to comment.