From 184b65df206efce4a2f0289814b4a0f34ac83c92 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 31 Jan 2022 12:10:57 +0100 Subject: [PATCH] uucore: allow backup suffix with hyphen value --- src/uucore/src/lib/mods/backup_control.rs | 10 +++++++++ tests/by-util/test_cp.rs | 18 +++++++++++++++ tests/by-util/test_install.rs | 25 +++++++++++++++++++++ tests/by-util/test_ln.rs | 27 +++++++++++++++++++++++ tests/by-util/test_mv.rs | 21 ++++++++++++++++++ 5 files changed, 101 insertions(+) diff --git a/src/uucore/src/lib/mods/backup_control.rs b/src/uucore/src/lib/mods/backup_control.rs index e1471659179..a2753b964b5 100644 --- a/src/uucore/src/lib/mods/backup_control.rs +++ b/src/uucore/src/lib/mods/backup_control.rs @@ -231,6 +231,7 @@ pub mod arguments { .help("override the usual backup suffix") .takes_value(true) .value_name("SUFFIX") + .allow_hyphen_values(true) } } @@ -618,4 +619,13 @@ mod tests { assert_eq!(result, BackupMode::SimpleBackup); env::remove_var(ENV_VERSION_CONTROL); } + + #[test] + fn test_suffix_takes_hyphen_value() { + let _dummy = TEST_MUTEX.lock().unwrap(); + let matches = make_app().get_matches_from(vec!["app", "-b", "--suffix", "-v"]); + + let result = determine_backup_suffix(&matches); + assert_eq!(result, "-v"); + } } diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 92637dfbe3e..e9b149edef7 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -385,6 +385,24 @@ fn test_cp_arg_suffix() { ); } +#[test] +fn test_cp_arg_suffix_hyphen_value() { + let (at, mut ucmd) = at_and_ucmd!(); + + ucmd.arg(TEST_HELLO_WORLD_SOURCE) + .arg("-b") + .arg("--suffix") + .arg("-v") + .arg(TEST_HOW_ARE_YOU_SOURCE) + .succeeds(); + + assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); + assert_eq!( + at.read(&*format!("{}-v", TEST_HOW_ARE_YOU_SOURCE)), + "How are you?\n" + ); +} + #[test] fn test_cp_custom_backup_suffix_via_env() { let (at, mut ucmd) = at_and_ucmd!(); diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index 97169f93431..23bebf22491 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -815,6 +815,31 @@ fn test_install_backup_short_custom_suffix() { assert!(at.file_exists(&format!("{}{}", file_b, suffix))); } +#[test] +fn test_install_backup_short_custom_suffix_hyphen_value() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let file_a = "test_install_backup_custom_suffix_file_a"; + let file_b = "test_install_backup_custom_suffix_file_b"; + let suffix = "-v"; + + at.touch(file_a); + at.touch(file_b); + scene + .ucmd() + .arg("-b") + .arg(format!("--suffix={}", suffix)) + .arg(file_a) + .arg(file_b) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file_a)); + assert!(at.file_exists(file_b)); + assert!(at.file_exists(&format!("{}{}", file_b, suffix))); +} + #[test] fn test_install_backup_custom_suffix_via_env() { let scene = TestScenario::new(util_name!()); diff --git a/tests/by-util/test_ln.rs b/tests/by-util/test_ln.rs index 9fa73c0bcfd..a2a31464fad 100644 --- a/tests/by-util/test_ln.rs +++ b/tests/by-util/test_ln.rs @@ -180,6 +180,33 @@ fn test_symlink_custom_backup_suffix() { assert_eq!(at.resolve_link(backup), file); } +#[test] +fn test_symlink_custom_backup_suffix_hyphen_value() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_symlink_custom_backup_suffix"; + let link = "test_symlink_custom_backup_suffix_link"; + let suffix = "-v"; + + at.touch(file); + at.symlink_file(file, link); + assert!(at.file_exists(file)); + assert!(at.is_symlink(link)); + assert_eq!(at.resolve_link(link), file); + + let arg = &format!("--suffix={}", suffix); + ucmd.args(&["-b", arg, "-s", file, link]) + .succeeds() + .no_stderr(); + assert!(at.file_exists(file)); + + assert!(at.is_symlink(link)); + assert_eq!(at.resolve_link(link), file); + + let backup = &format!("{}{}", link, suffix); + assert!(at.is_symlink(backup)); + assert_eq!(at.resolve_link(backup), file); +} + #[test] fn test_symlink_backup_numbering() { let (at, mut ucmd) = at_and_ucmd!(); diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index 89f4043f850..a0bd0209d68 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -340,6 +340,27 @@ fn test_mv_custom_backup_suffix() { assert!(at.file_exists(&format!("{}{}", file_b, suffix))); } +#[test] +fn test_mv_custom_backup_suffix_hyphen_value() { + let (at, mut ucmd) = at_and_ucmd!(); + let file_a = "test_mv_custom_backup_suffix_file_a"; + let file_b = "test_mv_custom_backup_suffix_file_b"; + let suffix = "-v"; + + at.touch(file_a); + at.touch(file_b); + ucmd.arg("-b") + .arg(format!("--suffix={}", suffix)) + .arg(file_a) + .arg(file_b) + .succeeds() + .no_stderr(); + + assert!(!at.file_exists(file_a)); + assert!(at.file_exists(file_b)); + assert!(at.file_exists(&format!("{}{}", file_b, suffix))); +} + #[test] fn test_mv_custom_backup_suffix_via_env() { let (at, mut ucmd) = at_and_ucmd!();