From d78e1e73999020c76efd39ea50007f41878945d5 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 8 Jan 2023 12:39:49 -0600 Subject: [PATCH 1/3] tests: revise/standardize usage error testing (for dd, install, mktemp, rm, seq, and touch) --- tests/by-util/test_dd.rs | 4 +-- tests/by-util/test_install.rs | 32 +++++++-------------- tests/by-util/test_mktemp.rs | 6 +--- tests/by-util/test_rm.rs | 6 +--- tests/by-util/test_seq.rs | 54 +++++++++++++++++++---------------- tests/by-util/test_touch.rs | 5 +--- 6 files changed, 45 insertions(+), 62 deletions(-) diff --git a/tests/by-util/test_dd.rs b/tests/by-util/test_dd.rs index ec909831b..ab4d4bf6a 100644 --- a/tests/by-util/test_dd.rs +++ b/tests/by-util/test_dd.rs @@ -1303,12 +1303,12 @@ fn test_invalid_flag_arg_gnu_compatibility() { new_ucmd!() .args(&[format!("{}=", command)]) .fails() - .stderr_is("dd: invalid input flag: ‘’\nTry 'dd --help' for more information."); + .usage_error("invalid input flag: ‘’"); new_ucmd!() .args(&[format!("{}=29d", command)]) .fails() - .stderr_is("dd: invalid input flag: ‘29d’\nTry 'dd --help' for more information."); + .usage_error("invalid input flag: ‘29d’"); } } diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index 3543d2628..4d2ead83b 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -1257,16 +1257,14 @@ fn test_install_missing_arguments() { .ucmd() .fails() .code_is(1) - .stderr_contains("install: missing file operand") - .stderr_contains("install --help' for more information."); + .usage_error("missing file operand"); scene .ucmd() .arg("-D") .arg(format!("-t {}", no_target_dir)) .fails() - .stderr_contains("install: missing file operand") - .stderr_contains("install --help' for more information."); + .usage_error("missing file operand"); assert!(!at.dir_exists(no_target_dir)); } @@ -1282,27 +1280,17 @@ fn test_install_missing_destination() { at.mkdir(dir_1); // will fail and also print some info on correct usage - scene - .ucmd() - .arg(file_1) - .fails() - .stderr_contains(format!( - "install: missing destination file operand after '{}'", - file_1 - )) - .stderr_contains("install --help' for more information."); + scene.ucmd().arg(file_1).fails().usage_error(format!( + "missing destination file operand after '{}'", + file_1 + )); // GNU's install will check for correct num of arguments and then fail // and it does not recognize, that the source is not a file but a directory. - scene - .ucmd() - .arg(dir_1) - .fails() - .stderr_contains(format!( - "install: missing destination file operand after '{}'", - dir_1 - )) - .stderr_contains("install --help' for more information."); + scene.ucmd().arg(dir_1).fails().usage_error(format!( + "missing destination file operand after '{}'", + dir_1 + )); } #[test] diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index d19f5d6d1..4650eb2fd 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -673,11 +673,7 @@ fn test_mktemp_with_posixly_correct() { .env("POSIXLY_CORRECT", "1") .args(&["aXXXX", "--suffix=b"]) .fails() - .stderr_is(&format!( - "mktemp: too many templates\nTry '{} {} --help' for more information.\n", - scene.bin_path.to_string_lossy(), - scene.util_name - )); + .usage_error("too many templates"); scene .ucmd() diff --git a/tests/by-util/test_rm.rs b/tests/by-util/test_rm.rs index 1ffd2cdf0..d11a6e379 100644 --- a/tests/by-util/test_rm.rs +++ b/tests/by-util/test_rm.rs @@ -286,11 +286,7 @@ fn test_rm_force_no_operand() { #[test] fn test_rm_no_operand() { let ts = TestScenario::new(util_name!()); - ts.ucmd().fails().stderr_is(&format!( - "{0}: missing operand\nTry '{1} {0} --help' for more information.\n", - ts.util_name, - ts.bin_path.to_string_lossy() - )); + ts.ucmd().fails().usage_error("missing operand"); } #[test] diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index ad224fbc7..fa73b2937 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -13,25 +13,40 @@ fn test_hex_rejects_sign_after_identifier() { .args(&["0x-123ABC"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '0x-123ABC'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '0x-123ABC'"); new_ucmd!() .args(&["0x+123ABC"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '0x+123ABC'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '0x+123ABC'"); + + new_ucmd!() + .args(&["--", "-0x-123ABC"]) + .fails() + .no_stdout() + .usage_error("invalid floating point argument: '-0x-123ABC'"); + new_ucmd!() + .args(&["--", "-0x+123ABC"]) + .fails() + .no_stdout() + .usage_error("invalid floating point argument: '-0x+123ABC'"); + + // test without "--" => argument parsed as (invalid) flag new_ucmd!() .args(&["-0x-123ABC"]) .fails() .no_stdout() - .stderr_contains("which wasn't expected, or isn't valid in this context") + .stderr_contains( + "Found argument '-0' which wasn't expected, or isn't valid in this context", + ) .stderr_contains("For more information try '--help'"); new_ucmd!() .args(&["-0x+123ABC"]) .fails() .no_stdout() - .stderr_contains("which wasn't expected, or isn't valid in this context") + .stderr_contains( + "Found argument '-0' which wasn't expected, or isn't valid in this context", + ) .stderr_contains("For more information try '--help'"); } @@ -66,8 +81,7 @@ fn test_hex_identifier_in_wrong_place() { .args(&["1234ABCD0x"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '1234ABCD0x'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '1234ABCD0x'"); } #[test] @@ -119,38 +133,32 @@ fn test_invalid_float() { .args(&["1e2.3"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '1e2.3'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '1e2.3'"); new_ucmd!() .args(&["1e2.3", "2"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '1e2.3'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '1e2.3'"); new_ucmd!() .args(&["1", "1e2.3"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '1e2.3'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '1e2.3'"); new_ucmd!() .args(&["1e2.3", "2", "3"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '1e2.3'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '1e2.3'"); new_ucmd!() .args(&["1", "1e2.3", "3"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '1e2.3'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '1e2.3'"); new_ucmd!() .args(&["1", "2", "1e2.3"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '1e2.3'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '1e2.3'"); } #[test] @@ -159,8 +167,7 @@ fn test_width_invalid_float() { .args(&["-w", "1e2.3"]) .fails() .no_stdout() - .stderr_contains("invalid floating point argument: '1e2.3'") - .stderr_contains("for more information."); + .usage_error("invalid floating point argument: '1e2.3'"); } // ---- Tests for the big integer based path ---- @@ -738,6 +745,5 @@ fn test_invalid_zero_increment_value() { .args(&["0", "0", "1"]) .fails() .no_stdout() - .stderr_contains("invalid Zero increment value: '0'") - .stderr_contains("for more information."); + .usage_error("invalid Zero increment value: '0'"); } diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index 3585fcf97..dc641a377 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -763,10 +763,7 @@ fn test_touch_permission_denied_error_msg() { #[test] fn test_touch_no_args() { let mut ucmd = new_ucmd!(); - ucmd.fails().stderr_only( - r##"touch: missing file operand -Try 'touch --help' for more information."##, - ); + ucmd.fails().no_stdout().usage_error("missing file operand"); } #[test] From 3d93e251609b89116163f1207dcb3e0929b172f4 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 8 Jan 2023 12:40:27 -0600 Subject: [PATCH 2/3] fix/touch ~ correct usage error display text --- src/uu/touch/src/touch.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index c898d8924..a50a00303 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -71,8 +71,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let files = matches.get_many::(ARG_FILES).ok_or_else(|| { USimpleError::new( 1, - r##"missing file operand -Try 'touch --help' for more information."##, + format!( + "missing file operand\nTry '{} --help' for more information.", + uucore::execution_phrase() + ), ) })?; let (mut atime, mut mtime) = From 27d20129fea28db237cb8e26250566f2389d61ad Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 8 Jan 2023 12:40:38 -0600 Subject: [PATCH 3/3] fix/dd ~ correct usage error display text --- src/uu/dd/src/parseargs.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/uu/dd/src/parseargs.rs b/src/uu/dd/src/parseargs.rs index 391287062..beeccc2e3 100644 --- a/src/uu/dd/src/parseargs.rs +++ b/src/uu/dd/src/parseargs.rs @@ -415,8 +415,9 @@ impl std::fmt::Display for ParseError { // Additional message about 'dd --help' is displayed only in this situation. write!( f, - "invalid input flag: ‘{}’\nTry 'dd --help' for more information.", - arg + "invalid input flag: ‘{}’\nTry '{} --help' for more information.", + arg, + uucore::execution_phrase() ) } Self::ConvFlagNoMatch(arg) => {