diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/dashboard/mod.rs | 5 | ||||
| -rw-r--r-- | src/federation/held.rs | 3 | ||||
| -rw-r--r-- | src/files.rs | 29 | ||||
| -rw-r--r-- | src/identity.rs | 6 | ||||
| -rw-r--r-- | src/swarm/auth.rs | 3 |
5 files changed, 34 insertions, 12 deletions
diff --git a/src/api/dashboard/mod.rs b/src/api/dashboard/mod.rs index 15b8cf9..c0de169 100644 --- a/src/api/dashboard/mod.rs +++ b/src/api/dashboard/mod.rs @@ -913,8 +913,8 @@ mod tests { #[test] #[ignore] fn dashboard_in_a_browser() { - let file = - std::env::temp_dir().join(format!("continuum-dashboard-{}.html", std::process::id())); + let dir = crate::files::scratch_dir("dashboard"); + let file = dir.join("page.html"); std::fs::write(&file, &page().1).expect("page written"); let check = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/api/dashboard/check.js"); @@ -928,7 +928,6 @@ mod tests { PATH, with Playwright's library and its Chromium (check.nix)" ) }); - let _ = std::fs::remove_file(&file); assert!(status.success(), "the browser checks failed"); } diff --git a/src/federation/held.rs b/src/federation/held.rs index 3ca74a9..abb3d30 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -1795,7 +1795,8 @@ mod tests { for event in [&write, &beside, &proposal, &yes] { put(&mut doc, event); } - let record = crate::files::scratch_dir("held-votes-above").join("makefu.json"); + let dir = crate::files::scratch_dir("held-votes-above"); + let record = dir.join("makefu.json"); let version = Version::read(&first.json()).unwrap(); let mut begun = Record::begin(Id::of(&root), "makefu", &version); begun.began = Id::of(b"before the federation").to_string(); 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(); } } diff --git a/src/identity.rs b/src/identity.rs index 1012d23..23c4f0f 100644 --- a/src/identity.rs +++ b/src/identity.rs @@ -118,7 +118,8 @@ mod tests { #[test] fn created_only_when_asked_and_then_kept() { - let path = scratch_dir("identity-create").join("node.key"); + let dir = scratch_dir("identity-create"); + let path = dir.join("node.key"); assert!(NodeKey::load(&path, false).is_err()); assert!(!path.exists()); @@ -138,7 +139,8 @@ mod tests { #[test] fn a_key_of_the_wrong_size_is_refused() { - let path = scratch_dir("identity-size").join("node.key"); + let dir = scratch_dir("identity-size"); + let path = dir.join("node.key"); std::fs::write(&path, [7u8; 16]).unwrap(); assert!(NodeKey::load(&path, true).is_err()); } diff --git a/src/swarm/auth.rs b/src/swarm/auth.rs index 98a004c..7ac813b 100644 --- a/src/swarm/auth.rs +++ b/src/swarm/auth.rs @@ -166,7 +166,8 @@ mod tests { /// A swarm key file is made only when asked, then read as any other. #[test] fn swarm_key_created_only_when_asked() { - let path = crate::files::scratch_dir("swarm-key-create").join("swarm.key"); + let dir = crate::files::scratch_dir("swarm-key-create"); + let path = dir.join("swarm.key"); assert!(SwarmKey::from_file(&path, false).is_err()); assert!(!path.exists()); let created = SwarmKey::from_file(&path, true).unwrap(); |
