Skip to content

Commit 27acfe1

Browse files
committed
test(runtime): isolate session and git metadata checks
1 parent c164661 commit 27acfe1

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

rust/crates/runtime/src/session_control.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,10 +828,40 @@ mod tests {
828828
use std::fs;
829829
use std::path::{Path, PathBuf};
830830
use std::sync::atomic::{AtomicU64, Ordering};
831+
use std::sync::{Mutex, OnceLock};
831832
use std::time::{SystemTime, UNIX_EPOCH};
832833

833834
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
834835

836+
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
837+
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
838+
LOCK.get_or_init(|| Mutex::new(()))
839+
.lock()
840+
.expect("env lock")
841+
}
842+
843+
struct EnvVarGuard {
844+
key: &'static str,
845+
previous: Option<std::ffi::OsString>,
846+
}
847+
848+
impl EnvVarGuard {
849+
fn set(key: &'static str, value: &Path) -> Self {
850+
let previous = std::env::var_os(key);
851+
std::env::set_var(key, value);
852+
Self { key, previous }
853+
}
854+
}
855+
856+
impl Drop for EnvVarGuard {
857+
fn drop(&mut self) {
858+
match &self.previous {
859+
Some(value) => std::env::set_var(self.key, value),
860+
None => std::env::remove_var(self.key),
861+
}
862+
}
863+
}
864+
835865
fn temp_dir() -> PathBuf {
836866
let nanos = SystemTime::now()
837867
.duration_since(UNIX_EPOCH)
@@ -1290,8 +1320,11 @@ mod tests {
12901320
#[test]
12911321
fn latest_session_returns_all_empty_error_when_sessions_exist_but_have_no_messages() {
12921322
// given — create sessions with 0 messages (empty)
1323+
let _env_guard = env_lock();
12931324
let base = temp_dir();
12941325
fs::create_dir_all(&base).expect("base dir should exist");
1326+
let isolated_config_home = base.join("config-home");
1327+
let _claw_config_home = EnvVarGuard::set("CLAW_CONFIG_HOME", &isolated_config_home);
12951328
let store = SessionStore::from_cwd(&base).expect("store should build");
12961329

12971330
let empty_handle = store.create_handle("empty-session");

rust/crates/runtime/src/worker_boot.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,16 +1644,13 @@ mod tests {
16441644

16451645
let tmp = tempfile::tempdir().expect("tempdir");
16461646
let worktree = tmp.path().join("worktree");
1647-
let git_dir = tmp.path().join("external-gitdir");
16481647
fs::create_dir_all(&worktree).expect("worktree dir");
1649-
fs::create_dir_all(git_dir.join("objects")).expect("objects dir");
1650-
fs::create_dir_all(git_dir.join("refs/heads")).expect("refs dir");
1651-
fs::write(git_dir.join("HEAD"), "ref: refs/heads/main\n").expect("HEAD");
1652-
fs::write(
1653-
worktree.join(".git"),
1654-
format!("gitdir: {}\n", git_dir.display()),
1655-
)
1656-
.expect(".git file");
1648+
Command::new("git")
1649+
.arg("init")
1650+
.current_dir(&worktree)
1651+
.output()
1652+
.expect("git init should run");
1653+
let git_dir = worktree.join(".git");
16571654

16581655
let original_permissions = fs::metadata(&git_dir)
16591656
.expect("gitdir metadata")

0 commit comments

Comments
 (0)