From ed3ff105400df0221a3b904143df1e17f8185405 Mon Sep 17 00:00:00 2001 From: John Shin Date: Sun, 30 Apr 2023 18:52:13 -0700 Subject: [PATCH] cp: write tests for --update --- src/uu/mv/src/mv.rs | 16 +++-- tests/by-util/test_cp.rs | 127 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 6 deletions(-) diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index 252446600..2edf91624 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -427,6 +427,16 @@ fn rename( return Ok(()); } + if b.update == UpdateMode::ReplaceNone { + return Ok(()); + } + + if (b.update == UpdateMode::ReplaceIfOlder) + && fs::metadata(from)?.modified()? <= fs::metadata(to)?.modified()? + { + return Ok(()); + } + match b.overwrite { OverwriteMode::NoClobber => return Ok(()), OverwriteMode::Interactive => { @@ -441,12 +451,6 @@ fn rename( if let Some(ref backup_path) = backup_path { rename_with_fallback(to, backup_path, multi_progress)?; } - - if (b.update == UpdateMode::ReplaceIfOlder) - && fs::metadata(from)?.modified()? <= fs::metadata(to)?.modified()? - { - return Ok(()); - } } // "to" may no longer exist if it was backed up diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index dfbbc1473..4e241e064 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -244,6 +244,133 @@ fn test_cp_arg_update_interactive_error() { .no_stdout(); } +#[test] +fn test_cp_arg_update_none() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .arg("--update=none") + .succeeds() + .no_stderr() + .no_stdout(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n") +} + +#[test] +fn test_cp_arg_update_all() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg(TEST_HELLO_WORLD_SOURCE) + .arg(TEST_HOW_ARE_YOU_SOURCE) + .arg("--update=all") + .succeeds() + .no_stderr() + .no_stdout(); + + assert_eq!( + at.read(TEST_HOW_ARE_YOU_SOURCE), + at.read(TEST_HELLO_WORLD_SOURCE) + ) +} + +#[test] +fn test_cp_arg_update_older_dest_not_older_than_src() { + let (at, mut ucmd) = at_and_ucmd!(); + + let old = "test_cp_arg_update_oldler_file1"; + let new = "test_cp_arg_update_oldler_file2"; + + at.touch(old); + sleep(Duration::from_secs(1)); + at.touch(new); + + at.append(old, "old content\n"); + at.append(new, "new content\n"); + + ucmd.arg(old) + .arg(new) + .arg("--update=older") + .succeeds() + .no_stderr() + .no_stdout(); + + assert_eq!(at.read(new), "new content\n") +} + +#[test] +fn test_cp_arg_update_older_dest_older_than_src() { + let (at, mut ucmd) = at_and_ucmd!(); + + let old = "test_cp_arg_update_oldler_file1"; + let new = "test_cp_arg_update_oldler_file2"; + + at.touch(old); + at.append(old, "old content\n"); + sleep(Duration::from_secs(1)); + at.touch(new); + at.append(new, "new content\n"); + + ucmd.arg(new) + .arg(old) + .arg("--update=older") + .succeeds() + .no_stderr() + .no_stdout(); + + assert_eq!(at.read(old), "new content\n") +} + +#[test] +fn test_cp_arg_update_short_fail() { + // same as --update=older + let (at, mut ucmd) = at_and_ucmd!(); + + let old = "test_cp_arg_update_oldler_file1"; + let new = "test_cp_arg_update_oldler_file2"; + + at.touch(old); + sleep(Duration::from_secs(1)); + at.touch(new); + + at.append(old, "old content\n"); + at.append(new, "new content\n"); + + ucmd.arg(old) + .arg(new) + .arg("-u") + .succeeds() + .no_stderr() + .no_stdout(); + + assert_eq!(at.read(new), "new content\n") +} + +#[test] +fn test_cp_arg_update_short_succeed() { + // same as --update=older + let (at, mut ucmd) = at_and_ucmd!(); + + let old = "test_cp_arg_update_oldler_file1"; + let new = "test_cp_arg_update_oldler_file2"; + + at.touch(old); + at.touch(new); + + at.append(old, "old content\n"); + at.append(new, "new content\n"); + + ucmd.arg(new) + .arg(old) + .arg("-u") + .succeeds() + .no_stderr() + .no_stdout(); + + assert_eq!(at.read(new), "new content\n") +} + #[test] fn test_cp_arg_interactive() { let (at, mut ucmd) = at_and_ucmd!();