mirror of
https://github.com/instructkr/claude-code.git
synced 2026-06-08 13:16:46 +00:00
test(runtime): isolate session and git metadata checks
This commit is contained in:
@@ -828,10 +828,40 @@ mod tests {
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
use std::sync::{Mutex, OnceLock};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
|
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
|
||||||
|
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
|
||||||
|
LOCK.get_or_init(|| Mutex::new(()))
|
||||||
|
.lock()
|
||||||
|
.expect("env lock")
|
||||||
|
}
|
||||||
|
|
||||||
|
struct EnvVarGuard {
|
||||||
|
key: &'static str,
|
||||||
|
previous: Option<std::ffi::OsString>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EnvVarGuard {
|
||||||
|
fn set(key: &'static str, value: &Path) -> Self {
|
||||||
|
let previous = std::env::var_os(key);
|
||||||
|
std::env::set_var(key, value);
|
||||||
|
Self { key, previous }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for EnvVarGuard {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
match &self.previous {
|
||||||
|
Some(value) => std::env::set_var(self.key, value),
|
||||||
|
None => std::env::remove_var(self.key),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn temp_dir() -> PathBuf {
|
fn temp_dir() -> PathBuf {
|
||||||
let nanos = SystemTime::now()
|
let nanos = SystemTime::now()
|
||||||
.duration_since(UNIX_EPOCH)
|
.duration_since(UNIX_EPOCH)
|
||||||
@@ -1290,8 +1320,11 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn latest_session_returns_all_empty_error_when_sessions_exist_but_have_no_messages() {
|
fn latest_session_returns_all_empty_error_when_sessions_exist_but_have_no_messages() {
|
||||||
// given — create sessions with 0 messages (empty)
|
// given — create sessions with 0 messages (empty)
|
||||||
|
let _env_guard = env_lock();
|
||||||
let base = temp_dir();
|
let base = temp_dir();
|
||||||
fs::create_dir_all(&base).expect("base dir should exist");
|
fs::create_dir_all(&base).expect("base dir should exist");
|
||||||
|
let isolated_config_home = base.join("config-home");
|
||||||
|
let _claw_config_home = EnvVarGuard::set("CLAW_CONFIG_HOME", &isolated_config_home);
|
||||||
let store = SessionStore::from_cwd(&base).expect("store should build");
|
let store = SessionStore::from_cwd(&base).expect("store should build");
|
||||||
|
|
||||||
let empty_handle = store.create_handle("empty-session");
|
let empty_handle = store.create_handle("empty-session");
|
||||||
|
|||||||
@@ -1644,16 +1644,13 @@ mod tests {
|
|||||||
|
|
||||||
let tmp = tempfile::tempdir().expect("tempdir");
|
let tmp = tempfile::tempdir().expect("tempdir");
|
||||||
let worktree = tmp.path().join("worktree");
|
let worktree = tmp.path().join("worktree");
|
||||||
let git_dir = tmp.path().join("external-gitdir");
|
|
||||||
fs::create_dir_all(&worktree).expect("worktree dir");
|
fs::create_dir_all(&worktree).expect("worktree dir");
|
||||||
fs::create_dir_all(git_dir.join("objects")).expect("objects dir");
|
Command::new("git")
|
||||||
fs::create_dir_all(git_dir.join("refs/heads")).expect("refs dir");
|
.arg("init")
|
||||||
fs::write(git_dir.join("HEAD"), "ref: refs/heads/main\n").expect("HEAD");
|
.current_dir(&worktree)
|
||||||
fs::write(
|
.output()
|
||||||
worktree.join(".git"),
|
.expect("git init should run");
|
||||||
format!("gitdir: {}\n", git_dir.display()),
|
let git_dir = worktree.join(".git");
|
||||||
)
|
|
||||||
.expect(".git file");
|
|
||||||
|
|
||||||
let original_permissions = fs::metadata(&git_dir)
|
let original_permissions = fs::metadata(&git_dir)
|
||||||
.expect("gitdir metadata")
|
.expect("gitdir metadata")
|
||||||
|
|||||||
Reference in New Issue
Block a user