diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 3ccd0575f..32168b090 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1266,7 +1266,15 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult for source in sources { let normalized_source = normalize_path(source); if options.backup == BackupMode::NoBackup && seen_sources.contains(&normalized_source) { - show_warning!("source file {} specified more than once", source.quote()); + let file_type = if source.symlink_metadata()?.file_type().is_dir() { + "directory" + } else { + "file" + }; + show_warning!( + "source {file_type} {} specified more than once", + source.quote() + ); } else { let dest = construct_dest_path(source, target, target_type, options) .unwrap_or_else(|_| target.to_path_buf()); diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 351ff6a90..df94bb28c 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -120,7 +120,19 @@ fn test_cp_duplicate_files() { )); assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); } - +#[test] +fn test_cp_duplicate_folder() { + let (at, mut ucmd) = at_and_ucmd!(); + ucmd.arg("-r") + .arg(TEST_COPY_FROM_FOLDER) + .arg(TEST_COPY_FROM_FOLDER) + .arg(TEST_COPY_TO_FOLDER) + .succeeds() + .stderr_contains(format!( + "source directory '{TEST_COPY_FROM_FOLDER}' specified more than once" + )); + assert!(at.dir_exists(format!("{TEST_COPY_TO_FOLDER}/{TEST_COPY_FROM_FOLDER}").as_str())); +} #[test] fn test_cp_duplicate_files_normalized_path() { let (at, mut ucmd) = at_and_ucmd!();