From e94f1d8eb7d4ab0ed0f7c35119e9d6eaab6f4b15 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Sun, 23 Apr 2023 15:27:24 +0200 Subject: [PATCH] mv: -i show no error if overwriting is declined --- src/uu/mv/src/mv.rs | 34 ++++++++++++++++++---------------- tests/by-util/test_mv.rs | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index ba6f2198a..d0a1a766f 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -24,7 +24,7 @@ use std::os::windows; use std::path::{Path, PathBuf}; use uucore::backup_control::{self, BackupMode}; use uucore::display::Quotable; -use uucore::error::{FromIo, UError, UResult, USimpleError, UUsageError}; +use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError, UUsageError}; use uucore::{format_usage, help_about, help_usage, prompt_yes, show}; use fs_extra::dir::{ @@ -378,21 +378,23 @@ fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UR } } - let rename_result = rename(sourcepath, &targetpath, b, multi_progress.as_ref()) - .map_err_context(|| { - format!( - "cannot move {} to {}", - sourcepath.quote(), - targetpath.quote() - ) - }); - - if let Err(e) = rename_result { - match multi_progress { - Some(ref pb) => pb.suspend(|| show!(e)), - None => show!(e), - }; - }; + match rename(sourcepath, &targetpath, b, multi_progress.as_ref()) { + Err(e) if e.to_string() == "" => set_exit_code(1), + Err(e) => { + let e = e.map_err_context(|| { + format!( + "cannot move {} to {}", + sourcepath.quote(), + targetpath.quote() + ) + }); + match multi_progress { + Some(ref pb) => pb.suspend(|| show!(e)), + None => show!(e), + }; + } + Ok(()) => (), + } if let Some(ref pb) = count_progress { pb.inc(1); diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index f9b0c59ea..034234107 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -181,6 +181,26 @@ fn test_mv_interactive() { assert!(at.file_exists(file_b)); } +#[test] +fn test_mv_interactive_with_dir_as_target() { + let (at, mut ucmd) = at_and_ucmd!(); + + let file = "test_mv_interactive_file"; + let target_dir = "target"; + + at.mkdir(target_dir); + at.touch(file); + at.touch(format!("{target_dir}/{file}")); + + ucmd.arg(file) + .arg(target_dir) + .arg("-i") + .pipe_in("n") + .fails() + .stderr_does_not_contain("cannot move") + .no_stdout(); +} + #[test] fn test_mv_arg_update_interactive() { let (at, mut ucmd) = at_and_ucmd!();