Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M13: isolate automation workdirs by call site (#1223)

cyfung1031b84ab4507aa2
diff --git a/crates/automation/tests/automation.rs b/crates/automation/tests/automation.rs
index 30765cc02..5f76908af 100644
--- a/crates/automation/tests/automation.rs
+++ b/crates/automation/tests/automation.rs
@@ -36,8 +36,10 @@ fn fixture(n: usize) -> Vec<u8> {
}
/// A fresh directory with `a.pdf` (3 pages) and `b.pdf` (2 pages).
+#[track_caller]
fn workdir(test: &str) -> PathBuf {
- let dir = std::env::temp_dir().join(format!("pdfcraft-automation-{test}-{}", std::process::id()));
+ let caller = std::panic::Location::caller();
+ let dir = std::env::temp_dir().join(format!("pdfcraft-automation-{test}-{}-{}-{}", std::process::id(), caller.line(), caller.column(),));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("a.pdf"), fixture(3)).unwrap();
@@ -45,6 +47,33 @@ fn workdir(test: &str) -> PathBuf {
dir
}
+#[test]
+fn workdir_call_sites_do_not_delete_each_other() {
+ let first = workdir("same-tag");
+ let sentinel = first.join("sentinel");
+ std::fs::write(&sentinel, b"first call site").unwrap();
+
+ let second = workdir("same-tag");
+ let paths_differ = first != second;
+ let sentinel_bytes = std::fs::read(&sentinel).ok();
+ let first_a = std::fs::read(first.join("a.pdf")).ok();
+ let first_b = std::fs::read(first.join("b.pdf")).ok();
+ let second_a = std::fs::read(second.join("a.pdf")).ok();
+ let second_b = std::fs::read(second.join("b.pdf")).ok();
+
+ let _ = std::fs::remove_dir_all(&first);
+ if paths_differ {
+ let _ = std::fs::remove_dir_all(&second);
+ }
+
+ assert!(paths_differ, "distinct call sites shared {first:?}");
+ assert_eq!(sentinel_bytes.as_deref(), Some(&b"first call site"[..]));
+ assert_eq!(first_a, Some(fixture(3)));
+ assert_eq!(first_b, Some(fixture(2)));
+ assert_eq!(second_a, Some(fixture(3)));
+ assert_eq!(second_b, Some(fixture(2)));
+}
+
fn auto(dir: &Path) -> Automation {
Automation::new().with_root(dir).unwrap().with_clock(|| 1_700_000_000)
}