Skip to content

Commit f9e7645

Browse files
authored
Merge pull request #6657 from sylvestre/invalid-opt
adjust error for runcon & stdbuf to make tests/misc/invalid-opt.pl pass
2 parents 0e6565b + 01db148 commit f9e7645

File tree

8 files changed

+71
-26
lines changed

8 files changed

+71
-26
lines changed

.vscode/cspell.dictionaries/jargon.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ flamegraph
4545
fsxattr
4646
fullblock
4747
getfacl
48+
getopt
4849
gibi
4950
gibibytes
5051
glob

src/uu/runcon/src/errors.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,17 @@ impl Display for RunconError {
117117
write_full_error(f, &self.inner)
118118
}
119119
}
120+
121+
impl UError for Error {
122+
fn code(&self) -> i32 {
123+
match self {
124+
Error::MissingCommand => error_exit_status::ANOTHER_ERROR,
125+
Error::SELinuxNotEnabled => error_exit_status::ANOTHER_ERROR,
126+
Error::NotUTF8(_) => error_exit_status::ANOTHER_ERROR,
127+
Error::CommandLine(e) => e.exit_code(),
128+
Error::SELinux { .. } => error_exit_status::ANOTHER_ERROR,
129+
Error::Io { .. } => error_exit_status::ANOTHER_ERROR,
130+
Error::Io1 { .. } => error_exit_status::ANOTHER_ERROR,
131+
}
132+
}
133+
}

src/uu/runcon/src/runcon.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// spell-checker:ignore (vars) RFILE
66

77
use clap::builder::ValueParser;
8-
use uucore::error::{UResult, UUsageError};
8+
use uucore::error::{UClapError, UError, UResult};
99

1010
use clap::{crate_version, Arg, ArgAction, Command};
1111
use selinux::{OpaqueSecurityContext, SecurityClass, SecurityContext};
@@ -42,20 +42,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4242
let options = match parse_command_line(config, args) {
4343
Ok(r) => r,
4444
Err(r) => {
45-
if let Error::CommandLine(ref r) = r {
46-
match r.kind() {
47-
clap::error::ErrorKind::DisplayHelp
48-
| clap::error::ErrorKind::DisplayVersion => {
49-
println!("{r}");
50-
return Ok(());
51-
}
52-
_ => {}
53-
}
54-
}
55-
return Err(UUsageError::new(
56-
error_exit_status::ANOTHER_ERROR,
57-
format!("{r}"),
58-
));
45+
return Err(r.into());
5946
}
6047
};
6148

@@ -198,8 +185,8 @@ struct Options {
198185
arguments: Vec<OsString>,
199186
}
200187

