mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
rm: fix for -d to match GNU's output #1769
This commit is contained in:
parent
99be7a3172
commit
58b9372dbe
2 changed files with 36 additions and 7 deletions
|
@ -305,16 +305,28 @@ fn remove_dir(path: &Path, options: &Options) -> bool {
|
||||||
true
|
true
|
||||||
};
|
};
|
||||||
if response {
|
if response {
|
||||||
match fs::remove_dir(path) {
|
if let Ok(mut read_dir) = fs::read_dir(path) {
|
||||||
Ok(_) => {
|
if options.dir && read_dir.next().is_none() {
|
||||||
if options.verbose {
|
match fs::remove_dir(path) {
|
||||||
println!("removed '{}'", path.display());
|
Ok(_) => {
|
||||||
|
if options.verbose {
|
||||||
|
println!("removed directory '{}'", path.display());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
show_error!("cannot remove '{}': {}", path.display(), e);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
Err(e) => {
|
// directory can be read but is not empty
|
||||||
show_error!("removing '{}': {}", path.display(), e);
|
show_error!("cannot remove '{}': Directory not empty", path.display());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// GNU's rm shows this message if directory is empty but not readable
|
||||||
|
show_error!("cannot remove '{}': Directory not empty", path.display());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -115,6 +115,23 @@ fn test_rm_empty_directory() {
|
||||||
assert!(!at.dir_exists(dir));
|
assert!(!at.dir_exists(dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rm_non_empty_directory() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let dir = "test_rm_non_empty_dir";
|
||||||
|
let file_a = &format!("{}/test_rm_non_empty_file_a", dir);
|
||||||
|
|
||||||
|
at.mkdir(dir);
|
||||||
|
at.touch(file_a);
|
||||||
|
|
||||||
|
let result = ucmd.arg("-d").arg(dir).fails();
|
||||||
|
assert!(result
|
||||||
|
.stderr
|
||||||
|
.contains(&format!("cannot remove '{}': Directory not empty", dir)));
|
||||||
|
assert!(at.file_exists(file_a));
|
||||||
|
assert!(at.dir_exists(dir));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rm_recursive() {
|
fn test_rm_recursive() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue