Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #1769: rm: -d should match GNU's output #1780

Closed
wants to merge 10 commits into from
Closed
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
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ fn remove_dir(path: &Path, options: &Options) -> bool {
} else {
true
};

if response {
match fs::remove_dir(path) {
Ok(_) => {
Expand All @@ -306,7 +307,17 @@ fn remove_dir(path: &Path, options: &Options) -> bool {
}
}
Err(e) => {
show_error!("removing '{}': {}", path.display(), e);
if e.to_string().starts_with("Directory not empty")
|| (cfg!(windows) && e.to_string().starts_with("The directory is not empty"))
{
let description = format!("cannot remove '{}'", path.display());
let error_message = "Directory not empty";

show_error_custom_description!(description, "{}", error_message);
} else {
show_error!("removing '{}': {}", path.display(), e);
}

return true;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/uucore/src/lib/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ macro_rules! show_error(
})
);

#[macro_export]
macro_rules! show_error_custom_description (
($err:expr,$($args:tt)+) => ({
eprint!("{}: {}: ", executable!(), $err);
eprintln!($($args)+);
})
);

#[macro_export]
macro_rules! show_warning(
($($args:tt)+) => ({
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ fn test_rm_errors() {
);
}

#[test]
fn test_rm_dir_not_empty() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "test_rm_errors_directory";
let file = "test_rm_errors_directory/test_rm_errors_file";

at.mkdir(dir);
at.touch(file);

// rm: cannot remove 'test_rm_errors_directory': Directory not empty
ucmd.arg("-d")
.arg(dir)
.fails()
.stderr_is("rm: cannot remove 'test_rm_errors_directory': Directory not empty\n");
}

#[test]
fn test_rm_verbose() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down