From cd2b56ea9c9822e410f8107e94f0d8a413f8a9d9 Mon Sep 17 00:00:00 2001 From: hz2 Date: Thu, 10 Apr 2025 10:00:39 -0700 Subject: [PATCH] cp: fix update prompting (#7668) * added logic to check if we are updating the file and the destination is newer, to skip copying * corrected logic to handle cp update * - added test case that mocks the issue described in #7660 - adjusted conditions to satisfy clippy * typo * Update src/uu/cp/src/cp.rs Co-authored-by: Daniel Hofstetter * Update tests/by-util/test_cp.rs Co-authored-by: Daniel Hofstetter --------- Co-authored-by: Daniel Hofstetter --- src/uu/cp/src/cp.rs | 7 +++++++ tests/by-util/test_cp.rs | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 90ef8500d..b85f3c8d8 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1828,6 +1828,13 @@ fn handle_existing_dest( return Err(format!("{} and {} are the same file", source.quote(), dest.quote()).into()); } + if options.update == UpdateMode::ReplaceNone { + if options.debug { + println!("skipped {}", dest.quote()); + } + return Err(Error::Skipped(false)); + } + if options.update != UpdateMode::ReplaceIfOlder { options.overwrite.verify(dest, options.debug)?; } diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 2b8404283..9a24792b0 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -6167,3 +6167,20 @@ fn test_cp_update_older_interactive_prompt_no() { .fails() .stdout_is("cp: overwrite 'old'? "); } + +#[test] +fn test_cp_update_none_interactive_prompt_no() { + let (at, mut ucmd) = at_and_ucmd!(); + let old_file = "old"; + let new_file = "new"; + + at.write(old_file, "old content"); + at.write(new_file, "new content"); + + ucmd.args(&["-i", "--update=none", new_file, old_file]) + .succeeds() + .no_output(); + + assert_eq!(at.read(old_file), "old content"); + assert_eq!(at.read(new_file), "new content"); +}