From 44ae927969dae87077e76a31bb53997cbe5d1e3d Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Tue, 3 Aug 2021 23:37:20 +0200 Subject: [PATCH 1/3] base32/64: print the first extra operand, not the one before Only one operand is allowed, so when there are multiple arguments we should print the second argument in the error. --- src/uu/base32/src/base_common.rs | 11 +++++++---- tests/by-util/test_base32.rs | 4 ++-- tests/by-util/test_base64.rs | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index 4fc8b495b..bc2faf63e 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -34,12 +34,15 @@ pub mod options { } impl Config { - fn from(options: clap::ArgMatches) -> Result { + fn from(app_name: &str, options: clap::ArgMatches) -> Result { let file: Option = match options.values_of(options::FILE) { Some(mut values) => { let name = values.next().unwrap(); - if values.len() != 0 { - return Err(format!("extra operand '{}'", name)); + if let Some(extra_op) = values.next() { + return Err(format!( + "extra operand '{}'\nTry '{} --help' for more information.", + extra_op, app_name + )); } if name == "-" { @@ -82,7 +85,7 @@ pub fn parse_base_cmd_args( let arg_list = args .collect_str(InvalidEncodingHandling::ConvertLossy) .accept_any(); - Config::from(app.get_matches_from(arg_list)) + Config::from(name, app.get_matches_from(arg_list)) } pub fn base_app<'a>(name: &str, version: &'a str, about: &'a str) -> App<'static, 'a> { diff --git a/tests/by-util/test_base32.rs b/tests/by-util/test_base32.rs index 38ead28f1..b7a976aa9 100644 --- a/tests/by-util/test_base32.rs +++ b/tests/by-util/test_base32.rs @@ -112,9 +112,9 @@ fn test_base32_extra_operand() { // Expect a failure when multiple files are specified. new_ucmd!() .arg("a.txt") - .arg("a.txt") + .arg("b.txt") .fails() - .stderr_only("base32: extra operand 'a.txt'"); + .stderr_only("base32: extra operand 'b.txt'"); } #[test] diff --git a/tests/by-util/test_base64.rs b/tests/by-util/test_base64.rs index 7c7f19205..73a30fffe 100644 --- a/tests/by-util/test_base64.rs +++ b/tests/by-util/test_base64.rs @@ -98,9 +98,9 @@ fn test_base64_extra_operand() { // Expect a failure when multiple files are specified. new_ucmd!() .arg("a.txt") - .arg("a.txt") + .arg("b.txt") .fails() - .stderr_only("base64: extra operand 'a.txt'"); + .stderr_only("base64: extra operand 'b.txt'"); } #[test] From 9700f0503dbc061f439f741153e3be7924192146 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Tue, 3 Aug 2021 23:37:49 +0200 Subject: [PATCH 2/3] base32/64: adjust error message to match gnu --- src/uu/base32/src/base_common.rs | 2 +- tests/by-util/test_base32.rs | 4 ++-- tests/by-util/test_base64.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index bc2faf63e..4f15db6bf 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -61,7 +61,7 @@ impl Config { .value_of(options::WRAP) .map(|num| { num.parse::() - .map_err(|e| format!("Invalid wrap size: '{}': {}", num, e)) + .map_err(|_| format!("invalid wrap size: '{}'", num)) }) .transpose()?; diff --git a/tests/by-util/test_base32.rs b/tests/by-util/test_base32.rs index b7a976aa9..178341f44 100644 --- a/tests/by-util/test_base32.rs +++ b/tests/by-util/test_base32.rs @@ -103,7 +103,7 @@ fn test_wrap_bad_arg() { .arg(wrap_param) .arg("b") .fails() - .stderr_only("base32: Invalid wrap size: 'b': invalid digit found in string\n"); + .stderr_only("base32: invalid wrap size: 'b'\n"); } } @@ -114,7 +114,7 @@ fn test_base32_extra_operand() { .arg("a.txt") .arg("b.txt") .fails() - .stderr_only("base32: extra operand 'b.txt'"); + .stderr_only("base32: extra operand 'b.txt'\nTry 'base32 --help' for more information."); } #[test] diff --git a/tests/by-util/test_base64.rs b/tests/by-util/test_base64.rs index 73a30fffe..a860aae91 100644 --- a/tests/by-util/test_base64.rs +++ b/tests/by-util/test_base64.rs @@ -89,7 +89,7 @@ fn test_wrap_bad_arg() { .arg(wrap_param) .arg("b") .fails() - .stderr_only("base64: Invalid wrap size: 'b': invalid digit found in string\n"); + .stderr_only("base64: invalid wrap size: 'b'\n"); } } @@ -100,7 +100,7 @@ fn test_base64_extra_operand() { .arg("a.txt") .arg("b.txt") .fails() - .stderr_only("base64: extra operand 'b.txt'"); + .stderr_only("base64: extra operand 'b.txt'\nTry 'base64 --help' for more information."); } #[test] From 319b9844340cba6c65ac8c0bb116d33e1644e751 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Tue, 3 Aug 2021 23:42:40 +0200 Subject: [PATCH 3/3] gnu-test: base32/64: modify test expectation for invalid inputs When decoding an invalid base32/64 string, gnu writes everything it was able to decode until it hit the decode error, while we don't write anything if the input is invalid. --- util/build-gnu.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/util/build-gnu.sh b/util/build-gnu.sh index 798a33456..1ce0133f8 100644 --- a/util/build-gnu.sh +++ b/util/build-gnu.sh @@ -1,6 +1,6 @@ #!/bin/bash -# spell-checker:ignore (paths) abmon deref discrim eacces getlimits getopt ginstall gnulib inacc infloop inotify reflink ; (misc) INT_OFLOW OFLOW ; (vars/env) BUILDDIR SRCDIR +# spell-checker:ignore (paths) abmon deref discrim eacces getlimits getopt ginstall gnulib inacc infloop inotify reflink ; (misc) INT_OFLOW OFLOW baddecode ; (vars/env) BUILDDIR SRCDIR set -e if test ! -d ../gnu; then @@ -119,3 +119,7 @@ sed -i -e "s|rm: cannot remove 'a/1'|rm: cannot remove 'a'|g" tests/rm/rm2.sh sed -i -e "s|removed directory 'a/'|removed directory 'a'|g" tests/rm/v-slash.sh test -f "${BUILDDIR}/getlimits" || cp src/getlimits "${BUILDDIR}" + +# When decoding an invalid base32/64 string, gnu writes everything it was able to decode until +# it hit the decode error, while we don't write anything if the input is invalid. +sed -i "s/\(baddecode.*OUT=>\"\).*\"/\1\"/g" tests/misc/base64.pl \ No newline at end of file