diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 25dd1e6d0f0..3ffcfeff2d7 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(), - root.join(target).quote() + target.join(root.file_name().unwrap()).quote() ) .into()); } diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index c85cf341abb..352a4ca6341 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -2089,3 +2089,19 @@ fn test_copy_directory_to_itself_disallowed() { let expected = "cp: cannot copy a directory, 'd', into itself, 'd\\d'"; ucmd.args(&["-R", "d", "d"]).fails().stderr_only(expected); } + +/// Test that copying a nested directory to itself is disallowed. +#[test] +fn test_copy_nested_directory_to_itself_disallowed() { + let (at, mut ucmd) = at_and_ucmd!(); + at.mkdir("a"); + at.mkdir("a/b"); + at.mkdir("a/b/c"); + #[cfg(not(windows))] + let expected = "cp: cannot copy a directory, 'a/b', into itself, 'a/b/c/b'"; + #[cfg(windows)] + let expected = "cp: cannot copy a directory, 'a\\b', into itself, 'a\\b\\c\\b'"; + ucmd.args(&["-R", "a/b", "a/b/c"]) + .fails() + .stderr_only(expected); +}