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); }