1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #6825 from matrixhead/dup-source

cp: normalize path when checking for duplicate source
This commit is contained in:
Daniel Hofstetter 2024-10-31 11:26:35 +01:00 committed by GitHub
commit d1f2534811
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 5 deletions

View file

@ -121,6 +121,60 @@ 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!();
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
.arg(format!("./{TEST_HELLO_WORLD_SOURCE}"))
.arg(TEST_COPY_TO_FOLDER)
.succeeds()
.stderr_contains(format!(
"source file './{TEST_HELLO_WORLD_SOURCE}' specified more than once"
));
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!();