From ecfed5d7e344c617f582d1d91c7395a210a70192 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 18 Nov 2026 11:14:56 +0000 Subject: files: a test's scratch directory goes when the test ends `scratch_dir` gives a guard (`Scratch`) that removes the directory with all it holds when dropped, the test passing or failing. Each test keeps it for as long as it uses the directory; the dashboard's browser check writes its page into one. --- src/files.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index 3e27066..320543f 100644 --- a/src/files.rs +++ b/src/files.rs @@ -261,13 +261,34 @@ fn with_suffix(path: &Path, suffix: &str) -> PathBuf { } /// An empty directory for the test `name`: `continuum--` in the -/// temporary directory, emptied first. +/// temporary directory, emptied first, and removed with all it holds once dropped, the +/// test passing or failing. #[cfg(test)] -pub(crate) fn scratch_dir(name: &str) -> PathBuf { +pub(crate) fn scratch_dir(name: &str) -> Scratch { let dir = std::env::temp_dir().join(format!("continuum-{name}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).unwrap(); - dir + Scratch(dir) +} + +/// A test's directory (`scratch_dir`), removed when dropped. +#[cfg(test)] +pub(crate) struct Scratch(PathBuf); + +#[cfg(test)] +impl std::ops::Deref for Scratch { + type Target = Path; + + fn deref(&self) -> &Path { + &self.0 + } +} + +#[cfg(test)] +impl Drop for Scratch { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } } #[cfg(test)] @@ -302,7 +323,5 @@ mod tests { ); assert_eq!(files.counts()[0].1.appended_bytes, 1); assert_eq!(files.counts()[1].1.reads, 0); - - std::fs::remove_dir_all(&dir).unwrap(); } } -- cgit v1.3.1