From 78909fdd392ce4513c87b29f81cf05776384ee4d Mon Sep 17 00:00:00 2001 From: John Shin Date: Mon, 22 May 2023 21:11:47 -0700 Subject: [PATCH 1/4] rm: allow empty when for --interactive arg --- src/uu/rm/src/rm.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index 2a8a57be06..2d875bdd96 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -169,6 +169,9 @@ pub fn uu_app() -> Command { prompts always", ) .value_name("WHEN") + .num_args(0..=1) + .require_equals(true) + .default_missing_value("always") .overrides_with_all([OPT_PROMPT, OPT_PROMPT_MORE]), ) .arg( From eeb3341101f9bcb500f51e4b242850bbd831e5cd Mon Sep 17 00:00:00 2001 From: John Shin Date: Mon, 22 May 2023 21:23:44 -0700 Subject: [PATCH 2/4] rm: add tests for --interactive with empty when --- tests/by-util/test_rm.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/by-util/test_rm.rs b/tests/by-util/test_rm.rs index a5c342ffe7..85d96d2ee7 100644 --- a/tests/by-util/test_rm.rs +++ b/tests/by-util/test_rm.rs @@ -361,6 +361,26 @@ fn test_rm_interactive_never() { assert!(!at.file_exists(file_2)); } +#[test] +fn test_rm_interactive_missing_value() { + // `--interactive` is equivalent to `--interactive=always` or `-i` + let (at, mut ucmd) = at_and_ucmd!(); + + let file1 = "test_rm_interactive_missing_value_file1"; + let file2 = "test_rm_interactive_missing_value_file2"; + + at.touch(file1); + at.touch(file2); + + ucmd.arg("--interactive") + .arg(file1) + .arg(file2) + .pipe_in("y\ny") + .succeeds(); + + assert!(!at.file_exists(file1)); + assert!(!at.file_exists(file2)); +} #[test] fn test_rm_descend_directory() { // This test descends into each directory and deletes the files and folders inside of them From e50b84035fc54063e57dbc9e13757ae4986b8d22 Mon Sep 17 00:00:00 2001 From: John Shin Date: Mon, 22 May 2023 21:26:52 -0700 Subject: [PATCH 3/4] rm: fix prompt when --interactive is once --- src/uu/rm/src/rm.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index 2d875bdd96..f2e2050d9f 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -115,11 +115,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { verbose: matches.get_flag(OPT_VERBOSE), }; if options.interactive == InteractiveMode::Once && (options.recursive || files.len() > 3) { - let msg = if options.recursive { - "Remove all arguments recursively?" - } else { - "Remove all arguments?" - }; + let msg: String = format!( + "remove {} {}{}", + files.len(), + if files.len() > 1 { + "arguments" + } else { + "argument" + }, + if options.recursive { + " recursively?" + } else { + "?" + } + ); if !prompt_yes!("{}", msg) { return Ok(()); } From 421b2f258152112fae0ea34cdb6faecae4dfba4b Mon Sep 17 00:00:00 2001 From: John Shin Date: Mon, 22 May 2023 21:27:43 -0700 Subject: [PATCH 4/4] rm: test prompts when --interactive is once --- tests/by-util/test_rm.rs | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/by-util/test_rm.rs b/tests/by-util/test_rm.rs index 85d96d2ee7..737c4fa79b 100644 --- a/tests/by-util/test_rm.rs +++ b/tests/by-util/test_rm.rs @@ -381,6 +381,54 @@ fn test_rm_interactive_missing_value() { assert!(!at.file_exists(file1)); assert!(!at.file_exists(file2)); } + +#[test] +fn test_rm_interactive_once_prompt() { + let (at, mut ucmd) = at_and_ucmd!(); + + let file1 = "test_rm_interactive_once_recursive_prompt_file1"; + let file2 = "test_rm_interactive_once_recursive_prompt_file2"; + let file3 = "test_rm_interactive_once_recursive_prompt_file3"; + let file4 = "test_rm_interactive_once_recursive_prompt_file4"; + + at.touch(file1); + at.touch(file2); + at.touch(file3); + at.touch(file4); + + ucmd.arg("--interactive=once") + .arg(file1) + .arg(file2) + .arg(file3) + .arg(file4) + .pipe_in("y") + .succeeds() + .stderr_contains("remove 4 arguments?"); + + assert!(!at.file_exists(file1)); + assert!(!at.file_exists(file2)); + assert!(!at.file_exists(file3)); + assert!(!at.file_exists(file4)); +} + +#[test] +fn test_rm_interactive_once_recursive_prompt() { + let (at, mut ucmd) = at_and_ucmd!(); + + let file1 = "test_rm_interactive_once_recursive_prompt_file1"; + + at.touch(file1); + + ucmd.arg("--interactive=once") + .arg("-r") + .arg(file1) + .pipe_in("y") + .succeeds() + .stderr_contains("remove 1 argument recursively?"); + + assert!(!at.file_exists(file1)); +} + #[test] fn test_rm_descend_directory() { // This test descends into each directory and deletes the files and folders inside of them