diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 3cd0263359..3ccd0575f0 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1265,7 +1265,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult for source in sources { let normalized_source = normalize_path(source); - if seen_sources.contains(&normalized_source) { + if options.backup == BackupMode::NoBackup && seen_sources.contains(&normalized_source) { show_warning!("source file {} specified more than once", source.quote()); } else { let dest = construct_dest_path(source, target, target_type, options) diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 0ecb06bd8f..351ff6a907 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -134,6 +134,33 @@ fn test_cp_duplicate_files_normalized_path() { assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); } +#[test] +fn test_cp_duplicate_files_with_plain_backup() { + let (_, mut ucmd) = at_and_ucmd!(); + ucmd.arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_COPY_TO_FOLDER) + .arg("--backup") + .fails() + // cp would skip duplicate src check and fail when it tries to overwrite the "just-created" file. + .stderr_contains( + "will not overwrite just-created 'hello_dir/hello_world.txt' with 'hello_world.txt", + ); +} + +#[test] +fn test_cp_duplicate_files_with_numbered_backup() { + let (at, mut ucmd) = at_and_ucmd!(); + // cp would skip duplicate src check and succeeds + ucmd.arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_COPY_TO_FOLDER) + .arg("--backup=numbered") + .succeeds(); + at.file_exists(TEST_COPY_TO_FOLDER_FILE); + at.file_exists(format!("{TEST_COPY_TO_FOLDER}.~1~")); +} + #[test] fn test_cp_same_file() { let (at, mut ucmd) = at_and_ucmd!();