Skip to content

Commit

Permalink
mv: gnu test backup-dir regression fix
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixhead committed Sep 12, 2024
1 parent e8c9ffc commit 0811df8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,21 @@ fn rename(
) -> io::Result<()> {
let mut backup_path = None;

// If `no-target-directory` is specified, we treat the destination as a file.
// In that case, if there is a trailing forward slash, we remove it.
let to = if path_ends_with_terminator(to) && opts.no_target_dir {
let _to = to.to_string_lossy();
if cfg!(windows) {
Path::new(_to.trim_end_matches('\\')).to_path_buf()
} else {
Path::new(_to.trim_end_matches('/')).to_path_buf()
}
} else {
to.to_path_buf()
};

let to = &to;

if to.exists() {
if opts.update == UpdateMode::ReplaceIfOlder && opts.overwrite == OverwriteMode::Interactive
{
Expand Down
21 changes: 21 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,3 +1778,24 @@ mod inter_partition_copying {
.stderr_contains("Permission denied");
}
}

#[cfg(unix)]
#[test]
fn test_mv_backup_when_dest_has_trailing_slash() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.mkdir("D");
at.mkdir("E");
at.touch("D/d_file");
at.touch("E/e_file");
scene
.ucmd()
.arg("-T")
.arg("--backup=numbered")
.arg("D")
.arg("E/")
.succeeds();
assert!(at.dir_exists("E.~1~"), "Backup folder not created");
assert!(at.file_exists("E.~1~/e_file"), "Backup file not created");
assert!(at.file_exists("E/d_file"), "source file didn't move");
}

0 comments on commit 0811df8

Please sign in to comment.