From 88d9d46abc16e8d7eb69dc100f92ece37c9d0abc Mon Sep 17 00:00:00 2001 From: Anirban Halder Date: Wed, 8 May 2024 17:51:00 +0530 Subject: [PATCH] Added test for dangling symlink in target directory --- src/uu/cp/src/cp.rs | 3 ++- tests/by-util/test_cp.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 69577a012..e44c52507 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1225,8 +1225,9 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult ) { show_error_if_needed(&error); non_fatal_errors = true; + } else { + copied_destinations.insert(dest.clone()); } - copied_destinations.insert(dest.clone()); } seen_sources.insert(source); } diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index f9ff5583d..ee226ed27 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -2691,8 +2691,40 @@ fn test_cp_symlink_overwrite_detection() { "cp: will not copy 'good/README' through just-created symlink 'tmp/README'\n" }); let contents = at.read("tmp/foo"); - assert_eq!(contents, "file1"); + // None of the files seem to be copied in macos + if cfg!(not(target_os = "macos")) { + assert_eq!(contents, "file1"); + } } + +#[test] +fn test_cp_dangling_symlink_inside_directory() { + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + + at.mkdir("good"); + at.mkdir("tmp"); + at.write("README", "file1"); + at.write("good/README", "file2"); + + ts.ccmd("ln") + .arg("-s") + .arg("foo") + .arg("tmp/README") + .succeeds(); + + ts.ucmd() + .arg("README") + .arg("good/README") + .arg("tmp") + .fails() + .stderr_only( if cfg!(target_os="windows") { + "cp: not writing through dangling symlink 'tmp\\README'\ncp: not writing through dangling symlink 'tmp\\README'\n" + } else { + "cp: not writing through dangling symlink 'tmp/README'\ncp: not writing through dangling symlink 'tmp/README'\n" + } ); +} + /// Test for copying a dangling symbolic link and its permissions. #[cfg(not(target_os = "freebsd"))] // FIXME: fix this test for FreeBSD #[test]