1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

mv: fix failing tests

This commit is contained in:
Roy Ivy III 2018-09-22 23:30:24 -05:00 committed by Roy Ivy III
parent 6f75331bf4
commit e00d586af1
2 changed files with 28 additions and 2 deletions

View file

@ -265,6 +265,12 @@ fn exec(files: &[PathBuf], b: Behaviour) -> i32 {
} }
return move_files_into_dir(&[source.clone()], target, &b); return move_files_into_dir(&[source.clone()], target, &b);
} else if target.exists() && source.is_dir() {
show_error!(
"cannot overwrite non-directory {} with directory {}",
target.display(), source.display()
);
return 1;
} }
if let Err(e) = rename(source, target, &b) { if let Err(e) = rename(source, target, &b) {
@ -360,6 +366,18 @@ fn rename(from: &PathBuf, to: &PathBuf, b: &Behaviour) -> Result<()> {
} }
} }
// "to" may no longer exist if it was backed up
if to.exists() && to.is_dir() {
// normalize behavior between *nix and windows
if from.is_dir() {
if is_empty_dir(to) {
try!(fs::remove_dir(to))
} else {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Directory not empty"));
}
}
}
try!(fs::rename(from, to)); try!(fs::rename(from, to));
if b.verbose { if b.verbose {
@ -405,3 +423,12 @@ fn existing_backup_path(path: &PathBuf, suffix: &str) -> PathBuf {
simple_backup_path(path, suffix) simple_backup_path(path, suffix)
} }
} }
fn is_empty_dir(path: &PathBuf) -> bool {
match fs::read_dir(path) {
Ok(contents) => {
return contents.peekable().peek().is_none();
},
Err(_e) => { return false; }
}
}

View file

@ -368,8 +368,7 @@ fn test_mv_errors() {
// $ mv -T -t a b // $ mv -T -t a b
// mv: cannot combine --target-directory (-t) and --no-target-directory (-T) // mv: cannot combine --target-directory (-t) and --no-target-directory (-T)
scene.ucmd().arg("-T").arg("-t").arg(dir).arg(file_a).arg(file_b).fails() scene.ucmd().arg("-T").arg("-t").arg(dir).arg(file_a).arg(file_b).fails()
.stderr_is("mv: error: cannot combine --target-directory (-t) and --no-target-directory \ .stderr_is("mv: error: cannot combine --target-directory (-t) and --no-target-directory (-T)\n");
(-T)\n");
// $ at.touch file && at.mkdir dir // $ at.touch file && at.mkdir dir
// $ mv -T file dir // $ mv -T file dir