Merge branch 'main' into bug/cp-preserve-xattr-9704

This commit is contained in:
nirv 2025-12-21 15:13:48 +05:30 committed by GitHub
commit f7969d7f82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 117 additions and 0 deletions

View file

@ -359,6 +359,14 @@ pub fn determine_backup_mode(matches: &ArgMatches) -> UResult<BackupMode> {
} else {
Ok(BackupMode::Existing)
}
} else if matches.contains_id(arguments::OPT_SUFFIX) {
// Suffix option is enough to determine mode even if --backup is not set.
// If VERSION_CONTROL is not set, the default backup type is 'existing'.
if let Ok(method) = env::var("VERSION_CONTROL") {
match_method(&method, "$VERSION_CONTROL")
} else {
Ok(BackupMode::Existing)
}
} else {
// No option was present at all
Ok(BackupMode::None)
@ -653,6 +661,29 @@ mod tests {
unsafe { env::remove_var(ENV_VERSION_CONTROL) };
}
// Using --suffix without --backup defaults to --backup=existing
#[test]
fn test_backup_mode_suffix_without_backup_option() {
let _dummy = TEST_MUTEX.lock().unwrap();
let matches = make_app().get_matches_from(vec!["command", "--suffix", ".bak"]);
let result = determine_backup_mode(&matches).unwrap();
assert_eq!(result, BackupMode::Existing);
}
// Using --suffix without --backup uses env var if existing
#[test]
fn test_backup_mode_suffix_without_backup_option_with_env_var() {
let _dummy = TEST_MUTEX.lock().unwrap();
unsafe { env::set_var(ENV_VERSION_CONTROL, "numbered") };
let matches = make_app().get_matches_from(vec!["command", "--suffix", ".bak"]);
let result = determine_backup_mode(&matches).unwrap();
assert_eq!(result, BackupMode::Numbered);
}
#[test]
fn test_suffix_takes_hyphen_value() {
let _dummy = TEST_MUTEX.lock().unwrap();

View file

@ -1123,6 +1123,23 @@ fn test_cp_arg_suffix() {
);
}
#[test]
fn test_cp_arg_suffix_without_backup_option() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg(TEST_HELLO_WORLD_SOURCE)
.arg("--suffix")
.arg(".bak")
.arg(TEST_HOW_ARE_YOU_SOURCE)
.succeeds();
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n");
assert_eq!(
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}.bak")),
"How are you?\n"
);
}
#[test]
fn test_cp_arg_suffix_hyphen_value() {
let (at, mut ucmd) = at_and_ucmd!();

View file

@ -1231,6 +1231,30 @@ fn test_install_backup_short_custom_suffix() {
assert!(at.file_exists(format!("{file_b}{suffix}")));
}
#[test]
fn test_install_suffix_without_backup_option() {
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 = "super-suffix-of-the-century";
at.touch(file_a);
at.touch(file_b);
scene
.ucmd()
.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_short_custom_suffix_hyphen_value() {
let scene = TestScenario::new(util_name!());

View file

@ -194,6 +194,31 @@ fn test_symlink_custom_backup_suffix() {
assert_eq!(at.resolve_link(backup), file);
}
#[test]
fn test_symlink_suffix_without_backup_option() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("a", "a\n");
at.write("b", "b2\n");
assert!(at.file_exists("a"));
assert!(at.file_exists("b"));
let suffix = ".sfx";
let suffix_arg = &format!("--suffix={suffix}");
scene
.ucmd()
.args(&["-s", "-f", suffix_arg, "a", "b"])
.succeeds()
.no_stderr();
assert!(at.file_exists("a"));
assert!(at.file_exists("b"));
assert_eq!(at.read("a"), "a\n");
assert_eq!(at.read("b"), "a\n");
// we should have created backup for b file
assert_eq!(at.read(&format!("b{suffix}")), "b2\n");
}
#[test]
fn test_symlink_custom_backup_suffix_hyphen_value() {
let (at, mut ucmd) = at_and_ucmd!();

View file

@ -801,6 +801,26 @@ fn test_mv_custom_backup_suffix() {
assert!(at.file_exists(format!("{file_b}{suffix}")));
}
#[test]
fn test_suffix_without_backup_option() {
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 = "super-suffix-of-the-century";
at.touch(file_a);
at.touch(file_b);
ucmd.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_hyphen_value() {
let (at, mut ucmd) = at_and_ucmd!();