diff options
| author | tv <tv@krebsco.de> | 2026-11-18 11:14:56 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-18 12:23:19 +0100 |
| commit | ecfed5d7e344c617f582d1d91c7395a210a70192 (patch) | |
| tree | b3bd6e23037220d641aa7ffb500dfa60b12ef2a7 /src/files.rs | |
| parent | b6a2649f54741e88a886fe8f64e916415e8a4b72 (diff) | |
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.
Diffstat (limited to 'src/files.rs')
| -rw-r--r-- | src/files.rs | 29 |
1 files changed, 24 insertions, 5 deletions
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-<name>-<process ID>` 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(); } } |
