1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

cp --no-clobber should fail

This commit is contained in:
Sylvestre Ledru 2023-05-31 23:53:21 +02:00
parent 0b4a91744d
commit 4eb1e847e9
2 changed files with 9 additions and 12 deletions

View file

@ -1102,23 +1102,21 @@ fn preserve_hardlinks(
} }
/// When handling errors, we don't always want to show them to the user. This function handles that. /// When handling errors, we don't always want to show them to the user. This function handles that.
/// If the error is printed, returns true, false otherwise. fn show_error_if_needed(error: &Error) {
fn show_error_if_needed(error: &Error) -> bool {
match error { match error {
// When using --no-clobber, we don't want to show // When using --no-clobber, we don't want to show
// an error message // an error message
Error::NotAllFilesCopied => (), Error::NotAllFilesCopied => {
// Need to return an error code
}
Error::Skipped => { Error::Skipped => {
// touch a b && echo "n"|cp -i a b && echo $? // touch a b && echo "n"|cp -i a b && echo $?
// should return an error from GNU 9.2 // should return an error from GNU 9.2
return true;
} }
_ => { _ => {
show_error!("{}", error); show_error!("{}", error);
return true;
} }
} }
false
} }
/// Copy all `sources` to `target`. Returns an /// Copy all `sources` to `target`. Returns an
@ -1175,9 +1173,8 @@ fn copy(sources: &[Source], target: &TargetSlice, options: &Options) -> CopyResu
options, options,
&mut symlinked_files, &mut symlinked_files,
) { ) {
if show_error_if_needed(&error) { show_error_if_needed(&error);
non_fatal_errors = true; non_fatal_errors = true;
}
} }
} }
seen_sources.insert(source); seen_sources.insert(source);

View file

@ -487,7 +487,7 @@ fn test_cp_arg_no_clobber() {
ucmd.arg(TEST_HELLO_WORLD_SOURCE) ucmd.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HOW_ARE_YOU_SOURCE) .arg(TEST_HOW_ARE_YOU_SOURCE)
.arg("--no-clobber") .arg("--no-clobber")
.succeeds(); .fails();
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n"); assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n");
} }
@ -498,7 +498,7 @@ fn test_cp_arg_no_clobber_inferred_arg() {
ucmd.arg(TEST_HELLO_WORLD_SOURCE) ucmd.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HOW_ARE_YOU_SOURCE) .arg(TEST_HOW_ARE_YOU_SOURCE)
.arg("--no-clob") .arg("--no-clob")
.succeeds(); .fails();
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n"); assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n");
} }
@ -525,7 +525,7 @@ fn test_cp_arg_no_clobber_twice() {
.arg("--no-clobber") .arg("--no-clobber")
.arg("source.txt") .arg("source.txt")
.arg("dest.txt") .arg("dest.txt")
.succeeds(); .fails();
assert_eq!(at.read("source.txt"), "some-content"); assert_eq!(at.read("source.txt"), "some-content");
// Should be empty as the "no-clobber" should keep // Should be empty as the "no-clobber" should keep