mod common;
#[test]
fn apply_keep_backup_preserved() {
let dir = tempfile::tempdir().expect("tempdir");
let target = dir.path().join("patched.txt");
let original = "line one\nline two\nline three\n";
std::fs::write(&target, original).expect("seed");
let ws = dir.path().to_str().unwrap();
let patch = "--- a/patched.txt\n+++ b/patched.txt\n@@ -1,3 +1,3 @@\n line one\n-line two\n+LINE TWO\n line three\n";
let output = common::atomwrite()
.args([
"--workspace",
ws,
"apply",
"--format",
"unified",
"--backup",
"--keep-backup",
"--retention",
"3",
])
.arg(&target)
.write_stdin(patch)
.output()
.expect("run");
assert!(
output.status.success(),
"apply with --keep-backup failed: stderr={}",
String::from_utf8_lossy(&output.stderr)
);
let content = std::fs::read_to_string(&target).expect("read");
assert!(content.contains("LINE TWO"));
assert!(!content.contains("line two"));
let bak_entries: Vec<_> = std::fs::read_dir(dir.path())
.expect("readdir")
.filter_map(|e| e.ok())
.filter(|e| {
e.file_name()
.to_str()
.is_some_and(|n| n.starts_with("patched.txt.bak."))
})
.collect();
assert_eq!(
bak_entries.len(),
1,
"exactly one .bak.* file must be preserved with --keep-backup"
);
let bak_path = dir.path().join(bak_entries[0].file_name());
let bak_content = std::fs::read_to_string(&bak_path).expect("read bak");
assert_eq!(bak_content, original, "backup must contain pre-patch state");
}