From 7e0399e4860cbe050bf1a4f0163b60c90bad2d51 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sun, 25 Sep 2022 17:09:01 -0400 Subject: [PATCH] cp: correct error message on copying dir to itself Correct the error message produced when attempting to copy a directory into itself with `cp`. Before this commit, the error message was $ cp -R d d cp: cannot copy a directory, 'd', into itself, 'd' After this commit, the error message is $ cp -R d d cp: cannot copy a directory, 'd', into itself, 'd/d' --- src/uu/cp/src/cp.rs | 2 +- tests/by-util/test_cp.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 2c5222bc966..25dd1e6d0f0 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1084,7 +1084,7 @@ fn copy_directory( return Err(format!( "cannot copy a directory, {}, into itself, {}", root.quote(), - target.quote() + root.join(target).quote() ) .into()); } diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index d6b00c3445d..c85cf341abb 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -2077,3 +2077,15 @@ fn test_cp_mode_hardlink_no_dereference() { assert!(at.symlink_exists("z")); assert_eq!(at.read_symlink("z"), "slink"); } + +/// Test that copying a directory to itself is disallowed. +#[test] +fn test_copy_directory_to_itself_disallowed() { + let (at, mut ucmd) = at_and_ucmd!(); + at.mkdir("d"); + #[cfg(not(windows))] + let expected = "cp: cannot copy a directory, 'd', into itself, 'd/d'"; + #[cfg(windows)] + let expected = "cp: cannot copy a directory, 'd', into itself, 'd\\d'"; + ucmd.args(&["-R", "d", "d"]).fails().stderr_only(expected); +}