Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down