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..5f841bb3767 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -2077,3 +2077,13 @@ 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"); + ucmd.args(&["-R", "d", "d"]) + .fails() + .stderr_only("cp: cannot copy a directory, 'd', into itself, 'd/d'"); +}