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

cp: write tests for --update

This commit is contained in:
John Shin 2023-04-30 18:52:13 -07:00
parent 2f8df653c5
commit ed3ff10540
2 changed files with 137 additions and 6 deletions

View file

@ -427,6 +427,16 @@ fn rename(
return Ok(()); 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 { match b.overwrite {
OverwriteMode::NoClobber => return Ok(()), OverwriteMode::NoClobber => return Ok(()),
OverwriteMode::Interactive => { OverwriteMode::Interactive => {
@ -441,12 +451,6 @@ fn rename(
if let Some(ref backup_path) = backup_path { if let Some(ref backup_path) = backup_path {
rename_with_fallback(to, backup_path, multi_progress)?; 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 // "to" may no longer exist if it was backed up

View file

@ -244,6 +244,133 @@ fn test_cp_arg_update_interactive_error() {
.no_stdout(); .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] #[test]
fn test_cp_arg_interactive() { fn test_cp_arg_interactive() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();