diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index ee598b5642d..98dd6cee17a 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1305,8 +1305,34 @@ fn symlink_file( Ok(()) } +/// Join the source and destination paths with an arrow in between. +/// +/// If the paths are sufficiently long, then they will be surrounded +/// by quotes. +/// +/// # Examples +/// +/// Short paths: +/// +/// ```rust,ignore +/// let src = Path::new("/a/b"); +/// let dest = Path::new("/c/d"); +/// assert_eq!(context_for(src, dest), "/a/b -> /c/d".to_string()); +/// ``` +/// +/// Long paths: +/// +/// ```rust,ignore +/// let src = Path::new("/a/b/c"); +/// let dest = Path::new("/d/e/f"); +/// assert_eq!(context_for(src, dest), "'/a/b/c' -> '/d/e/f'".to_string()); +/// ``` fn context_for(src: &Path, dest: &Path) -> String { - format!("{} -> {}", src.quote(), dest.quote()) + if src.components().count() < 3 { + format!("{} -> {}", src.display(), dest.display()) + } else { + format!("{} -> {}", src.quote(), dest.quote()) + } } /// Implements a simple backup copy for the destination file. diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 5bd64da7cd3..95dbe40611d 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -1368,12 +1368,16 @@ fn test_cp_reflink_insufficient_permission() { .set_permissions(PermissionsExt::from_mode(0o000)) .unwrap(); + // FIXME On GNU cp v8.30, the error message is: + // + // cp: cannot open 'unreadable' for reading: Permission denied + // ucmd.arg("-r") .arg("--reflink=auto") .arg("unreadable") .arg(TEST_EXISTING_FILE) .fails() - .stderr_only("cp: 'unreadable' -> 'existing_file.txt': Permission denied (os error 13)"); + .stderr_only("cp: unreadable -> existing_file.txt: Permission denied (os error 13)"); } #[cfg(any(target_os = "linux", target_os = "android"))]