mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 07:16: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]
|
#[test]
|
||||||
fn marker_lifecycle() {
|
fn marker_lifecycle() {
|
||||||
let d = std::env::temp_dir().join(format!("grove-busy-{}", std::process::id()));
|
let tmp = crate::test_tmp::TempDir::new("busy");
|
||||||
let _ = fs::remove_dir_all(&d);
|
let d = tmp.path().join("d");
|
||||||
assert_eq!(busy_state(&d), BusyState::Free);
|
assert_eq!(busy_state(&d), BusyState::Free);
|
||||||
let g = BusyGuard::acquire(&d).unwrap();
|
let g = BusyGuard::acquire(&d).unwrap();
|
||||||
assert_eq!(busy_state(&d), BusyState::Active);
|
assert_eq!(busy_state(&d), BusyState::Active);
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
//! Sync engine core: git plumbing wrapper, config, snapshots and state classification.
|
//! 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 busy;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod git;
|
pub mod git;
|
||||||
|
|||||||
@@ -218,8 +218,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rollback_that_cannot_remove_everything_leaves_a_stage_marker() {
|
fn rollback_that_cannot_remove_everything_leaves_a_stage_marker() {
|
||||||
let d = std::env::temp_dir().join(format!("grove-rb-{}", std::process::id()));
|
let tmp = crate::test_tmp::TempDir::new("rb");
|
||||||
let _ = fs::remove_dir_all(&d);
|
let d = tmp.path().join("d");
|
||||||
let locked = d.join(".bare/locked");
|
let locked = d.join(".bare/locked");
|
||||||
fs::create_dir_all(&locked).unwrap();
|
fs::create_dir_all(&locked).unwrap();
|
||||||
fs::write(locked.join("f"), "x").unwrap();
|
fs::write(locked.join("f"), "x").unwrap();
|
||||||
|
|||||||
@@ -1,19 +1,14 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
use grove_core::git::Git;
|
use grove_core::git::Git;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::Path;
|
||||||
|
|
||||||
pub fn tmpdir(tag: &str) -> PathBuf {
|
#[path = "../../../../test-support/tmp.rs"]
|
||||||
let p = std::env::temp_dir().join(format!(
|
pub mod tmp;
|
||||||
"grove-test-{tag}-{}-{}",
|
|
||||||
std::process::id(),
|
/// A unique temp dir that is removed (recursively) when dropped, also if the test fails.
|
||||||
std::time::SystemTime::now()
|
pub fn tmpdir(tag: &str) -> tmp::TempDir {
|
||||||
.duration_since(std::time::UNIX_EPOCH)
|
tmp::TempDir::new(tag)
|
||||||
.unwrap()
|
|
||||||
.as_nanos()
|
|
||||||
));
|
|
||||||
fs::create_dir_all(&p).unwrap();
|
|
||||||
p
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(dir: &Path) -> Git {
|
pub fn init(dir: &Path) -> Git {
|
||||||
|
|||||||
@@ -1,22 +1,11 @@
|
|||||||
use grove_core::git::Git;
|
use grove_core::git::Git;
|
||||||
use grove_core::inspect::{state, worktrees};
|
use grove_core::inspect::{state, worktrees};
|
||||||
use grove_core::state::State;
|
use grove_core::state::State;
|
||||||
|
mod common;
|
||||||
|
use common::tmpdir;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
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) {
|
fn commit(g: &Git, dir: &Path, file: &str, body: &str) {
|
||||||
fs::write(dir.join(file), body).unwrap();
|
fs::write(dir.join(file), body).unwrap();
|
||||||
g.run(&["add", "."]).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`.
|
/// 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 base = tmpdir("inspect");
|
||||||
let remote = base.join("remote.git");
|
let remote = base.join("remote.git");
|
||||||
Git::new(&base)
|
Git::new(&base)
|
||||||
@@ -60,12 +49,12 @@ fn setup() -> (Git, PathBuf, Git, PathBuf) {
|
|||||||
g2.run(&["pull", "-q", "origin", "main"]).unwrap();
|
g2.run(&["pull", "-q", "origin", "main"]).unwrap();
|
||||||
g2.run(&["branch", "-q", "--set-upstream-to=origin/main", "main"])
|
g2.run(&["branch", "-q", "--set-upstream-to=origin/main", "main"])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
(g1, d1, g2, d2)
|
(base, g1, d1, g2, d2)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn classifies_real_repos() {
|
fn classifies_real_repos() {
|
||||||
let (g1, d1, g2, d2) = setup();
|
let (_base, g1, d1, g2, d2) = setup();
|
||||||
assert_eq!(state(&d1), State::Synced);
|
assert_eq!(state(&d1), State::Synced);
|
||||||
|
|
||||||
fs::write(d1.join("wip.txt"), "x").unwrap();
|
fs::write(d1.join("wip.txt"), "x").unwrap();
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use std::fs;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
struct World {
|
struct World {
|
||||||
|
_tmp: common::tmp::TempDir,
|
||||||
base: PathBuf,
|
base: PathBuf,
|
||||||
remote: PathBuf,
|
remote: PathBuf,
|
||||||
g1: Git,
|
g1: Git,
|
||||||
@@ -19,7 +20,8 @@ struct World {
|
|||||||
|
|
||||||
/// Bare remote plus two plain clones (machines "m1" and "m2") sharing `main` with one commit.
|
/// Bare remote plus two plain clones (machines "m1" and "m2") sharing `main` with one commit.
|
||||||
fn world() -> World {
|
fn world() -> World {
|
||||||
let base = tmpdir("recv");
|
let tmp = tmpdir("recv");
|
||||||
|
let base = tmp.to_path_buf();
|
||||||
let remote = base.join("remote.git");
|
let remote = base.join("remote.git");
|
||||||
Git::new(&base)
|
Git::new(&base)
|
||||||
.run(&[
|
.run(&[
|
||||||
@@ -51,6 +53,7 @@ fn world() -> World {
|
|||||||
g2.run(&["config", "user.email", "t@t"]).unwrap();
|
g2.run(&["config", "user.email", "t@t"]).unwrap();
|
||||||
g2.run(&["config", "user.name", "t"]).unwrap();
|
g2.run(&["config", "user.name", "t"]).unwrap();
|
||||||
World {
|
World {
|
||||||
|
_tmp: tmp,
|
||||||
base,
|
base,
|
||||||
remote,
|
remote,
|
||||||
g1,
|
g1,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use std::fs;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// Two machines on the bare layout, both committed on main: diverged on B.
|
/// 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 base = tmpdir("resolve");
|
||||||
let remote = base.join("r.git");
|
let remote = base.join("r.git");
|
||||||
Git::new(&base)
|
Git::new(&base)
|
||||||
|
|||||||
@@ -1,20 +1,9 @@
|
|||||||
use grove_core::git::Git;
|
use grove_core::git::Git;
|
||||||
use grove_core::snapshot::{Outcome, take};
|
use grove_core::snapshot::{Outcome, take};
|
||||||
|
mod common;
|
||||||
|
use common::tmpdir;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::Path;
|
||||||
|
|
||||||
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 repo(dir: &Path) -> Git {
|
fn repo(dir: &Path) -> Git {
|
||||||
let g = Git::new(dir);
|
let g = Git::new(dir);
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
/// `<tmp>/grove-test-<tag>-<pid>-<nanos>-<counter>`, 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<Path> for TempDir {
|
||||||
|
fn as_ref(&self) -> &Path {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&TempDir> for PathBuf {
|
||||||
|
fn from(t: &TempDir) -> PathBuf {
|
||||||
|
t.path.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user