From 00226b41787e1a9c99544ca8534613ccfc51c859 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 16 Nov 2026 19:23:09 +0100 Subject: [PATCH] test(core): remove temp dirs after tests via shared RAII helper Co-Authored-By: Claude Sonnet 5.5 --- crates/grove-core/src/busy.rs | 4 +- crates/grove-core/src/lib.rs | 4 ++ crates/grove-core/src/registry.rs | 4 +- crates/grove-core/tests/common/mod.rs | 19 +++--- crates/grove-core/tests/inspect.rs | 21 ++----- crates/grove-core/tests/receive.rs | 5 +- crates/grove-core/tests/resolve.rs | 2 +- crates/grove-core/tests/snapshot.rs | 17 +----- test-support/tmp.rs | 87 +++++++++++++++++++++++++++ 9 files changed, 115 insertions(+), 48 deletions(-) create mode 100644 test-support/tmp.rs diff --git a/crates/grove-core/src/busy.rs b/crates/grove-core/src/busy.rs index 58f399f..7539ea6 100644 --- a/crates/grove-core/src/busy.rs +++ b/crates/grove-core/src/busy.rs @@ -73,8 +73,8 @@ mod tests { #[test] fn marker_lifecycle() { - let d = std::env::temp_dir().join(format!("grove-busy-{}", std::process::id())); - let _ = fs::remove_dir_all(&d); + let tmp = crate::test_tmp::TempDir::new("busy"); + let d = tmp.path().join("d"); assert_eq!(busy_state(&d), BusyState::Free); let g = BusyGuard::acquire(&d).unwrap(); assert_eq!(busy_state(&d), BusyState::Active); diff --git a/crates/grove-core/src/lib.rs b/crates/grove-core/src/lib.rs index 59e8c8e..04797f0 100644 --- a/crates/grove-core/src/lib.rs +++ b/crates/grove-core/src/lib.rs @@ -1,5 +1,9 @@ //! Sync engine core: git plumbing wrapper, config, snapshots and state classification. +#[cfg(test)] +#[path = "../../../test-support/tmp.rs"] +mod test_tmp; + pub mod busy; pub mod config; pub mod git; diff --git a/crates/grove-core/src/registry.rs b/crates/grove-core/src/registry.rs index 7da8120..e0e5712 100644 --- a/crates/grove-core/src/registry.rs +++ b/crates/grove-core/src/registry.rs @@ -218,8 +218,8 @@ mod tests { #[test] fn rollback_that_cannot_remove_everything_leaves_a_stage_marker() { - let d = std::env::temp_dir().join(format!("grove-rb-{}", std::process::id())); - let _ = fs::remove_dir_all(&d); + let tmp = crate::test_tmp::TempDir::new("rb"); + let d = tmp.path().join("d"); let locked = d.join(".bare/locked"); fs::create_dir_all(&locked).unwrap(); fs::write(locked.join("f"), "x").unwrap(); diff --git a/crates/grove-core/tests/common/mod.rs b/crates/grove-core/tests/common/mod.rs index af7ccad..c8a8ccc 100644 --- a/crates/grove-core/tests/common/mod.rs +++ b/crates/grove-core/tests/common/mod.rs @@ -1,19 +1,14 @@ #![allow(dead_code)] use grove_core::git::Git; use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; -pub fn tmpdir(tag: &str) -> PathBuf { - let p = std::env::temp_dir().join(format!( - "grove-test-{tag}-{}-{}", - std::process::id(), - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_nanos() - )); - fs::create_dir_all(&p).unwrap(); - p +#[path = "../../../../test-support/tmp.rs"] +pub mod tmp; + +/// A unique temp dir that is removed (recursively) when dropped, also if the test fails. +pub fn tmpdir(tag: &str) -> tmp::TempDir { + tmp::TempDir::new(tag) } pub fn init(dir: &Path) -> Git { diff --git a/crates/grove-core/tests/inspect.rs b/crates/grove-core/tests/inspect.rs index 527ac16..5dbeaed 100644 --- a/crates/grove-core/tests/inspect.rs +++ b/crates/grove-core/tests/inspect.rs @@ -1,22 +1,11 @@ use grove_core::git::Git; use grove_core::inspect::{state, worktrees}; use grove_core::state::State; +mod common; +use common::tmpdir; use std::fs; use std::path::{Path, PathBuf}; -fn tmpdir(tag: &str) -> PathBuf { - let p = std::env::temp_dir().join(format!( - "grove-test-{tag}-{}-{}", - std::process::id(), - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_nanos() - )); - fs::create_dir_all(&p).unwrap(); - p -} - fn commit(g: &Git, dir: &Path, file: &str, body: &str) { fs::write(dir.join(file), body).unwrap(); g.run(&["add", "."]).unwrap(); @@ -24,7 +13,7 @@ fn commit(g: &Git, dir: &Path, file: &str, body: &str) { } /// A bare "remote" plus two clones (two machines) tracking its `main`. -fn setup() -> (Git, PathBuf, Git, PathBuf) { +fn setup() -> (common::tmp::TempDir, Git, PathBuf, Git, PathBuf) { let base = tmpdir("inspect"); let remote = base.join("remote.git"); Git::new(&base) @@ -60,12 +49,12 @@ fn setup() -> (Git, PathBuf, Git, PathBuf) { g2.run(&["pull", "-q", "origin", "main"]).unwrap(); g2.run(&["branch", "-q", "--set-upstream-to=origin/main", "main"]) .unwrap(); - (g1, d1, g2, d2) + (base, g1, d1, g2, d2) } #[test] fn classifies_real_repos() { - let (g1, d1, g2, d2) = setup(); + let (_base, g1, d1, g2, d2) = setup(); assert_eq!(state(&d1), State::Synced); fs::write(d1.join("wip.txt"), "x").unwrap(); diff --git a/crates/grove-core/tests/receive.rs b/crates/grove-core/tests/receive.rs index 567614b..b86bd34 100644 --- a/crates/grove-core/tests/receive.rs +++ b/crates/grove-core/tests/receive.rs @@ -9,6 +9,7 @@ use std::fs; use std::path::PathBuf; struct World { + _tmp: common::tmp::TempDir, base: PathBuf, remote: PathBuf, g1: Git, @@ -19,7 +20,8 @@ struct World { /// Bare remote plus two plain clones (machines "m1" and "m2") sharing `main` with one commit. fn world() -> World { - let base = tmpdir("recv"); + let tmp = tmpdir("recv"); + let base = tmp.to_path_buf(); let remote = base.join("remote.git"); Git::new(&base) .run(&[ @@ -51,6 +53,7 @@ fn world() -> World { g2.run(&["config", "user.email", "t@t"]).unwrap(); g2.run(&["config", "user.name", "t"]).unwrap(); World { + _tmp: tmp, base, remote, g1, diff --git a/crates/grove-core/tests/resolve.rs b/crates/grove-core/tests/resolve.rs index 6d6e32e..a46017b 100644 --- a/crates/grove-core/tests/resolve.rs +++ b/crates/grove-core/tests/resolve.rs @@ -9,7 +9,7 @@ use std::fs; use std::path::PathBuf; /// Two machines on the bare layout, both committed on main: diverged on B. -fn diverged() -> (PathBuf, PathBuf, PathBuf) { +fn diverged() -> (common::tmp::TempDir, PathBuf, PathBuf) { let base = tmpdir("resolve"); let remote = base.join("r.git"); Git::new(&base) diff --git a/crates/grove-core/tests/snapshot.rs b/crates/grove-core/tests/snapshot.rs index 4c62007..f3447c9 100644 --- a/crates/grove-core/tests/snapshot.rs +++ b/crates/grove-core/tests/snapshot.rs @@ -1,20 +1,9 @@ use grove_core::git::Git; use grove_core::snapshot::{Outcome, take}; +mod common; +use common::tmpdir; use std::fs; -use std::path::{Path, PathBuf}; - -fn tmpdir(tag: &str) -> PathBuf { - let p = std::env::temp_dir().join(format!( - "grove-test-{tag}-{}-{}", - std::process::id(), - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_nanos() - )); - fs::create_dir_all(&p).unwrap(); - p -} +use std::path::Path; fn repo(dir: &Path) -> Git { let g = Git::new(dir); diff --git a/test-support/tmp.rs b/test-support/tmp.rs new file mode 100644 index 0000000..25519c0 --- /dev/null +++ b/test-support/tmp.rs @@ -0,0 +1,87 @@ +//! Shared RAII temp-dir helper for tests; included with `#[path]` so no extra crate is needed. +//! +//! Creates a unique directory under `std::env::temp_dir()` and removes it recursively on drop, +//! also when the test panics. `/tmp` is tmpfs and has run out of inodes before, so tests must +//! never leave directories behind. +#![allow(dead_code)] + +use std::ops::Deref; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; + +static COUNTER: AtomicU64 = AtomicU64::new(0); + +pub struct TempDir { + path: PathBuf, +} + +impl TempDir { + /// `/grove-test----`, created (with parents). + pub fn new(tag: &str) -> TempDir { + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0); + let n = COUNTER.fetch_add(1, Ordering::Relaxed); + let path = std::env::temp_dir().join(format!( + "grove-test-{tag}-{}-{nanos}-{n}", + std::process::id() + )); + std::fs::create_dir_all(&path).expect("create temp dir"); + TempDir { path } + } + + pub fn path(&self) -> &Path { + &self.path + } +} + +/// Remove `path` recursively, making read-only entries (git objects) writable first. +fn remove_all(path: &Path) { + if std::fs::remove_dir_all(path).is_ok() { + return; + } + fn chmod(p: &Path) { + use std::os::unix::fs::PermissionsExt; + let Ok(meta) = std::fs::symlink_metadata(p) else { + return; + }; + if meta.is_dir() { + let _ = std::fs::set_permissions(p, std::fs::Permissions::from_mode(0o700)); + if let Ok(rd) = std::fs::read_dir(p) { + for e in rd.flatten() { + chmod(&e.path()); + } + } + } else if !meta.file_type().is_symlink() { + let _ = std::fs::set_permissions(p, std::fs::Permissions::from_mode(0o600)); + } + } + chmod(path); + let _ = std::fs::remove_dir_all(path); +} + +impl Drop for TempDir { + fn drop(&mut self) { + remove_all(&self.path); + } +} + +impl Deref for TempDir { + type Target = Path; + fn deref(&self) -> &Path { + &self.path + } +} + +impl AsRef for TempDir { + fn as_ref(&self) -> &Path { + &self.path + } +} + +impl From<&TempDir> for PathBuf { + fn from(t: &TempDir) -> PathBuf { + t.path.clone() + } +}