| 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 |
// spell-checker:ignore nusr |
| 7 |
|
| 8 |
use uutests::new_ucmd; |
| 9 |
use uutests::path_concat; |
| 10 |
use uutests::util::{TestScenario, get_root_path}; |
| 11 |
use uutests::{at_and_ucmd, util_name}; |
| 12 |
|
| 13 |
#[cfg(windows)] |
| 14 |
use regex::Regex; |
| 15 |
use std::path::{MAIN_SEPARATOR, Path}; |
| 16 |
|
| 17 |
static GIBBERISH: &str = "supercalifragilisticexpialidocious"; |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn test_realpath_current_directory() { |
| 21 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 22 |
let expect = at.root_dir_resolved() + "\n"; |
| 23 |
ucmd.arg(".").succeeds().stdout_is(expect); |
| 24 |
} |
| 25 |
|
| 26 |
#[test] |
| 27 |
fn test_realpath_long_redirection_to_current_dir() { |
| 28 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 29 |
// Create a 256-character path to current directory |
| 30 |
let dir = path_concat!(".", ..128); |
| 31 |
let expect = at.root_dir_resolved() + "\n"; |
| 32 |
ucmd.arg(dir).succeeds().stdout_is(expect); |
| 33 |
} |
| 34 |
|
| 35 |
#[test] |
| 36 |
fn test_realpath_long_redirection_to_root() { |
| 37 |
// Create a 255-character path to root |
| 38 |
let dir = path_concat!("..", ..85); |
| 39 |
let expect = get_root_path().to_owned() + "\n"; |
| 40 |
new_ucmd!().arg(dir).succeeds().stdout_is(expect); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn test_realpath_file_and_links() { |
| 45 |
let scene = TestScenario::new(util_name!()); |
| 46 |
let at = &scene.fixtures; |
| 47 |
|
| 48 |
at.touch("foo"); |
| 49 |
at.symlink_file("foo", "bar"); |
| 50 |
|
| 51 |
scene.ucmd().arg("foo").succeeds().stdout_contains("foo\n"); |
| 52 |
scene.ucmd().arg("bar").succeeds().stdout_contains("foo\n"); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn test_realpath_file_and_links_zero() { |
| 57 |
let scene = TestScenario::new(util_name!()); |
| 58 |
let at = &scene.fixtures; |
| 59 |
|
| 60 |
at.touch("foo"); |
| 61 |
at.symlink_file("foo", "bar"); |
| 62 |
|
| 63 |
scene |
| 64 |
.ucmd() |
| 65 |
.arg("foo") |
| 66 |
.arg("-z") |
| 67 |
.succeeds() |
| 68 |
.stdout_contains("foo\u{0}"); |
| 69 |
|
| 70 |
scene |
| 71 |
.ucmd() |
| 72 |
.arg("bar") |
| 73 |
.arg("-z") |
| 74 |
.succeeds() |
| 75 |
.stdout_contains("foo\u{0}"); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn test_realpath_file_and_links_strip() { |
| 80 |
let strip_args = ["-s", "--strip", "--no-symlinks"]; |
| 81 |
let scene = TestScenario::new(util_name!()); |
| 82 |
let at = &scene.fixtures; |
| 83 |
|
| 84 |
at.touch("foo"); |
| 85 |
at.symlink_file("foo", "bar"); |
| 86 |
|
| 87 |
for strip_arg in strip_args { |
| 88 |
scene |
| 89 |
.ucmd() |
| 90 |
.arg("foo") |
| 91 |
.arg(strip_arg) |
| 92 |
.succeeds() |
| 93 |
.stdout_contains("foo\n"); |
| 94 |
|
| 95 |
scene |
| 96 |
.ucmd() |
| 97 |
.arg("bar") |
| 98 |
.arg(strip_arg) |
| 99 |
.succeeds() |
| 100 |
.stdout_contains("bar\n"); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn test_realpath_file_and_links_strip_zero() { |
| 106 |
let strip_args = ["-s", "--strip", "--no-symlinks"]; |
| 107 |
let scene = TestScenario::new(util_name!()); |
| 108 |
let at = &scene.fixtures; |
| 109 |
|
| 110 |
at.touch("foo"); |
| 111 |
at.symlink_file("foo", "bar"); |
| 112 |
|
| 113 |
for strip_arg in strip_args { |
| 114 |
scene |
| 115 |
.ucmd() |
| 116 |
.arg("foo") |
| 117 |
.arg(strip_arg) |
| 118 |
.arg("-z") |
| 119 |
.succeeds() |
| 120 |
.stdout_contains("foo\u{0}"); |
| 121 |
|
| 122 |
scene |
| 123 |
.ucmd() |
| 124 |
.arg("bar") |
| 125 |
.arg(strip_arg) |
| 126 |
.arg("-z") |
| 127 |
.succeeds() |
| 128 |
.stdout_contains("bar\u{0}"); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn test_realpath_physical_mode() { |
| 134 |
let scene = TestScenario::new(util_name!()); |
| 135 |
let at = &scene.fixtures; |
| 136 |
|
| 137 |
at.mkdir("dir1"); |
| 138 |
at.mkdir_all("dir2/bar"); |
| 139 |
at.symlink_dir("dir2/bar", "dir1/foo"); |
| 140 |
|
| 141 |
scene |
| 142 |
.ucmd() |
| 143 |
.arg("dir1/foo/..") |
| 144 |
.succeeds() |
| 145 |
.stdout_contains("dir2\n"); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn test_realpath_logical_mode() { |
| 150 |
let scene = TestScenario::new(util_name!()); |
| 151 |
let at = &scene.fixtures; |
| 152 |
|
| 153 |
at.mkdir("dir1"); |
| 154 |
at.mkdir("dir2"); |
| 155 |
at.symlink_dir("dir2", "dir1/foo"); |
| 156 |
|
| 157 |
scene |
| 158 |
.ucmd() |
| 159 |
.arg("-L") |
| 160 |
.arg("dir1/foo/..") |
| 161 |
.succeeds() |
| 162 |
.stdout_contains("dir1\n"); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn test_realpath_dangling() { |
| 167 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 168 |
at.symlink_file("nonexistent-file", "link"); |
| 169 |
let output = ucmd.arg("link").succeeds().stdout_move_str(); |
| 170 |
let printed = Path::new(output.trim_end_matches('\n')); |
| 171 |
assert_eq!(printed.file_name().unwrap(), "nonexistent-file"); |
| 172 |
// Canonicalize the printed parent dir before comparing, since on Windows |
| 173 |
// CI the temp dir may be reported using its short (8.3) alias, which is |
| 174 |
// not guaranteed to match the long-form path from `root_dir_resolved()`. |
| 175 |
let printed_dir = printed.parent().unwrap().canonicalize().unwrap(); |
| 176 |
let expect_dir = Path::new(&at.root_dir_resolved()).canonicalize().unwrap(); |
| 177 |
assert_eq!(printed_dir, expect_dir); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn test_realpath_loop() { |
| 182 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 183 |
at.symlink_file("2", "1"); |
| 184 |
at.symlink_file("3", "2"); |
| 185 |
at.symlink_file("1", "3"); |
| 186 |
ucmd.arg("1") |
| 187 |
.fails() |
| 188 |
.stderr_contains("Too many levels of symbolic links"); |
| 189 |
} |
| 190 |
|
| 191 |
#[test] |
| 192 |
fn test_realpath_default_allows_final_non_existent() { |
| 193 |
let p = Path::new("").join(GIBBERISH); |
| 194 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 195 |
let expect = path_concat!(at.root_dir_resolved(), p.to_str().unwrap()) + "\n"; |
| 196 |
ucmd.arg(p.as_os_str()).succeeds().stdout_only(expect); |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn test_realpath_default_forbids_non_final_non_existent() { |
| 201 |
let p = Path::new("").join(GIBBERISH).join(GIBBERISH); |
| 202 |
new_ucmd!().arg(p.to_str().unwrap()).fails(); |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
fn test_realpath_existing() { |
| 207 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 208 |
ucmd.arg("-e") |
| 209 |
.arg(".") |
| 210 |
.succeeds() |
| 211 |
.stdout_only(at.plus_as_string(format!("{}\n", at.root_dir_resolved()))); |
| 212 |
} |
| 213 |
|
| 214 |
#[test] |
| 215 |
fn test_realpath_existing_error() { |
| 216 |
new_ucmd!().arg("-e").arg(GIBBERISH).fails(); |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn test_realpath_existing_error_quiet() { |
| 221 |
new_ucmd!() |
| 222 |
.args(&["-q", "-e", GIBBERISH]) |
| 223 |
.fails_with_code(1) |
| 224 |
.no_output(); |
| 225 |
} |
| 226 |
|
| 227 |
#[test] |
| 228 |
fn test_realpath_missing() { |
| 229 |
let p = Path::new("").join(GIBBERISH).join(GIBBERISH); |
| 230 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 231 |
let expect = path_concat!(at.root_dir_resolved(), p.to_str().unwrap()) + "\n"; |
| 232 |
ucmd.arg("-m") |
| 233 |
.arg(p.as_os_str()) |
| 234 |
.succeeds() |
| 235 |
.stdout_only(expect); |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn test_realpath_when_symlink_is_absolute_and_enoent() { |
| 240 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 241 |
|
| 242 |
at.mkdir("dir2"); |
| 243 |
at.touch("dir2/bar"); |
| 244 |
|
| 245 |
at.mkdir("dir1"); |
| 246 |
at.symlink_file("dir2/bar", "dir1/foo1"); |
| 247 |
at.symlink_file("/dir2/bar", "dir1/foo2"); |
| 248 |
at.relative_symlink_file("../dir2/baz", "dir1/foo3"); |
| 249 |
|
| 250 |
#[cfg(unix)] |
| 251 |
ucmd.arg("dir1/foo1") |
| 252 |
.arg("dir1/foo2") |
| 253 |
.arg("dir1/foo3") |
| 254 |
.fails() |
| 255 |
.stdout_contains("/dir2/bar\n") |
| 256 |
.stdout_contains("/dir2/baz\n") |
| 257 |
.stderr_is("realpath: dir1/foo2: No such file or directory\n"); |
| 258 |
|
| 259 |
#[cfg(windows)] |
| 260 |
ucmd.arg("dir1/foo1") |
| 261 |
.arg("dir1/foo2") |
| 262 |
.arg("dir1/foo3") |
| 263 |
.fails() |
| 264 |
.stdout_contains("\\dir2\\bar\n") |
| 265 |
.stdout_contains("\\dir2\\baz\n") |
| 266 |
.stderr_is("realpath: dir1/foo2: No such file or directory\n"); |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn test_realpath_when_symlink_part_is_missing() { |
| 271 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 272 |
|
| 273 |
at.mkdir("dir2"); |
| 274 |
at.touch("dir2/bar"); |
| 275 |
|
| 276 |
at.mkdir("dir1"); |
| 277 |
at.relative_symlink_file("../dir2/bar", "dir1/foo1"); |
| 278 |
at.relative_symlink_file("dir2/bar", "dir1/foo2"); |
| 279 |
at.relative_symlink_file("../dir2/baz", "dir1/foo3"); |
| 280 |
at.symlink_file("dir3/bar", "dir1/foo4"); |
| 281 |
|
| 282 |
let expect1 = format!("dir2{MAIN_SEPARATOR}bar"); |
| 283 |
let expect2 = format!("dir2{MAIN_SEPARATOR}baz"); |
| 284 |
|
| 285 |
ucmd.args(&["dir1/foo1", "dir1/foo2", "dir1/foo3", "dir1/foo4"]) |
| 286 |
.fails() |
| 287 |
.stdout_contains(expect1 + "\n") |
| 288 |
.stdout_contains(expect2 + "\n") |
| 289 |
.stderr_contains("realpath: dir1/foo2: No such file or directory\n") |
| 290 |
.stderr_contains("realpath: dir1/foo4: No such file or directory\n"); |
| 291 |
} |
| 292 |
|
| 293 |
#[test] |
| 294 |
fn test_relative_existing_require_directories() { |
| 295 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 296 |
at.mkdir("dir1"); |
| 297 |
at.touch("dir1/f"); |
| 298 |
ucmd.args(&["-e", "--relative-base=.", "--relative-to=dir1/f", "."]) |
| 299 |
.fails_with_code(1) |
| 300 |
.stderr_contains("directory"); |
| 301 |
} |
| 302 |
|
| 303 |
#[test] |
| 304 |
fn test_relative_existing_require_directories_2() { |
| 305 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 306 |
at.mkdir("dir1"); |
| 307 |
at.touch("dir1/f"); |
| 308 |
ucmd.args(&["-e", "--relative-base=.", "--relative-to=dir1", "."]) |
| 309 |
.succeeds() |
| 310 |
.stdout_is("..\n"); |
| 311 |
} |
| 312 |
|
| 313 |
#[test] |
| 314 |
fn test_relative_base_not_prefix_of_relative_to() { |
| 315 |
let result = new_ucmd!() |
| 316 |
.args(&[ |
| 317 |
"-sm", |
| 318 |
"--relative-base=/usr/local", |
| 319 |
"--relative-to=/usr", |
| 320 |
"/usr", |
| 321 |
"/usr/local", |
| 322 |
]) |
| 323 |
.succeeds(); |
| 324 |
|
| 325 |
#[cfg(windows)] |
| 326 |
result.stdout_matches(&Regex::new(r"^.*:\\usr\n.*:\\usr\\local\n$").unwrap()); |
| 327 |
|
| 328 |
#[cfg(not(windows))] |
| 329 |
result.stdout_is("/usr\n/usr/local\n"); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn test_relative_string_handling() { |
| 334 |
let result = new_ucmd!() |
| 335 |
.args(&["-m", "--relative-to=prefix", "prefixed/1"]) |
| 336 |
.succeeds(); |
| 337 |
#[cfg(not(windows))] |
| 338 |
result.stdout_is("../prefixed/1\n"); |
| 339 |
#[cfg(windows)] |
| 340 |
result.stdout_is("..\\prefixed\\1\n"); |
| 341 |
|
| 342 |
let result = new_ucmd!() |
| 343 |
.args(&["-m", "--relative-to=prefixed", "prefix/1"]) |
| 344 |
.succeeds(); |
| 345 |
#[cfg(not(windows))] |
| 346 |
result.stdout_is("../prefix/1\n"); |
| 347 |
#[cfg(windows)] |
| 348 |
result.stdout_is("..\\prefix\\1\n"); |
| 349 |
|
| 350 |
new_ucmd!() |
| 351 |
.args(&["-m", "--relative-to=prefixed", "prefixed/1"]) |
| 352 |
.succeeds() |
| 353 |
.stdout_is("1\n"); |
| 354 |
} |
| 355 |
|
| 356 |
#[test] |
| 357 |
fn test_relative() { |
| 358 |
let result = new_ucmd!() |
| 359 |
.args(&[ |
| 360 |
"-sm", |
| 361 |
"--relative-base=/usr", |
| 362 |
"--relative-to=/usr", |
| 363 |
"/tmp", |
| 364 |
"/usr", |
| 365 |
]) |
| 366 |
.succeeds(); |
| 367 |
#[cfg(not(windows))] |
| 368 |
result.stdout_is("/tmp\n.\n"); |
| 369 |
#[cfg(windows)] |
| 370 |
result.stdout_matches(&Regex::new(r"^.*:\\tmp\n\.\n$").unwrap()); |
| 371 |
|
| 372 |
new_ucmd!() |
| 373 |
.args(&["-sm", "--relative-base=/", "--relative-to=/", "/", "/usr"]) |
| 374 |
.succeeds() |
| 375 |
.stdout_is(".\nusr\n"); |
| 376 |
|
| 377 |
let result = new_ucmd!() |
| 378 |
.args(&["-sm", "--relative-base=/usr", "/tmp", "/usr"]) |
| 379 |
.succeeds(); |
| 380 |
#[cfg(not(windows))] |
| 381 |
result.stdout_is("/tmp\n.\n"); |
| 382 |
#[cfg(windows)] |
| 383 |
result.stdout_matches(&Regex::new(r"^.*:\\tmp\n\.\n$").unwrap()); |
| 384 |
|
| 385 |
new_ucmd!() |
| 386 |
.args(&["-sm", "--relative-base=/", "/", "/usr"]) |
| 387 |
.succeeds() |
| 388 |
.stdout_is(".\nusr\n"); |
| 389 |
} |
| 390 |
|
| 391 |
#[test] |
| 392 |
fn test_realpath_trailing_slash() { |
| 393 |
let scene = TestScenario::new(util_name!()); |
| 394 |
let at = &scene.fixtures; |
| 395 |
at.touch("file"); |
| 396 |
at.mkdir("dir"); |
| 397 |
at.relative_symlink_file("file", "link_file"); |
| 398 |
at.relative_symlink_dir("dir", "link_dir"); |
| 399 |
at.relative_symlink_dir("no_dir", "link_no_dir"); |
| 400 |
scene |
| 401 |
.ucmd() |
| 402 |
.arg("link_file") |
| 403 |
.succeeds() |
| 404 |
.stdout_contains(format!("{MAIN_SEPARATOR}file\n")); |
| 405 |
scene.ucmd().arg("link_file/").fails_with_code(1); |
| 406 |
scene |
| 407 |
.ucmd() |
| 408 |
.arg("link_dir") |
| 409 |
.succeeds() |
| 410 |
.stdout_contains(format!("{MAIN_SEPARATOR}dir\n")); |
| 411 |
scene |
| 412 |
.ucmd() |
| 413 |
.arg("link_dir/") |
| 414 |
.succeeds() |
| 415 |
.stdout_contains(format!("{MAIN_SEPARATOR}dir\n")); |
| 416 |
scene |
| 417 |
.ucmd() |
| 418 |
.arg("link_no_dir") |
| 419 |
.succeeds() |
| 420 |
.stdout_contains(format!("{MAIN_SEPARATOR}no_dir\n")); |
| 421 |
scene |
| 422 |
.ucmd() |
| 423 |
.arg("link_no_dir/") |
| 424 |
.succeeds() |
| 425 |
.stdout_contains(format!("{MAIN_SEPARATOR}no_dir\n")); |
| 426 |
scene |
| 427 |
.ucmd() |
| 428 |
.args(&["-e", "link_file"]) |
| 429 |
.succeeds() |
| 430 |
.stdout_contains(format!("{MAIN_SEPARATOR}file\n")); |
| 431 |
scene.ucmd().args(&["-e", "link_file/"]).fails_with_code(1); |
| 432 |
scene |
| 433 |
.ucmd() |
| 434 |
.args(&["-e", "link_dir"]) |
| 435 |
.succeeds() |
| 436 |
.stdout_contains(format!("{MAIN_SEPARATOR}dir\n")); |
| 437 |
scene |
| 438 |
.ucmd() |
| 439 |
.args(&["-e", "link_dir/"]) |
| 440 |
.succeeds() |
| 441 |
.stdout_contains(format!("{MAIN_SEPARATOR}dir\n")); |
| 442 |
scene.ucmd().args(&["-e", "link_no_dir"]).fails_with_code(1); |
| 443 |
scene |
| 444 |
.ucmd() |
| 445 |
.args(&["-e", "link_no_dir/"]) |
| 446 |
.fails_with_code(1); |
| 447 |
scene |
| 448 |
.ucmd() |
| 449 |
.args(&["-m", "link_file"]) |
| 450 |
.succeeds() |
| 451 |
.stdout_contains(format!("{MAIN_SEPARATOR}file\n")); |
| 452 |
scene |
| 453 |
.ucmd() |
| 454 |
.args(&["-m", "link_file/"]) |
| 455 |
.succeeds() |
| 456 |
.stdout_contains(format!("{MAIN_SEPARATOR}file\n")); |
| 457 |
scene |
| 458 |
.ucmd() |
| 459 |
.args(&["-m", "link_dir"]) |
| 460 |
.succeeds() |
| 461 |
.stdout_contains(format!("{MAIN_SEPARATOR}dir\n")); |
| 462 |
scene |
| 463 |
.ucmd() |
| 464 |
.args(&["-m", "link_dir/"]) |
| 465 |
.succeeds() |
| 466 |
.stdout_contains(format!("{MAIN_SEPARATOR}dir\n")); |
| 467 |
scene |
| 468 |
.ucmd() |
| 469 |
.args(&["-m", "link_no_dir"]) |
| 470 |
.succeeds() |
| 471 |
.stdout_contains(format!("{MAIN_SEPARATOR}no_dir\n")); |
| 472 |
scene |
| 473 |
.ucmd() |
| 474 |
.args(&["-m", "link_no_dir/"]) |
| 475 |
.succeeds() |
| 476 |
.stdout_contains(format!("{MAIN_SEPARATOR}no_dir\n")); |
| 477 |
|
| 478 |
scene |
| 479 |
.ucmd() |
| 480 |
.arg("nonexistent/.") |
| 481 |
.fails() |
| 482 |
.stderr_contains("No such file or directory\n"); |
| 483 |
|
| 484 |
scene |
| 485 |
.ucmd() |
| 486 |
.arg("nonexistent/./") |
| 487 |
.fails() |
| 488 |
.stderr_contains("No such file or directory\n"); |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
#[cfg(unix)] |
| 493 |
fn test_realpath_trailing_slash_unreadable_directory() { |
| 494 |
// A trailing slash only asserts that the operand is a directory, which is |
| 495 |
// a stat-level check needing search permission on the parent, not read |
| 496 |
// permission on the directory itself. |
| 497 |
// https://github.com/uutils/coreutils/issues/13151 |
| 498 |
let scene = TestScenario::new(util_name!()); |
| 499 |
let at = &scene.fixtures; |
| 500 |
at.mkdir("dir"); |
| 501 |
at.set_mode("dir", 0o100); |
| 502 |
|
| 503 |
scene |
| 504 |
.ucmd() |
| 505 |
.arg("dir/") |
| 506 |
.succeeds() |
| 507 |
.stdout_contains(format!("{MAIN_SEPARATOR}dir\n")); |
| 508 |
scene |
| 509 |
.ucmd() |
| 510 |
.args(&["-e", "dir/"]) |
| 511 |
.succeeds() |
| 512 |
.stdout_contains(format!("{MAIN_SEPARATOR}dir\n")); |
| 513 |
|
| 514 |
at.set_mode("dir", 0o700); |
| 515 |
} |
| 516 |
|
| 517 |
#[test] |
| 518 |
fn test_realpath_empty() { |
| 519 |
new_ucmd!().fails_with_code(1); |
| 520 |
} |
| 521 |
|
| 522 |
#[test] |
| 523 |
#[cfg(target_os = "linux")] |
| 524 |
fn test_realpath_non_utf8_paths() { |
| 525 |
use std::ffi::OsStr; |
| 526 |
use std::os::unix::ffi::OsStrExt; |
| 527 |
|
| 528 |
let scene = TestScenario::new(util_name!()); |
| 529 |
let at = &scene.fixtures; |
| 530 |
|
| 531 |
// Create a test file with non-UTF-8 bytes in the name |
| 532 |
let non_utf8_bytes = b"test_\xFF\xFE.txt"; |
| 533 |
let non_utf8_name = OsStr::from_bytes(non_utf8_bytes); |
| 534 |
|
| 535 |
at.touch(non_utf8_name); |
| 536 |
let result = scene.ucmd().arg(non_utf8_name).succeeds(); |
| 537 |
|
| 538 |
let output = result.stdout_str_lossy(); |
| 539 |
assert!(output.contains("test_")); |
| 540 |
assert!(output.contains(".txt")); |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn test_realpath_empty_string() { |
| 545 |
// Test that empty string arguments are rejected with exit code 1 |
| 546 |
new_ucmd!().arg("").fails().code_is(1); |
| 547 |
|
| 548 |
// Test that empty --relative-base is rejected |
| 549 |
new_ucmd!() |
| 550 |
.arg("--relative-base=") |
| 551 |
.arg("--relative-to=.") |
| 552 |
.arg(".") |
| 553 |
.fails() |
| 554 |
.code_is(1); |
| 555 |
|
| 556 |
new_ucmd!() |
| 557 |
.arg("--relative-to=") |
| 558 |
.arg(".") |
| 559 |
.fails() |
| 560 |
.code_is(1); |
| 561 |
} |
| 562 |
|
| 563 |
#[test] |
| 564 |
fn test_realpath_canonicalize_options() { |
| 565 |
// Test that default, -E, and --canonicalize all allow nonexistent final component |
| 566 |
let scene = TestScenario::new(util_name!()); |
| 567 |
let at = &scene.fixtures; |
| 568 |
at.mkdir("existing_dir"); |
| 569 |
|
| 570 |
let test_cases = [ |
| 571 |
vec![], // default behavior |
| 572 |
vec!["-E"], // explicit -E flag |
| 573 |
vec!["--canonicalize"], // --canonicalize long form |
| 574 |
]; |
| 575 |
|
| 576 |
#[cfg(windows)] |
| 577 |
let expected_path = "existing_dir\\nonexistent"; |
| 578 |
#[cfg(not(windows))] |
| 579 |
let expected_path = "existing_dir/nonexistent"; |
| 580 |
|
| 581 |
for args in test_cases { |
| 582 |
let mut ucmd = scene.ucmd(); |
| 583 |
for arg in args { |
| 584 |
ucmd.arg(arg); |
| 585 |
} |
| 586 |
ucmd.arg("existing_dir/nonexistent") |
| 587 |
.succeeds() |
| 588 |
.stdout_contains(expected_path); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
#[test] |
| 593 |
fn test_realpath_canonicalize_vs_existing() { |
| 594 |
// Test difference between -E and -e, and option overrides |
| 595 |
let scene = TestScenario::new(util_name!()); |
| 596 |
let at = &scene.fixtures; |
| 597 |
at.mkdir("existing_dir"); |
| 598 |
|
| 599 |
let test_cases = [ |
| 600 |
(vec!["-E"], true), // -E should succeed with nonexistent final component |
| 601 |
(vec!["-e"], false), // -e should fail with nonexistent final component |
| 602 |
(vec!["-e", "-E"], true), // -E should override -e |
| 603 |
]; |
| 604 |
|
| 605 |
#[cfg(windows)] |
| 606 |
let expected_path = "existing_dir\\nonexistent"; |
| 607 |
#[cfg(not(windows))] |
| 608 |
let expected_path = "existing_dir/nonexistent"; |
| 609 |
|
| 610 |
for (args, should_succeed) in test_cases { |
| 611 |
let mut ucmd = scene.ucmd(); |
| 612 |
for arg in args { |
| 613 |
ucmd.arg(arg); |
| 614 |
} |
| 615 |
ucmd.arg("existing_dir/nonexistent"); |
| 616 |
|
| 617 |
if should_succeed { |
| 618 |
ucmd.succeeds().stdout_contains(expected_path); |
| 619 |
} else { |
| 620 |
ucmd.fails(); |
| 621 |
} |
| 622 |
} |
| 623 |
} |