| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
use uutests::at_and_ucmd; |
| 7 |
use uutests::new_ucmd; |
| 8 |
|
| 9 |
const DIR: &str = "dir"; |
| 10 |
const DIR_FILE: &str = "dir/file"; |
| 11 |
const NESTED_DIR: &str = "dir/ect/ory"; |
| 12 |
const NESTED_DIR_FILE: &str = "dir/ect/ory/file"; |
| 13 |
|
| 14 |
#[cfg(windows)] |
| 15 |
const NOT_FOUND: &str = "The system cannot find the file specified."; |
| 16 |
#[cfg(not(windows))] |
| 17 |
const NOT_FOUND: &str = "No such file or directory"; |
| 18 |
|
| 19 |
#[cfg(windows)] |
| 20 |
const NOT_EMPTY: &str = "The directory is not empty."; |
| 21 |
#[cfg(not(windows))] |
| 22 |
const NOT_EMPTY: &str = "Directory not empty"; |
| 23 |
|
| 24 |
#[cfg(windows)] |
| 25 |
const NOT_A_DIRECTORY: &str = "The directory name is invalid."; |
| 26 |
#[cfg(not(windows))] |
| 27 |
const NOT_A_DIRECTORY: &str = "Not a directory"; |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn test_invalid_arg() { |
| 31 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 32 |
} |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn test_no_arg() { |
| 36 |
new_ucmd!() |
| 37 |
.fails_with_code(1) |
| 38 |
.stderr_contains("error: the following required arguments were not provided:"); |
| 39 |
} |
| 40 |
|
| 41 |
#[test] |
| 42 |
fn test_rmdir_empty_directory_no_parents() { |
| 43 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 44 |
|
| 45 |
at.mkdir(DIR); |
| 46 |
|
| 47 |
ucmd.arg(DIR).succeeds().no_stderr(); |
| 48 |
|
| 49 |
assert!(!at.dir_exists(DIR)); |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn test_rmdir_empty_directory_with_parents() { |
| 54 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 55 |
|
| 56 |
at.mkdir_all(NESTED_DIR); |
| 57 |
|
| 58 |
ucmd.arg("-p").arg(NESTED_DIR).succeeds().no_stderr(); |
| 59 |
|
| 60 |
assert!(!at.dir_exists(NESTED_DIR)); |
| 61 |
assert!(!at.dir_exists(DIR)); |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn test_rmdir_nonempty_directory_no_parents() { |
| 66 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 67 |
|
| 68 |
at.mkdir(DIR); |
| 69 |
at.touch(DIR_FILE); |
| 70 |
|
| 71 |
ucmd.arg(DIR) |
| 72 |
.fails() |
| 73 |
.stderr_is(format!("rmdir: failed to remove 'dir': {NOT_EMPTY}\n")); |
| 74 |
|
| 75 |
assert!(at.dir_exists(DIR)); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn test_rmdir_nonempty_directory_with_parents() { |
| 80 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 81 |
|
| 82 |
at.mkdir_all(NESTED_DIR); |
| 83 |
at.touch(NESTED_DIR_FILE); |
| 84 |
|
| 85 |
ucmd.arg("-p").arg(NESTED_DIR).fails().stderr_is(format!( |
| 86 |
"rmdir: failed to remove 'dir/ect/ory': {NOT_EMPTY}\n" |
| 87 |
)); |
| 88 |
|
| 89 |
assert!(at.dir_exists(NESTED_DIR)); |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn test_rmdir_ignore_nonempty_directory_no_parents() { |
| 94 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 95 |
|
| 96 |
at.mkdir(DIR); |
| 97 |
at.touch(DIR_FILE); |
| 98 |
|
| 99 |
ucmd.arg("--ignore-fail-on-non-empty") |
| 100 |
.arg(DIR) |
| 101 |
.succeeds() |
| 102 |
.no_stderr(); |
| 103 |
|
| 104 |
assert!(at.dir_exists(DIR)); |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn test_rmdir_ignore_nonempty_directory_with_parents() { |
| 109 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 110 |
|
| 111 |
at.mkdir_all(NESTED_DIR); |
| 112 |
at.touch(NESTED_DIR_FILE); |
| 113 |
|
| 114 |
ucmd.arg("--ignore-fail-on-non-empty") |
| 115 |
.arg("-p") |
| 116 |
.arg(NESTED_DIR) |
| 117 |
.succeeds() |
| 118 |
.no_stderr(); |
| 119 |
|
| 120 |
assert!(at.dir_exists(NESTED_DIR)); |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn test_rmdir_not_a_directory() { |
| 125 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 126 |
|
| 127 |
at.touch("file"); |
| 128 |
|
| 129 |
ucmd.arg("--ignore-fail-on-non-empty") |
| 130 |
.arg("file") |
| 131 |
.fails() |
| 132 |
.no_stdout() |
| 133 |
.stderr_is(format!( |
| 134 |
"rmdir: failed to remove 'file': {NOT_A_DIRECTORY}\n" |
| 135 |
)); |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
fn test_verbose_single() { |
| 140 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 141 |
|
| 142 |
at.mkdir(DIR); |
| 143 |
|
| 144 |
ucmd.arg("-v") |
| 145 |
.arg(DIR) |
| 146 |
.succeeds() |
| 147 |
.no_stderr() |
| 148 |
.stdout_is("rmdir: removing directory, 'dir'\n"); |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn test_verbose_multi() { |
| 153 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 154 |
|
| 155 |
at.mkdir(DIR); |
| 156 |
|
| 157 |
ucmd.arg("-v") |
| 158 |
.arg("does_not_exist") |
| 159 |
.arg(DIR) |
| 160 |
.fails() |
| 161 |
.stdout_is( |
| 162 |
"rmdir: removing directory, 'does_not_exist'\n\ |
| 163 |
rmdir: removing directory, 'dir'\n", |
| 164 |
) |
| 165 |
.stderr_is(format!( |
| 166 |
"rmdir: failed to remove 'does_not_exist': {NOT_FOUND}\n" |
| 167 |
)); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_verbose_nested_failure() { |
| 172 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 173 |
|
| 174 |
at.mkdir_all(NESTED_DIR); |
| 175 |
at.touch("dir/ect/file"); |
| 176 |
|
| 177 |
ucmd.arg("-pv") |
| 178 |
.arg(NESTED_DIR) |
| 179 |
.fails() |
| 180 |
.stdout_is( |
| 181 |
"rmdir: removing directory, 'dir/ect/ory'\n\ |
| 182 |
rmdir: removing directory, 'dir/ect'\n", |
| 183 |
) |
| 184 |
.stderr_is(format!("rmdir: failed to remove 'dir/ect': {NOT_EMPTY}\n")); |
| 185 |
} |
| 186 |
|
| 187 |
#[cfg(unix)] |
| 188 |
#[test] |
| 189 |
fn test_rmdir_ignore_nonempty_no_permissions() { |
| 190 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 191 |
|
| 192 |
// We make the *parent* dir read-only to prevent deleting the dir in it. |
| 193 |
at.mkdir_all("dir/ect/ory"); |
| 194 |
at.touch("dir/ect/ory/file"); |
| 195 |
at.set_mode("dir/ect", 0o555); |
| 196 |
|
| 197 |
// rmdir should now get a permissions error that it interprets as |
| 198 |
// a non-empty error. |
| 199 |
ucmd.arg("--ignore-fail-on-non-empty") |
| 200 |
.arg("dir/ect/ory") |
| 201 |
.succeeds() |
| 202 |
.no_stderr(); |
| 203 |
|
| 204 |
assert!(at.dir_exists("dir/ect/ory")); |
| 205 |
|
| 206 |
// Politely restore permissions for cleanup |
| 207 |
at.set_mode("dir/ect", 0o755); |
| 208 |
} |
| 209 |
|
| 210 |
#[test] |
| 211 |
fn test_rmdir_remove_symlink_file() { |
| 212 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 213 |
|
| 214 |
at.touch("file"); |
| 215 |
at.symlink_file("file", "fl"); |
| 216 |
|
| 217 |
ucmd.arg("fl/").fails().stderr_is(format!( |
| 218 |
"rmdir: failed to remove 'fl/': {NOT_A_DIRECTORY}\n" |
| 219 |
)); |
| 220 |
} |
| 221 |
|
| 222 |
// This behavior is known to happen on Linux but not all Unixes |
| 223 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 224 |
#[test] |
| 225 |
fn test_rmdir_remove_symlink_dir() { |
| 226 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 227 |
|
| 228 |
at.mkdir("dir"); |
| 229 |
at.symlink_dir("dir", "dl"); |
| 230 |
|
| 231 |
ucmd.arg("dl/") |
| 232 |
.fails() |
| 233 |
.stderr_is("rmdir: failed to remove 'dl/': Symbolic link not followed\n"); |
| 234 |
} |
| 235 |
|
| 236 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 237 |
#[test] |
| 238 |
fn test_rmdir_remove_symlink_dangling() { |
| 239 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 240 |
|
| 241 |
at.symlink_dir("dir", "dl"); |
| 242 |
|
| 243 |
ucmd.arg("dl/") |
| 244 |
.fails() |
| 245 |
.stderr_is("rmdir: failed to remove 'dl/': Symbolic link not followed\n"); |
| 246 |
} |
| 247 |
|
| 248 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 249 |
#[test] |
| 250 |
fn test_rmdir_remove_symlink_dir_with_trailing_slashes() { |
| 251 |
// a symlink with trailing slashes should still be printing the 'Symbolic link not followed' |
| 252 |
// message |
| 253 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 254 |
|
| 255 |
at.mkdir("dir"); |
| 256 |
at.symlink_dir("dir", "dl"); |
| 257 |
|
| 258 |
ucmd.arg("dl////") |
| 259 |
.fails() |
| 260 |
.stderr_is("rmdir: failed to remove 'dl////': Symbolic link not followed\n"); |
| 261 |
} |