From ff8a83b256b1983c56d85933fa0d869046a503d9 Mon Sep 17 00:00:00 2001 From: Hanif Ariffin Date: Thu, 3 Feb 2022 21:10:39 +0800 Subject: [PATCH 1/3] touch: Better error message when no args is given Matches the behavior of GNU touch ```shell hbina@akarin ~/g/uutils (hbina-realpath-absolute-symlinks)> touch > /dev/null touch: missing file operand Try 'touch --help' for more information. hbina@akarin ~/g/uutils (hbina-realpath-absolute-symlinks) [1]> cargo run --quiet -- touch > /dev/null touch: missing file operand Try 'touch --help' for more information. hbina@akarin ~/g/uutils (hbina-realpath-absolute-symlinks) [1]> cargo run --quiet -- touch 2> /dev/null hbina@akarin ~/g/uutils (hbina-realpath-absolute-symlinks) [1]> touch 2> /dev/null ``` Signed-off-by: Hanif Ariffin --- src/uu/touch/src/touch.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index b1df1aca4..32dd4817d 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -58,7 +58,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().override_usage(&usage[..]).get_matches_from(args); - let files = matches.values_of_os(ARG_FILES).unwrap(); + let files = matches.values_of_os(ARG_FILES).ok_or(USimpleError::new( + 1, + r##"missing file operand +Try 'touch --help' for more information."##, + ))?; let (mut atime, mut mtime) = if let Some(reference) = matches.value_of_os(options::sources::REFERENCE) { From 9cd65c766af5d9996926b8a429d2e442b6a5b2e9 Mon Sep 17 00:00:00 2001 From: Hanif Ariffin Date: Thu, 3 Feb 2022 21:14:56 +0800 Subject: [PATCH 2/3] Add tests Signed-off-by: Hanif Ariffin --- tests/by-util/test_touch.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index e661907cc..dd4a0b6cc 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -530,3 +530,12 @@ fn test_touch_permission_denied_error_msg() { &full_path )); } + +#[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."##, + ); +} From 861437addf4dee8cb3ab932cf0b23eb62e548296 Mon Sep 17 00:00:00 2001 From: Hanif Ariffin Date: Thu, 3 Feb 2022 21:45:02 +0800 Subject: [PATCH 3/3] Fix small clippy issue Signed-off-by: Hanif Ariffin --- src/uu/touch/src/touch.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 32dd4817d..e27dbfc18 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -58,11 +58,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().override_usage(&usage[..]).get_matches_from(args); - let files = matches.values_of_os(ARG_FILES).ok_or(USimpleError::new( - 1, - r##"missing file operand + let files = matches.values_of_os(ARG_FILES).ok_or_else(|| { + USimpleError::new( + 1, + r##"missing file operand Try 'touch --help' for more information."##, - ))?; + ) + })?; let (mut atime, mut mtime) = if let Some(reference) = matches.value_of_os(options::sources::REFERENCE) {