test: clean temp dirs and spawned daemons in all crates

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Sonnet 5.5 committed 2026-11-16 19:26:23 +01:00
1 parent 00226b4178
commit dd4cea48d0
12 files changed
+92 -65

No files matched your search

+2 -1
View File
@@ -91,7 +91,8 @@ mod tests {
#[test]
fn only_bare_layout_repos_in_configured_groups_are_managed() {
let base = std::env::temp_dir().join(format!("grove-disc-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("grove-disc");
let base = _tmp.path().to_path_buf();
let _ = std::fs::remove_dir_all(&base);
for d in [
"grp/migrated/.bare",
+3
View File
@@ -6,6 +6,9 @@ pub mod activity;
pub mod daemon;
pub mod discover;
pub mod ipc;
#[cfg(test)]
#[path = "../../../test-support/tmp.rs"]
mod test_tmp;
pub mod visibility;
pub use activity::{Activity, Event};
+8 -4
View File
@@ -670,7 +670,8 @@ owners = { github = "gh-org" }
#[test]
fn cache_persists_in_the_state_dir_with_mode_0600() {
use std::os::unix::fs::PermissionsExt;
let dir = std::env::temp_dir().join(format!("grove-vis-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("grove-vis");
let dir = _tmp.path().to_path_buf();
let _ = std::fs::remove_dir_all(&dir);
let file = dir.join("visibility.json");
let c = cfg();
@@ -714,7 +715,8 @@ owners = { github = "gh-org" }
#[test]
fn gate_uses_group_policy_and_every_push_url() {
let c = cfg();
let base = std::env::temp_dir().join(format!("grove-gate-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("grove-gate");
let base = _tmp.path().to_path_buf();
let _ = std::fs::remove_dir_all(&base);
let f = Fake::default()
.with("o/r", Ok(Seen::Private))
@@ -752,7 +754,8 @@ owners = { github = "gh-org" }
#[test]
fn gate_checks_all_push_urls_and_push_instead_of() {
let c = cfg();
let base = std::env::temp_dir().join(format!("grove-gate2-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("grove-gate2");
let base = _tmp.path().to_path_buf();
let _ = std::fs::remove_dir_all(&base);
let f = Fake::default()
.with("o/r", Ok(Seen::Private))
@@ -781,7 +784,8 @@ owners = { github = "gh-org" }
#[test]
fn gate_requires_mirror_targets_to_be_private_or_absent() {
let c = cfg();
let base = std::env::temp_dir().join(format!("grove-gate3-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("grove-gate3");
let base = _tmp.path().to_path_buf();
let _ = std::fs::remove_dir_all(&base);
let repo = mk(&base, "mir", &["https://git.example.com:3000/o/r.git"], &[]);
let gate = |f: &Fake| {
+46 -30
View File
@@ -1,6 +1,8 @@
use grove_core::config::Config;
use grove_daemon::{Daemon, Settings, ipc};
use serde_json::json;
#[path = "../../../test-support/tmp.rs"]
mod tmp;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
@@ -39,18 +41,24 @@ fn wait_until(what: &str, secs: u64, mut f: impl FnMut() -> bool) {
struct World {
base: PathBuf,
remote: PathBuf,
// Declared last so it is dropped (and the dir removed) after everything else in the world.
_tmp: tmp::TempDir,
}
fn tmpdir(tag: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("grove-dt-{tag}-{}", std::process::id()));
let _ = fs::remove_dir_all(&d);
fs::create_dir_all(&d).unwrap();
d
/// Kills and reaps a spawned `groved` (by its own handle) when dropped, also on panic.
struct Spawned(std::process::Child);
impl Drop for Spawned {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
/// A bare remote plus one seeded clone per machine under `<base>/<machine>/repo`.
fn world(tag: &str, machines: &[&str]) -> World {
let base = tmpdir(tag);
let tmp = tmp::TempDir::new(tag);
let base = tmp.path().to_path_buf();
let remote = base.join("remote.git");
fs::create_dir_all(&remote).unwrap();
git(&remote, &["init", "-q", "--bare", "-b", "main"]);
@@ -70,7 +78,11 @@ fn world(tag: &str, machines: &[&str]) -> World {
fs::create_dir_all(&dir).unwrap();
bare_layout(&dir.join("g/repo"), &remote);
}
World { base, remote }
World {
base,
remote,
_tmp: tmp,
}
}
/// A bare + worktrees repo (`.bare`, worktree `main`) cloned from `remote`, as `grove add` leaves it.
@@ -274,22 +286,24 @@ fn killing_the_daemon_loses_nothing_and_a_restart_recovers() {
let runtime = w.base.join("run");
fs::create_dir_all(&runtime).unwrap();
let spawn = || {
Command::new(env!("CARGO_BIN_EXE_groved"))
.arg("--config")
.arg(&cfg_path)
.env("XDG_RUNTIME_DIR", &runtime)
.env("XDG_STATE_HOME", w.base.join("state"))
.env("GIT_AUTHOR_NAME", "t")
.spawn()
.unwrap()
Spawned(
Command::new(env!("CARGO_BIN_EXE_groved"))
.arg("--config")
.arg(&cfg_path)
.env("XDG_RUNTIME_DIR", &runtime)
.env("XDG_STATE_HOME", w.base.join("state"))
.env("GIT_AUTHOR_NAME", "t")
.spawn()
.unwrap(),
)
};
let mut child = spawn();
std::thread::sleep(Duration::from_millis(300));
fs::write(repo.join("wip.txt"), "precious\n").unwrap();
// Kill at an arbitrary moment: maybe before, during or after a pass.
std::thread::sleep(Duration::from_millis(400));
child.kill().unwrap();
child.wait().unwrap();
child.0.kill().unwrap();
child.0.wait().unwrap();
assert_eq!(
fs::read_to_string(repo.join("wip.txt")).unwrap(),
@@ -310,8 +324,8 @@ fn killing_the_daemon_loses_nothing_and_a_restart_recovers() {
fs::read_to_string(repo.join("wip.txt")).unwrap(),
"precious\n"
);
child.kill().unwrap();
child.wait().unwrap();
child.0.kill().unwrap();
child.0.wait().unwrap();
}
#[test]
@@ -356,24 +370,26 @@ fn paused_daemon_does_not_spin() {
.unwrap();
let runtime = w.base.join("run");
fs::create_dir_all(&runtime).unwrap();
let mut child = Command::new(env!("CARGO_BIN_EXE_groved"))
.arg("--config")
.arg(&cfg_path)
.env("XDG_RUNTIME_DIR", &runtime)
.env("XDG_STATE_HOME", w.base.join("state"))
.spawn()
.unwrap();
let mut child = Spawned(
Command::new(env!("CARGO_BIN_EXE_groved"))
.arg("--config")
.arg(&cfg_path)
.env("XDG_RUNTIME_DIR", &runtime)
.env("XDG_STATE_HOME", w.base.join("state"))
.spawn()
.unwrap(),
);
let sock = runtime.join("grove/grove.sock");
wait_until("daemon up", 10, || {
ipc::request(&sock, &json!({"v":1,"cmd":"ping"})).is_ok()
});
ipc::request(&sock, &json!({"v":1,"cmd":"pause"})).unwrap();
std::thread::sleep(Duration::from_millis(1500)); // let any in-flight pass finish
let before = cpu_ticks(child.id());
let before = cpu_ticks(child.0.id());
std::thread::sleep(Duration::from_secs(3)); // well past several fetch intervals
let used = cpu_ticks(child.id()) - before;
child.kill().unwrap();
child.wait().unwrap();
let used = cpu_ticks(child.0.id()) - before;
child.0.kill().unwrap();
child.0.wait().unwrap();
assert!(used < 30, "paused daemon burned {used} ticks of CPU in 3 s");
}
+2 -1
View File
@@ -41,7 +41,8 @@ mod tests {
#[test]
fn toggle_writes_and_removes_the_desktop_file() {
let d = std::env::temp_dir().join(format!("gg-auto-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("gg-auto");
let d = _tmp.path().to_path_buf();
assert!(!is_enabled(&d));
set(&d, true).unwrap();
assert!(is_enabled(&d));
+4 -2
View File
@@ -219,7 +219,8 @@ mod tests {
#[test]
fn migrate_path_must_be_a_clone_directly_under_a_group() {
let base = std::env::temp_dir().join(format!("grove-mig-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("grove-mig");
let base = _tmp.path().to_path_buf();
let _ = std::fs::remove_dir_all(&base);
std::fs::create_dir_all(base.join("work/proj/sub")).unwrap();
std::fs::create_dir_all(base.join("other/proj")).unwrap();
@@ -248,7 +249,8 @@ mod tests {
#[test]
fn add_runs_only_for_valid_clone_paths() {
use std::os::unix::fs::PermissionsExt;
let base = std::env::temp_dir().join(format!("grove-add-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("grove-add");
let base = _tmp.path().to_path_buf();
let _ = std::fs::remove_dir_all(&base);
std::fs::create_dir_all(base.join("work/proj")).unwrap();
std::fs::create_dir_all(base.join("elsewhere")).unwrap();
+10 -10
View File
@@ -125,17 +125,16 @@ mod tests {
use std::os::unix::fs::PermissionsExt;
use std::sync::mpsc;
fn tmp_sock(tag: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("gg-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d); // leftovers of an earlier run with the same pid
std::fs::create_dir_all(&d).unwrap();
std::fs::set_permissions(&d, std::fs::Permissions::from_mode(0o700)).unwrap();
d.join("gui.sock")
fn tmp_sock(tag: &str) -> (crate::test_tmp::TempDir, PathBuf) {
let t = crate::test_tmp::TempDir::new(&format!("gg-{tag}"));
std::fs::set_permissions(t.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
let p = t.path().join("gui.sock");
(t, p)
}
#[test]
fn second_launch_asks_first_to_show() {
let p = tmp_sock("show");
let (_t, p) = tmp_sock("show");
let Role::Primary(l) = acquire(&p, true).unwrap() else {
panic!("first must be primary");
};
@@ -151,7 +150,7 @@ mod tests {
#[test]
fn unresponsive_instance_is_not_evicted() {
let p = tmp_sock("busy");
let (_t, p) = tmp_sock("busy");
let l = UnixListener::bind(&p).unwrap();
// Accepts but never answers (a primary stuck in startup).
let t = std::thread::spawn(move || {
@@ -167,7 +166,8 @@ mod tests {
#[test]
fn private_dir_is_enforced() {
let base = std::env::temp_dir().join(format!("gg-priv-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("gg-priv");
let base = _tmp.path().to_path_buf();
let d = base.join("x");
ensure_private_dir(&d).unwrap();
assert_eq!(std::fs::metadata(&d).unwrap().mode() & 0o777, 0o700);
@@ -179,7 +179,7 @@ mod tests {
#[test]
fn stale_socket_is_replaced() {
let p = tmp_sock("stale");
let (_t, p) = tmp_sock("stale");
drop(UnixListener::bind(&p).unwrap()); // leaves the socket file behind
assert!(p.exists());
assert!(matches!(acquire(&p, true).unwrap(), Role::Primary(_)));
+3
View File
@@ -9,6 +9,9 @@ mod bridge;
mod instance;
mod log;
mod model;
#[cfg(test)]
#[path = "../../../test-support/tmp.rs"]
mod test_tmp;
mod tray;
use anyhow::{Context, Result};
+4 -10
View File
@@ -1,3 +1,5 @@
#[path = "../../../test-support/tmp.rs"]
mod tmp;
use grove_core::config::ProviderKind;
use grove_providers::{Error, GitHub, Gitea, MirrorSpec, NewRepo, Provider, PullMirrorSpec, token};
use std::io::{Read, Write};
@@ -273,15 +275,8 @@ fn github_cannot_pull_mirror() {
fn falls_back_to_tea_and_gh_logins() {
use grove_core::config::Provider as ProviderCfg;
use std::os::unix::fs::PermissionsExt;
let dir = std::env::temp_dir().join(format!(
"grove-clitok-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&dir).unwrap();
let tmp = tmp::TempDir::new("clitok");
let dir = tmp.path().to_path_buf();
// tea's credential helper answers only for its host; gh echoes the host it was asked for.
let scripts = [
(
@@ -320,7 +315,6 @@ fn falls_back_to_tea_and_gh_logins() {
drop(off);
let _off = EnvGuard::set("GROVE_NO_CLI_TOKENS", "1");
assert!(token::resolve(&id, &github).is_err());
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
+3
View File
@@ -13,6 +13,9 @@ use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
mod secrets;
#[cfg(test)]
#[path = "../../../test-support/tmp.rs"]
mod test_tmp;
mod workflow;
#[derive(Parser)]
+2 -1
View File
@@ -763,7 +763,8 @@ mod tests {
#[test]
fn local_repos_match_by_name_or_origin_ignoring_case() {
let d = std::env::temp_dir().join(format!("grove-local-{}", std::process::id()));
let _tmp = crate::test_tmp::TempDir::new("grove-local");
let d = _tmp.path().to_path_buf();
let _ = fs::remove_dir_all(&d);
// `synapse` (lowercase dir) whose origin is Dasemu/Synapse; `other` with a renamed origin.
for (dir, origin) in [
+5 -6
View File
@@ -1,12 +1,11 @@
#[path = "../../../test-support/tmp.rs"]
mod tmp;
use std::fs;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::process::{Command, Output};
fn tmp(tag: &str) -> PathBuf {
let p = std::env::temp_dir().join(format!("grove-cli-{tag}-{}", std::process::id()));
let _ = fs::remove_dir_all(&p);
fs::create_dir_all(&p).unwrap();
p
fn tmp(tag: &str) -> tmp::TempDir {
tmp::TempDir::new(tag)
}
fn git(dir: &Path, args: &[&str]) -> String {