Skip to content

Commit 939ab03

Browse files
authored
uucore: use --suffix to enable backup mode (#9741)
1 parent 1fca829 commit 939ab03

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

src/uucore/src/lib/features/backup_control.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ pub fn determine_backup_mode(matches: &ArgMatches) -> UResult<BackupMode> {
359359
} else {
360360
Ok(BackupMode::Existing)
361361
}
362+
} else if matches.contains_id(arguments::OPT_SUFFIX) {
363+
// Suffix option is enough to determine mode even if --backup is not set.
364+
// If VERSION_CONTROL is not set, the default backup type is 'existing'.
365+
if let Ok(method) = env::var("VERSION_CONTROL") {
366+
match_method(&method, "$VERSION_CONTROL")
367+
} else {
368+
Ok(BackupMode::Existing)
369+
}
362370
} else {
363371
// No option was present at all
364372
Ok(BackupMode::None)
@@ -653,6 +661,29 @@ mod tests {
653661
unsafe { env::remove_var(ENV_VERSION_CONTROL) };
654662
}
655663

664+
// Using --suffix without --backup defaults to --backup=existing
665+
#[test]
666+
fn test_backup_mode_suffix_without_backup_option() {
667+
let _dummy = TEST_MUTEX.lock().unwrap();
668+
let matches = make_app().get_matches_from(vec!["command", "--suffix", ".bak"]);
669+
670+
let result = determine_backup_mode(&matches).unwrap();
671+
672+
assert_eq!(result, BackupMode::Existing);
673+
}
674+
675+
// Using --suffix without --backup uses env var if existing
676+
#[test]
677+
fn test_backup_mode_suffix_without_backup_option_with_env_var() {
678+
let _dummy = TEST_MUTEX.lock().unwrap();
679+
unsafe { env::set_var(ENV_VERSION_CONTROL, "numbered") };
680+
let matches = make_app().get_matches_from(vec!["command", "--suffix", ".bak"]);
681+
682+
let result = determine_backup_mode(&matches).unwrap();
683+
684+
assert_eq!(result, BackupMode::Numbered);
685+
}
686+
656687
#[test]
657688
fn test_suffix_takes_hyphen_value() {
658689
let _dummy = TEST_MUTEX.lock().unwrap();

tests/by-util/test_cp.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,23 @@ fn test_cp_arg_suffix() {
11231123
);
11241124
}
11251125

1126+
#[test]
1127+
fn test_cp_arg_suffix_without_backup_option() {
1128+
let (at, mut ucmd) = at_and_ucmd!();
1129+
1130+
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
1131+
.arg("--suffix")
1132+
.arg(".bak")
1133+
.arg(TEST_HOW_ARE_YOU_SOURCE)
1134+
.succeeds();
1135+
1136+
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n");
1137+
assert_eq!(
1138+
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}.bak")),
1139+
"How are you?\n"
1140+
);
1141+
}
1142+
11261143
#[test]
11271144
fn test_cp_arg_suffix_hyphen_value() {
11281145
let (at, mut ucmd) = at_and_ucmd!();

tests/by-util/test_install.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,30 @@ fn test_install_backup_short_custom_suffix() {
12311231
assert!(at.file_exists(format!("{file_b}{suffix}")));
12321232
}
12331233

1234+
#[test]
1235+
fn test_install_suffix_without_backup_option() {
1236+
let scene = TestScenario::new(util_name!());
1237+
let at = &scene.fixtures;
1238+
1239+
let file_a = "test_install_backup_custom_suffix_file_a";
1240+
let file_b = "test_install_backup_custom_suffix_file_b";
1241+
let suffix = "super-suffix-of-the-century";
1242+
1243+
at.touch(file_a);
1244+
at.touch(file_b);
1245+
scene
1246+
.ucmd()
1247+
.arg(format!("--suffix={suffix}"))
1248+
.arg(file_a)
1249+
.arg(file_b)
1250+
.succeeds()
1251+
.no_stderr();
1252+
1253+
assert!(at.file_exists(file_a));
1254+
assert!(at.file_exists(file_b));
1255+
assert!(at.file_exists(format!("{file_b}{suffix}")));
1256+
}
1257+
12341258
#[test]
12351259
fn test_install_backup_short_custom_suffix_hyphen_value() {
12361260
let scene = TestScenario::new(util_name!());

tests/by-util/test_ln.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,31 @@ fn test_symlink_custom_backup_suffix() {
194194
assert_eq!(at.resolve_link(backup), file);
195195
}
196196

197+
#[test]
198+
fn test_symlink_suffix_without_backup_option() {
199+
let scene = TestScenario::new(util_name!());
200+
let at = &scene.fixtures;
201+
202+
at.write("a", "a\n");
203+
at.write("b", "b2\n");
204+
205+
assert!(at.file_exists("a"));
206+
assert!(at.file_exists("b"));
207+
let suffix = ".sfx";
208+
let suffix_arg = &format!("--suffix={suffix}");
209+
scene
210+
.ucmd()
211+
.args(&["-s", "-f", suffix_arg, "a", "b"])
212+
.succeeds()
213+
.no_stderr();
214+
assert!(at.file_exists("a"));
215+
assert!(at.file_exists("b"));
216+
assert_eq!(at.read("a"), "a\n");
217+
assert_eq!(at.read("b"), "a\n");
218+
// we should have created backup for b file
219+
assert_eq!(at.read(&format!("b{suffix}")), "b2\n");
220+
}
221+
197222
#[test]
198223
fn test_symlink_custom_backup_suffix_hyphen_value() {
199224
let (at, mut ucmd) = at_and_ucmd!();

tests/by-util/test_mv.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,26 @@ fn test_mv_custom_backup_suffix() {
801801
assert!(at.file_exists(format!("{file_b}{suffix}")));
802802
}
803803

804+
#[test]
805+
fn test_suffix_without_backup_option() {
806+
let (at, mut ucmd) = at_and_ucmd!();
807+
let file_a = "test_mv_custom_backup_suffix_file_a";
808+
let file_b = "test_mv_custom_backup_suffix_file_b";
809+
let suffix = "super-suffix-of-the-century";
810+
811+
at.touch(file_a);
812+
at.touch(file_b);
813+
ucmd.arg(format!("--suffix={suffix}"))
814+
.arg(file_a)
815+
.arg(file_b)
816+
.succeeds()
817+
.no_stderr();
818+
819+
assert!(!at.file_exists(file_a));
820+
assert!(at.file_exists(file_b));
821+
assert!(at.file_exists(format!("{file_b}{suffix}")));
822+
}
823+
804824
#[test]
805825
fn test_mv_custom_backup_suffix_hyphen_value() {
806826
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)