Watch
0
0
Fork
You've already forked hyperhive
0

hive-agent: test the in-agent socket's request dispatch

This commit is contained in:
damocles 2026-11-09 21:07:19 +01:00 • committed by mara
commit a0a3f9a70c

View file

@ -138,31 +138,50 @@ pub fn claude_dir() -> PathBuf {
/// are serialised on a lock.
#[cfg(test)]
pub fn with_harness_dir<T>(f: impl FnOnce() -> T) -> T {
use std::ffi::OsString;
let _guard = harness_dir_guard();
f()
}
/// [`with_harness_dir`] for a test body that cannot be a closure: the harness
/// dir stays set, and the lock stays held, until the guard drops.
#[cfg(test)]
pub fn harness_dir_guard() -> HarnessDirGuard {
use std::sync::Mutex;
static LOCK: Mutex<()> = Mutex::new(());
struct Restore(Option<OsString>);
impl Drop for Restore {
fn drop(&mut self) {
// SAFETY: still holding `LOCK`, the only writer of this variable.
unsafe {
match self.0.take() {
Some(v) => std::env::set_var("HYPERHIVE_HARNESS_DIR", v),
None => std::env::remove_var("HYPERHIVE_HARNESS_DIR"),
}
}
}
}
let _lock = LOCK
let lock = LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let dir = tempfile::tempdir().unwrap();
// Declared after `_lock` so the variable is restored before the lock drops.
let _restore = Restore(std::env::var_os("HYPERHIVE_HARNESS_DIR"));
let previous = std::env::var_os("HYPERHIVE_HARNESS_DIR");
// SAFETY: serialised by `LOCK`.
unsafe { std::env::set_var("HYPERHIVE_HARNESS_DIR", dir.path()) };
f()
HarnessDirGuard {
previous,
_dir: dir,
_lock: lock,
}
}
/// Holds `HYPERHIVE_HARNESS_DIR` at a fresh temp dir; see [`harness_dir_guard`].
#[cfg(test)]
pub struct HarnessDirGuard {
previous: Option<std::ffi::OsString>,
_dir: tempfile::TempDir,
// Declared last so the variable is restored (in `drop`) before the lock goes.
_lock: std::sync::MutexGuard<'static, ()>,
}
#[cfg(test)]
impl Drop for HarnessDirGuard {
fn drop(&mut self) {
// SAFETY: still holding the lock, the only writer of this variable.
unsafe {
match self.previous.take() {
Some(v) => std::env::set_var("HYPERHIVE_HARNESS_DIR", v),
None => std::env::remove_var("HYPERHIVE_HARNESS_DIR"),
}
}
}
}