Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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 @@
// 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
3 changes: 1 addition & 2 deletions util/build-gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ grep -rl 'path_prepend_' tests/* | xargs sed -i 's| path_prepend_ ./src||'

# Remove tests checking for --version & --help
# Not really interesting for us and logs are too big
sed -i -e '/tests\/misc\/invalid-opt.pl/ D' \
-e '/tests\/help\/help-version.sh/ D' \
sed -i -e '/tests\/help\/help-version.sh/ D' \
-e '/tests\/help\/help-version-getopt.sh/ D' \
Makefile

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};
Loading