From 70d84e168c1620c03a171429acfeeb48068dbc5f Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Mon, 1 Apr 2024 08:06:18 +0200 Subject: [PATCH] shred: accept shortcuts for stringly-enum arguments --- src/uu/shred/src/shred.rs | 5 +++-- tests/by-util/test_shred.rs | 41 +++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/uu/shred/src/shred.rs b/src/uu/shred/src/shred.rs index d023b6210..763d6cfd4 100644 --- a/src/uu/shred/src/shred.rs +++ b/src/uu/shred/src/shred.rs @@ -17,6 +17,7 @@ use std::path::{Path, PathBuf}; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; use uucore::parse_size::parse_size_u64; +use uucore::shortcut_value_parser::ShortcutValueParser; use uucore::{format_usage, help_about, help_section, help_usage, show_error, show_if_err}; const ABOUT: &str = help_about!("shred.md"); @@ -315,11 +316,11 @@ pub fn uu_app() -> Command { Arg::new(options::REMOVE) .long(options::REMOVE) .value_name("HOW") - .value_parser([ + .value_parser(ShortcutValueParser::new([ options::remove::UNLINK, options::remove::WIPE, options::remove::WIPESYNC, - ]) + ])) .num_args(0..=1) .require_equals(true) .default_missing_value(options::remove::WIPESYNC) diff --git a/tests/by-util/test_shred.rs b/tests/by-util/test_shred.rs index 1ff847afc..82e421839 100644 --- a/tests/by-util/test_shred.rs +++ b/tests/by-util/test_shred.rs @@ -17,6 +17,11 @@ fn test_invalid_remove_arg() { new_ucmd!().arg("--remove=unknown").fails().code_is(1); } +#[test] +fn test_ambiguous_remove_arg() { + new_ucmd!().arg("--remove=wip").fails().code_is(1); +} + #[test] fn test_shred() { let (at, mut ucmd) = at_and_ucmd!(); @@ -49,15 +54,15 @@ fn test_shred_remove() { #[test] fn test_shred_remove_unlink() { - let (at, mut ucmd) = at_and_ucmd!(); - - let file = "test_shred_remove_unlink"; - at.touch(file); - - ucmd.arg("--remove=unlink").arg(file).succeeds(); - - // File was deleted - assert!(!at.file_exists(file)); + // spell-checker:disable-next-line + for argument in ["--remove=unlink", "--remove=unlin", "--remove=u"] { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_shred_remove_unlink"; + at.touch(file); + ucmd.arg(argument).arg(file).succeeds(); + // File was deleted + assert!(!at.file_exists(file)); + } } #[test] @@ -75,15 +80,15 @@ fn test_shred_remove_wipe() { #[test] fn test_shred_remove_wipesync() { - let (at, mut ucmd) = at_and_ucmd!(); - - let file = "test_shred_remove_wipesync"; - at.touch(file); - - ucmd.arg("--remove=wipesync").arg(file).succeeds(); - - // File was deleted - assert!(!at.file_exists(file)); + // spell-checker:disable-next-line + for argument in ["--remove=wipesync", "--remove=wipesyn", "--remove=wipes"] { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_shred_remove_wipesync"; + at.touch(file); + ucmd.arg(argument).arg(file).succeeds(); + // File was deleted + assert!(!at.file_exists(file)); + } } #[test]