From e0ed7e9a1de8f002fb9a6aa26e0e941a9622ca9a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 21:18:25 +0100 Subject: [PATCH] thing-testkit: each fixture open has its own temp directory; the read-only test restores permissions on drop Co-Authored-By: Claude Sonnet 5.5 --- crates/thing-testkit/src/fixture.rs | 7 +++++-- crates/thing-testkit/tests/fixtures.rs | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/thing-testkit/src/fixture.rs b/crates/thing-testkit/src/fixture.rs index d982071..07eaa10 100644 --- a/crates/thing-testkit/src/fixture.rs +++ b/crates/thing-testkit/src/fixture.rs @@ -84,9 +84,12 @@ impl Fixture { /// The committed store, opened through the contract. Opening creates a lock file beside the - /// store, so this opens a copy: the fixtures directory is never written. + /// store, so this opens a copy in a directory of its own (process id and a serial), removed + /// once the store is read: the fixtures directory is never written. pub fn open(self) -> CultCache { - let work = std::env::temp_dir().join(format!("thing-testkit-open-{}-{}", std::process::id(), self.file_name())); + static OPENS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let serial = OPENS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let work = std::env::temp_dir().join(format!("thing-testkit-open-{}-{serial}", std::process::id())); std::fs::create_dir_all(&work).unwrap(); let copy = work.join(self.file_name()); std::fs::copy(Self::dir().join(self.file_name()), ©).unwrap(); diff --git a/crates/thing-testkit/tests/fixtures.rs b/crates/thing-testkit/tests/fixtures.rs index 7508e7b..73c0e00 100644 --- a/crates/thing-testkit/tests/fixtures.rs +++ b/crates/thing-testkit/tests/fixtures.rs @@ -175,13 +175,19 @@ fn loading_a_fixture_writes_nothing_in_the_fixtures_directory() { names }; let before = listing(); + // Restores the directory's permissions even when the test panics. + struct Restore(std::fs::Permissions); + impl Drop for Restore { + fn drop(&mut self) { + let _ = std::fs::set_permissions(Fixture::dir(), self.0.clone()); + } + } let mut permissions = std::fs::metadata(Fixture::dir()).unwrap().permissions(); - let writable = permissions.clone(); + let _restore = Restore(permissions.clone()); permissions.set_readonly(true); std::fs::set_permissions(Fixture::dir(), permissions).unwrap(); for fixture in Fixture::ALL { load(fixture); } - std::fs::set_permissions(Fixture::dir(), writable).unwrap(); assert_eq!(listing(), before); }