201-
fn parse_command_line(config: Command, args: impl uucore::Args) -> Result<Options> {
202-
let matches = config.try_get_matches_from(args)?;
188+
fn parse_command_line(config: Command, args: impl uucore::Args) -> UResult<Options> {
189+
let matches = config.try_get_matches_from(args).with_exit_code(125)?;
203190

204191
let compute_transition_context = matches.get_flag(options::COMPUTE);
205192

@@ -233,7 +220,7 @@ fn parse_command_line(config: Command, args: impl uucore::Args) -> Result<Option
233220
// runcon CONTEXT COMMAND [args]
234221

235222
args.next()
236-
.ok_or(Error::MissingCommand)
223+
.ok_or_else(|| Box::new(Error::MissingCommand) as Box<dyn UError>)
237224
.map(move |command| Options {
238225
mode: CommandLineMode::PlainContext { context, command },
239226
arguments: args.collect(),

src/uu/stdbuf/src/stdbuf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::path::PathBuf;
1313
use std::process;
1414
use tempfile::tempdir;
1515
use tempfile::TempDir;
16-
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
16+
use uucore::error::{FromIo, UClapError, UResult, USimpleError, UUsageError};
1717
use uucore::parse_size::parse_size_u64;
1818
use uucore::{format_usage, help_about, help_section, help_usage};
1919

@@ -141,7 +141,7 @@ fn get_preload_env(tmp_dir: &TempDir) -> UResult<(String, PathBuf)> {
141141

142142
#[uucore::main]
143143
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
144-
let matches = uu_app().try_get_matches_from(args)?;
144+
let matches = uu_app().try_get_matches_from(args).with_exit_code(125)?;
145145

146146
let options = ProgramOptions::try_from(&matches).map_err(|e| UUsageError::new(125, e.0))?;
147147

tests/by-util/test_runcon.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ fn help() {
2222
new_ucmd!().arg("-h").succeeds();
2323
}
2424

25+
#[test]
26+
fn invalid_input() {
27+
new_ucmd!().arg("-/").fails().code_is(125);
28+
}
29+
2530
#[test]
2631
fn print() {
2732
new_ucmd!().succeeds();
@@ -46,7 +51,7 @@ fn invalid() {
4651
"unconfined_u:unconfined_r:unconfined_t:s0",
4752
"inexistent-file",
4853
];
49-
new_ucmd!().args(args).fails().code_is(127);
54+
new_ucmd!().args(args).fails().code_is(1);
5055

5156
let args = &["invalid", "/bin/true"];
5257
new_ucmd!().args(args).fails().code_is(1);
@@ -55,7 +60,7 @@ fn invalid() {
5560
new_ucmd!().args(args).fails().code_is(1);
5661

5762
let args = &["--compute", "--compute"];
58-
new_ucmd!().args(args).fails().code_is(1);
63+
new_ucmd!().args(args).fails().code_is(125);
5964

6065
// clap has an issue that makes this test fail: https://github.com/clap-rs/clap/issues/1543
6166
// TODO: Enable this code once the issue is fixed in the clap version we're using.
@@ -64,14 +69,15 @@ fn invalid() {
6469
for flag in [
6570
"-t", "--type", "-u", "--user", "-r", "--role", "-l", "--range",
6671
] {
67-
new_ucmd!().arg(flag).fails().code_is(1);
72+
new_ucmd!().arg(flag).fails().code_is(125);
6873

6974
let args = &[flag, "example", flag, "example"];
70-
new_ucmd!().args(args).fails().code_is(1);
75+
new_ucmd!().args(args).fails().code_is(125);
7176
}
7277
}
7378

7479
#[test]
80+
#[cfg(feature = "feat_selinux")]
7581
fn plain_context() {
7682
let ctx = "unconfined_u:unconfined_r:unconfined_t:s0-s0";
7783
new_ucmd!().args(&[ctx, "/bin/true"]).succeeds();
@@ -90,6 +96,7 @@ fn plain_context() {
9096
}
9197

9298
#[test]
99+
#[cfg(feature = "feat_selinux")]
93100
fn custom_context() {
94101
let t_ud = "unconfined_t";
95102
let u_ud = "unconfined_u";

tests/by-util/test_stdbuf.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#[cfg(not(target_os = "windows"))]
66
use crate::common::util::TestScenario;
77

8+
#[test]
9+
fn invalid_input() {
10+
new_ucmd!().arg("-/").fails().code_is(125);
11+
}
12+
813
#[cfg(all(not(target_os = "windows"), not(target_os = "openbsd")))]
914
#[test]
1015
fn test_stdbuf_unbuffered_stdout() {

util/build-gnu.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ grep -rl 'path_prepend_' tests/* | xargs sed -i 's| path_prepend_ ./src||'
174174

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
diff --git a/tests/misc/invalid-opt.pl b/tests/misc/invalid-opt.pl
2+
index 4b9c4c184..4ccd89482 100755
3+
--- a/tests/misc/invalid-opt.pl
4+
+++ b/tests/misc/invalid-opt.pl
5+
@@ -74,23 +74,13 @@ foreach my $prog (@built_programs)
6+
defined $out
7+
or $out = '';
8+
9+
- my $err = $expected_err{$prog};
10+
- defined $err
11+
- or $err = $x == 0 ? '' : "$prog: invalid option -- /\n$try";
12+
-
13+
- # Accommodate different syntax in glibc's getopt
14+
- # diagnostics by filtering out single quotes.
15+
- # Also accommodate BSD getopt.
16+
- my $err_subst = "s,'/',/,; s,unknown,invalid,";
17+
-
18+
- # Depending on how this script is run, stty emits different
19+
- # diagnostics. Don't bother checking them.
20+
- $prog eq 'stty'
21+
- and $err_subst = 's/(.|\n)*//ms';
22+
+ # Strip all stderr output
23+
+ # Our output is better and more consistent
24+
+ my $err_subst = 's/(.|\n)*//ms';
25+
26+
my @Tests = (["$prog-invalid-opt", '-/', {OUT=>$out},
27+
{ERR_SUBST => $err_subst},
28+
- {EXIT=>$x}, {ERR=>$err}]);
29+
+ {EXIT=>$x}]);
30+
31+
my $save_temps = $ENV{DEBUG};
32+
my $verbose = $ENV{VERBOSE};

0 commit comments

Comments
 (0)