Skip to content

Commit

Permalink
cp: correct error message on copying dir to itself
Browse files Browse the repository at this point in the history
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'
  • Loading branch information
jfinkels authored and sylvestre committed Oct 5, 2022
1 parent e523a56 commit ceb7beb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ fn copy_directory(
return Err(format!(
"cannot copy a directory, {}, into itself, {}",
root.quote(),
target.quote()
root.join(target).quote()
)
.into());
}
Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2095,3 +2095,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);
}

0 comments on commit ceb7beb

Please sign in to comment.