1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

cp: handle update prompt with and without interactive mode enabled

This commit is contained in:
Vikrant2691 2024-03-13 23:31:11 +00:00 committed by Ben Wiederhake
parent aeafabd23f
commit 9b9c0cc237
2 changed files with 19 additions and 26 deletions

View file

@ -1543,7 +1543,9 @@ fn handle_existing_dest(
return Err(format!("{} and {} are the same file", source.quote(), dest.quote()).into());
}
options.overwrite.verify(dest)?;
if options.update != UpdateMode::ReplaceIfOlder {
options.overwrite.verify(dest)?;
}
let backup_path = backup_control::get_backup_path(options.backup, dest, &options.backup_suffix);
if let Some(backup_path) = backup_path {
@ -1770,6 +1772,8 @@ fn handle_copy_mode(
if src_time <= dest_time {
return Ok(());
} else {
options.overwrite.verify(dest)?;
copy_helper(
source,
dest,
@ -1866,14 +1870,6 @@ fn copy_file(
copied_files: &mut HashMap<FileInformation, PathBuf>,
source_in_command_line: bool,
) -> CopyResult<()> {
if (options.update == UpdateMode::ReplaceIfOlder || options.update == UpdateMode::ReplaceNone)
&& options.overwrite == OverwriteMode::Interactive(ClobberMode::Standard)
{
// `cp -i --update old new` when `new` exists doesn't copy anything
// and exit with 0
return Ok(());
}
// Fail if dest is a dangling symlink or a symlink this program created previously
if dest.is_symlink() {
if FileInformation::from_path(dest, false)

View file

@ -274,20 +274,6 @@ fn test_cp_target_directory_is_file() {
.stderr_contains(format!("'{TEST_HOW_ARE_YOU_SOURCE}' is not a directory"));
}
#[test]
// FixMe: for FreeBSD, flaky test; track repair progress at GH:uutils/coreutils/issue/4725
#[cfg(not(target_os = "freebsd"))]
fn test_cp_arg_update_interactive() {
new_ucmd!()
.arg(TEST_HELLO_WORLD_SOURCE)
.arg(TEST_HOW_ARE_YOU_SOURCE)
.arg("-i")
.arg("--update")
.succeeds()
.no_stdout()
.no_stderr();
}
#[test]
fn test_cp_arg_update_interactive_error() {
new_ucmd!()
@ -498,7 +484,7 @@ fn test_cp_arg_interactive() {
#[test]
#[cfg(not(any(target_os = "android", target_os = "freebsd")))]
fn test_cp_arg_interactive_update() {
fn test_cp_arg_interactive_update_overwrite_newer() {
// -u -i won't show the prompt to validate the override or not
// Therefore, the error code will be 0
let (at, mut ucmd) = at_and_ucmd!();
@ -517,12 +503,13 @@ fn test_cp_arg_interactive_update() {
#[test]
#[cfg(not(any(target_os = "android", target_os = "freebsd")))]
#[ignore = "known issue #6019"]
fn test_cp_arg_interactive_update_newer() {
fn test_cp_arg_interactive_update_overwrite_older() {
// -u -i *WILL* show the prompt to validate the override.
// Therefore, the error code depends on the prompt response.
// Option N
let (at, mut ucmd) = at_and_ucmd!();
at.touch("b");
std::thread::sleep(Duration::from_secs(1));
at.touch("a");
ucmd.args(&["-i", "-u", "a", "b"])
.pipe_in("N\n")
@ -530,6 +517,16 @@ fn test_cp_arg_interactive_update_newer() {
.code_is(1)
.no_stdout()
.stderr_is("cp: overwrite 'b'? ");
// Option Y
let (at, mut ucmd) = at_and_ucmd!();
at.touch("b");
std::thread::sleep(Duration::from_secs(1));
at.touch("a");
ucmd.args(&["-i", "-u", "a", "b"])
.pipe_in("Y\n")
.succeeds()
.no_stdout();
}
#[test]