From c53b5dbce54acf7ea539ffa102d8a56d23d91e65 Mon Sep 17 00:00:00 2001 From: id3v1669 Date: Sat, 30 Aug 2025 15:58:50 +0800 Subject: [PATCH] mv: symlinks as files, tests --- tests/by-util/test_mv.rs | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index 23d7315fb..7741fa8ca 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -398,6 +398,78 @@ fn test_mv_replace_file() { assert!(at.file_exists(file_b)); } +#[test] +#[cfg(all(unix, not(target_os = "android")))] +fn test_mv_replace_symlink_with_symlink() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir("a"); + at.mkdir("b"); + at.touch("a/empty_file_a"); + at.touch("b/empty_file_b"); + + at.symlink_dir("a", "symlink_a"); + at.symlink_dir("b", "symlink_b"); + + assert_eq!(at.read("symlink_a/empty_file_a"), ""); + + ucmd.arg("-T") + .arg("symlink_b") + .arg("symlink_a") + .succeeds() + .no_stderr(); + + assert!(at.file_exists("symlink_a/empty_file_b")); + assert!(!at.file_exists("symlink_a/empty_file_a")); + assert!(!at.symlink_exists("symlink_b")); +} + +#[test] +#[cfg(all(unix, not(target_os = "android")))] +fn test_mv_replace_symlink_with_directory() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir("a"); + at.mkdir("b"); + at.touch("a/empty_file_a"); + at.touch("b/empty_file_b"); + + at.symlink_dir("a", "symlink"); + + ucmd.arg("-T") + .arg("b") + .arg("symlink") + .fails() + .stderr_contains("cannot overwrite non-directory") + .stderr_contains("with directory"); +} + +#[test] +#[cfg(all(unix, not(target_os = "android")))] +fn test_mv_replace_symlink_with_file() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.mkdir("a"); + at.touch("a/empty_file_a"); + at.touch("empty_file_b"); + + at.symlink_dir("a", "symlink"); + + assert!(at.file_exists("symlink/empty_file_a")); + + ucmd.arg("-T") + .arg("empty_file_b") + .arg("symlink") + .succeeds() + .no_stderr(); + + assert!(at.file_exists("symlink")); + assert!(!at.is_symlink("symlink")); + assert!(!at.file_exists("empty_file_b")); + assert!(at.dir_exists("a")); + assert!(at.file_exists("a/empty_file_a")); +} + #[test] fn test_mv_force_replace_file() { let (at, mut ucmd) = at_and_ucmd!();