mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 23:36:11 +00:00
test(core): remove temp dirs after tests via shared RAII helper
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
09fbf79e87
commit
00226b4178
9 files changed
+115
-48
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user