From 2ae9cdb2db4e75ca03f7fd9b0f5413a5d447e996 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Tue, 27 Sep 2022 18:03:21 -0400 Subject: [PATCH] fixup! cp: correct error message on copying dir to itself --- src/uu/cp/src/cp.rs | 2 +- tests/by-util/test_cp.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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); +}