1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

rm: add more tests

This commit is contained in:
Jan Scheer 2021-03-25 23:04:02 +01:00
parent bdf603a65e
commit 61eb4f250d
2 changed files with 61 additions and 25 deletions

View file

@ -233,7 +233,7 @@ fn remove(files: Vec<String>, options: Options) -> bool {
// (e.g., permission), even rm -f should fail with
// outputting the error, but there's no easy eay.
if !options.force {
show_error!("no such file or directory '{}'", filename);
show_error!("cannot remove '{}': No such file or directory", filename);
true
} else {
false
@ -289,7 +289,7 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
had_err = true;
} else {
show_error!(
"could not remove directory '{}' (did you mean to pass '-r' or '-R'?)",
"cannot remove '{}': Is a directory", // GNU's rm error message does not include help
path.display()
);
had_err = true;
@ -326,10 +326,7 @@ fn remove_dir(path: &Path, options: &Options) -> bool {
}
} else {
// called to remove a symlink_dir (windows) without "-r"/"-R" or "-d"
show_error!(
"could not remove directory '{}' (did you mean to pass '-r' or '-R'?)",
path.display()
);
show_error!("cannot remove '{}': Is a directory", path.display());
return true;
}
} else {

View file

@ -17,7 +17,12 @@ fn test_rm_failed() {
let (_at, mut ucmd) = at_and_ucmd!();
let file = "test_rm_one_file";
ucmd.arg(file).fails(); // Doesn't exist
let result = ucmd.arg(file).fails(); // Doesn't exist
assert!(result.stderr.contains(&format!(
"cannot remove '{}': No such file or directory",
file
)));
}
#[test]
@ -115,6 +120,22 @@ fn test_rm_empty_directory() {
assert!(!at.dir_exists(dir));
}
#[test]
fn test_rm_empty_directory_verbose() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "test_rm_empty_directory_verbose";
at.mkdir(dir);
ucmd.arg("-d")
.arg("-v")
.arg(dir)
.succeeds()
.stdout_only(format!("removed directory '{}'\n", dir));
assert!(!at.dir_exists(dir));
}
#[test]
fn test_rm_non_empty_directory() {
let (at, mut ucmd) = at_and_ucmd!();
@ -151,22 +172,17 @@ fn test_rm_recursive() {
}
#[test]
fn test_rm_errors() {
fn test_rm_directory_without_flag() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "test_rm_errors_directory";
let file_a = "test_rm_errors_directory/test_rm_errors_file_a";
let file_b = "test_rm_errors_directory/test_rm_errors_file_b";
let dir = "test_rm_directory_without_flag_dir";
at.mkdir(dir);
at.touch(file_a);
at.touch(file_b);
// $ rm test_rm_errors_directory
// rm: error: could not remove directory 'test_rm_errors_directory' (did you mean to pass '-r'?)
ucmd.arg(dir).fails().stderr_is(
"rm: error: could not remove directory 'test_rm_errors_directory' (did you mean \
to pass '-r' or '-R'?)\n",
);
let result = ucmd.arg(dir).fails();
println!("{}", result.stderr);
assert!(result
.stderr
.contains(&format!("cannot remove '{}': Is a directory", dir)));
}
#[test]
@ -186,18 +202,41 @@ fn test_rm_verbose() {
}
#[test]
fn test_rm_dir_symlink() {
#[cfg(not(windows))]
// on unix symlink_dir is a file
fn test_rm_symlink_dir() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "test_rm_dir_symlink_dir";
let link = "test_rm_dir_symlink_link";
let dir = "test_rm_symlink_dir_directory";
let link = "test_rm_symlink_dir_link";
at.mkdir(dir);
at.symlink_dir(dir, link);
#[cfg(not(windows))]
ucmd.arg(link).succeeds();
#[cfg(windows)]
ucmd.arg("-r").arg(link).succeeds();
}
#[test]
#[cfg(windows)]
// on windows removing symlink_dir requires "-r" or "-d"
fn test_rm_symlink_dir() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let dir = "test_rm_symlink_dir_directory";
let link = "test_rm_symlink_dir_link";
at.mkdir(dir);
at.symlink_dir(dir, link);
let result = scene.ucmd().arg(link).fails();
assert!(result
.stderr
.contains(&format!("cannot remove '{}': Is a directory", link)));
assert!(at.dir_exists(link));
scene.ucmd().arg("-r").arg(link).succeeds();
}
#[test]