Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(tests): extract duplicated EnvVarGuard into shared support module

Steven Enamakelc90a7085d8ef
diff --git a/tests/agent_harness_e2e.rs b/tests/agent_harness_e2e.rs
index 63330ffca8..eec5ca55f7 100644
--- a/tests/agent_harness_e2e.rs
+++ b/tests/agent_harness_e2e.rs
@@ -11,6 +11,9 @@
//! a false positive here — the lock IS the serialization mechanism.
#![allow(clippy::await_holding_lock)]
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Mutex, OnceLock};
@@ -62,40 +65,6 @@ fn env_lock() -> std::sync::MutexGuard<'static, ()> {
}
}
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(v) => std::env::set_var(self.key, v),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
// ─── Scripted upstream ──────────────────────────────────────────────────────
//
// Queue entries are JSON objects:
diff --git a/tests/inference_provider_e2e.rs b/tests/inference_provider_e2e.rs
index 8387eeb336..0f8c0af56e 100644
--- a/tests/inference_provider_e2e.rs
+++ b/tests/inference_provider_e2e.rs
@@ -9,6 +9,9 @@
//!
//! No live LLM API calls are made.
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use std::sync::{Mutex, OnceLock};
use axum::body::Body;
@@ -53,32 +56,6 @@ fn ensure_rpc_auth() {
// ── Helper: build an env-isolated Config pointing at tempdir ─────────────────
-/// Sets OPENHUMAN_WORKSPACE to `dir` and returns an `EnvVarGuard` that
-/// restores the previous value on drop. Must be called under `env_lock()`.
-struct EnvGuard {
- key: &'static str,
- prev: Option<String>,
-}
-
-impl EnvGuard {
- fn set(key: &'static str, val: &str) -> Self {
- let prev = std::env::var(key).ok();
- // SAFETY: caller holds env_lock().
- unsafe { std::env::set_var(key, val) };
- Self { key, prev }
- }
-}
-
-impl Drop for EnvGuard {
- fn drop(&mut self) {
- match &self.prev {
- // SAFETY: caller's env_lock guard is still alive during drop.
- Some(v) => unsafe { std::env::set_var(self.key, v) },
- None => unsafe { std::env::remove_var(self.key) },
- }
- }
-}
-
// ── Test 6: Streaming response returns ordered deltas ────────────────────────
// ── Test 8: /v1/chat/completions HTTP endpoint — unauthorized ─────────────────
@@ -128,7 +105,7 @@ async fn http_endpoint_models_with_bearer_returns_model_list() {
ensure_rpc_auth();
let tmp = tempdir().expect("tempdir");
- let _workspace_guard = EnvGuard::set("OPENHUMAN_WORKSPACE", tmp.path().to_str().unwrap());
+ let _workspace_guard = EnvVarGuard::set("OPENHUMAN_WORKSPACE", tmp.path().to_str().unwrap());
let req = Request::builder()
.method(Method::GET)
diff --git a/tests/json_rpc_e2e.rs b/tests/json_rpc_e2e.rs
index a20ab1a4a1..640fa92715 100644
--- a/tests/json_rpc_e2e.rs
+++ b/tests/json_rpc_e2e.rs
@@ -8,6 +8,9 @@ mod memory_module;
#[path = "support/tinyhumans_boot.rs"]
mod tinyhumans_boot;
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use std::collections::VecDeque;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
@@ -33,40 +36,6 @@ use openhuman_rpc::server::build_core_http_router;
const TEST_RPC_TOKEN: &str = "json-rpc-e2e-local-token";
static JSON_RPC_AUTH_INIT: OnceLock<()> = OnceLock::new();
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(v) => std::env::set_var(self.key, v),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
/// Serializes tests in this binary: `HOME` / `OPENHUMAN_WORKSPACE` / backend URL overrides are
/// process-global, so parallel tests would clobber each other and hit the wrong `config.toml` or
/// inherited `VITE_BACKEND_URL`.
diff --git a/tests/keyring_secretstore_e2e.rs b/tests/keyring_secretstore_e2e.rs
index de929057a7..b02b107e4f 100644
--- a/tests/keyring_secretstore_e2e.rs
+++ b/tests/keyring_secretstore_e2e.rs
@@ -1,3 +1,6 @@
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use openhuman_core::config::schema::{Config, StreamMode, TelegramConfig};
use openhuman_core::security::keyring;
use std::sync::OnceLock;
@@ -9,42 +12,6 @@ async fn env_lock() -> tokio::sync::MutexGuard<'static, ()> {
.await
}
-struct EnvGuard {
- key: &'static str,
- previous: Option<std::ffi::OsString>,
-}
-
-impl EnvGuard {
- fn set(key: &'static str, value: &std::path::Path) -> Self {
- let previous = std::env::var_os(key);
- unsafe {
- std::env::set_var(key, value);
- }
- Self { key, previous }
- }
-
- fn set_str(key: &'static str, value: &'static str) -> Self {
- let previous = std::env::var_os(key);
- unsafe {
- std::env::set_var(key, value);
- }
- Self { key, previous }
- }
-}
-
-impl Drop for EnvGuard {
- fn drop(&mut self) {
- match self.previous.take() {
- Some(value) => unsafe {
- std::env::set_var(self.key, value);
- },
- None => unsafe {
- std::env::remove_var(self.key);
- },
- }
- }
-}
-
#[tokio::test]
async fn config_secrets_roundtrip_via_keyring_backed_master_key_migration() {
let _guard = env_lock().await;
@@ -53,8 +20,8 @@ async fn config_secrets_roundtrip_via_keyring_backed_master_key_migration() {
let workspace_dir = openhuman_dir.join("workspace");
std::fs::create_dir_all(&workspace_dir).expect("workspace dir");
- let _keyring_backend = EnvGuard::set_str("OPENHUMAN_KEYRING_BACKEND", "file");
- let _workspace_override = EnvGuard::set("OPENHUMAN_WORKSPACE", &openhuman_dir);
+ let _keyring_backend = EnvVarGuard::set("OPENHUMAN_KEYRING_BACKEND", "file");
+ let _workspace_override = EnvVarGuard::set("OPENHUMAN_WORKSPACE", &openhuman_dir);
keyring::init_workspace(&workspace_dir);
let legacy_key_path = openhuman_dir.join(".secret_key");
diff --git a/tests/keyring_secretstore_fresh_e2e.rs b/tests/keyring_secretstore_fresh_e2e.rs
index 22af814b79..0d8b315ba5 100644
--- a/tests/keyring_secretstore_fresh_e2e.rs
+++ b/tests/keyring_secretstore_fresh_e2e.rs
@@ -1,3 +1,6 @@
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use openhuman_core::config::schema::{Config, StreamMode, TelegramConfig};
use openhuman_core::security::keyring;
use std::sync::{Mutex, OnceLock};
@@ -7,42 +10,6 @@ fn env_lock() -> std::sync::MutexGuard<'static, ()> {
LOCK.get_or_init(|| Mutex::new(())).lock().unwrap()
}
-struct EnvGuard {
- key: &'static str,
- previous: Option<std::ffi::OsString>,
-}
-
-impl EnvGuard {
- fn set(key: &'static str, value: &std::path::Path) -> Self {
- let previous = std::env::var_os(key);
- unsafe {
- std::env::set_var(key, value);
- }
- Self { key, previous }
- }
-
- fn set_str(key: &'static str, value: &'static str) -> Self {
- let previous = std::env::var_os(key);
- unsafe {
- std::env::set_var(key, value);
- }
- Self { key, previous }
- }
-}
-
-impl Drop for EnvGuard {
- fn drop(&mut self) {
- match self.previous.take() {
- Some(value) => unsafe {
- std::env::set_var(self.key, value);
- },
- None => unsafe {
- std::env::remove_var(self.key);
- },
- }
- }
-}
-
fn parse_keyring_payload(json: &str) -> serde_json::Value {
serde_json::from_str(json).expect("parse keyring json")
}
@@ -55,8 +22,8 @@ async fn config_secrets_create_master_key_in_keyring_on_fresh_install() {
let workspace_dir = openhuman_dir.join("workspace");
std::fs::create_dir_all(&workspace_dir).expect("workspace dir");
- let _keyring_backend = EnvGuard::set_str("OPENHUMAN_KEYRING_BACKEND", "file");
- let _workspace_override = EnvGuard::set("OPENHUMAN_WORKSPACE", &openhuman_dir);
+ let _keyring_backend = EnvVarGuard::set("OPENHUMAN_KEYRING_BACKEND", "file");
+ let _workspace_override = EnvVarGuard::set("OPENHUMAN_WORKSPACE", &openhuman_dir);
keyring::init_workspace(&workspace_dir);
let legacy_key_path = openhuman_dir.join(".secret_key");
diff --git a/tests/live_flows_demo_e2e.rs b/tests/live_flows_demo_e2e.rs
index 01bc013e41..50170d8ddb 100644
--- a/tests/live_flows_demo_e2e.rs
+++ b/tests/live_flows_demo_e2e.rs
@@ -19,6 +19,9 @@
//! OPENHUMAN_LIVE_USER_ID="<user-id>" \
//! cargo test --test live_flows_demo_e2e -- --ignored --nocapture
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use std::path::Path;
use std::sync::{Mutex, OnceLock};
use std::time::Duration;
@@ -33,31 +36,6 @@ static LIVE_E2E_ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
static LIVE_RPC_AUTH_INIT: OnceLock<()> = OnceLock::new();
const TEST_RPC_TOKEN: &str = "live-flows-demo-e2e-local-token";
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- // SAFETY: EnvVarGuard is only used after acquiring live_e2e_env_lock(),
- // which serializes process-global env mutations.
- unsafe { std::env::set_var(key, path.as_os_str()) };
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- // SAFETY: See set_to_path; teardown runs under the same lock.
- Some(v) => unsafe { std::env::set_var(self.key, v) },
- None => unsafe { std::env::remove_var(self.key) },
- }
- }
-}
-
fn live_e2e_env_lock() -> std::sync::MutexGuard<'static, ()> {
let mutex = LIVE_E2E_ENV_LOCK.get_or_init(|| Mutex::new(()));
match mutex.lock() {
diff --git a/tests/live_routing_e2e.rs b/tests/live_routing_e2e.rs
index 9ed6a1727b..667f0c8a4b 100644
--- a/tests/live_routing_e2e.rs
+++ b/tests/live_routing_e2e.rs
@@ -11,6 +11,9 @@
//! OPENHUMAN_LIVE_USER_ID="<user-id>" \
//! cargo test --test live_routing_e2e -- --ignored --nocapture
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use std::path::Path;
use std::sync::{Mutex, OnceLock};
use std::time::Duration;
@@ -27,33 +30,6 @@ static LIVE_E2E_ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
static LIVE_RPC_AUTH_INIT: OnceLock<()> = OnceLock::new();
const TEST_RPC_TOKEN: &str = "live-routing-e2e-local-token";
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- // SAFETY: EnvVarGuard is only used in tests that first acquire
- // live_e2e_env_lock(), which serializes process-global env mutations.
- unsafe { std::env::set_var(key, path.as_os_str()) };
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- // SAFETY: See EnvVarGuard::set_to_path; teardown runs under the same
- // live_e2e_env_lock() critical section as setup.
- Some(v) => unsafe { std::env::set_var(self.key, v) },
- // SAFETY: Guarded by live_e2e_env_lock(), preventing concurrent env access.
- None => unsafe { std::env::remove_var(self.key) },
- }
- }
-}
-
fn live_e2e_env_lock() -> std::sync::MutexGuard<'static, ()> {
let mutex = LIVE_E2E_ENV_LOCK.get_or_init(|| Mutex::new(()));
match mutex.lock() {
diff --git a/tests/memory_roundtrip_e2e.rs b/tests/memory_roundtrip_e2e.rs
index a4e1aaa66f..45cc157acd 100644
--- a/tests/memory_roundtrip_e2e.rs
+++ b/tests/memory_roundtrip_e2e.rs
@@ -36,6 +36,9 @@
//! `scripts/test-rust-e2e.sh` supplies both; a bare
//! `cargo test --test memory_roundtrip_e2e` supplies neither.
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
@@ -49,33 +52,6 @@ use openhuman_core::memory::rpc_models::{RecallContextRequest, RecallMemoriesReq
// ── Env isolation ────────────────────────────────────────────────────
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- // SAFETY: EnvVarGuard is only used in tests that first acquire
- // env_lock(), which serializes process-global env mutations.
- unsafe { std::env::set_var(key, path.as_os_str()) };
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- // SAFETY: See EnvVarGuard::set_to_path; teardown runs under the same
- // env_lock() critical section as setup.
- Some(v) => unsafe { std::env::set_var(self.key, v) },
- // SAFETY: Guarded by env_lock(), preventing concurrent env access.
- None => unsafe { std::env::remove_var(self.key) },
- }
- }
-}
-
/// Serialises tests: `HOME` + `OPENHUMAN_WORKSPACE` are process-global.
static ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
static MEMORY_SEAMS_INIT: OnceLock<()> = OnceLock::new();
diff --git a/tests/raw_coverage/agent_orchestration_e2e.rs b/tests/raw_coverage/agent_orchestration_e2e.rs
index 5cab0c8394..9cd710ec04 100644
--- a/tests/raw_coverage/agent_orchestration_e2e.rs
+++ b/tests/raw_coverage/agent_orchestration_e2e.rs
@@ -14,6 +14,7 @@
//! Aggregated into `tests/raw_coverage_all.rs` by `build.rs`. Run with:
//! `cargo test --test raw_coverage_all --features "$(bash scripts/ci/product-features.sh)" agent_orchestration_e2e`
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
@@ -44,40 +45,6 @@ static MEMORY_SEAMS_INIT: OnceLock<()> = OnceLock::new();
/// and a lock local to this file would isolate nothing.
static ENV_LOCK: &OnceLock<Mutex<()>> = &crate::SHARED_ENV_LOCK;
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
ENV_LOCK
.get_or_init(|| Mutex::new(()))
diff --git a/tests/raw_coverage/automation_scheduling_e2e.rs b/tests/raw_coverage/automation_scheduling_e2e.rs
index 98ac0fe6d9..59fabac2ab 100644
--- a/tests/raw_coverage/automation_scheduling_e2e.rs
+++ b/tests/raw_coverage/automation_scheduling_e2e.rs
@@ -12,6 +12,7 @@
//! Aggregated into `tests/raw_coverage_all.rs` by `build.rs`. Run with:
//! `cargo test --test raw_coverage_all --features "$(bash scripts/ci/product-features.sh)" automation_scheduling_e2e`
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Mutex, OnceLock};
@@ -38,40 +39,6 @@ static AUTH_INIT: OnceLock<()> = OnceLock::new();
/// and a lock local to this file would isolate nothing.
static ENV_LOCK: &OnceLock<Mutex<()>> = &crate::SHARED_ENV_LOCK;
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
ENV_LOCK
.get_or_init(|| Mutex::new(()))
diff --git a/tests/raw_coverage/channels_round26_raw_coverage_e2e.rs b/tests/raw_coverage/channels_round26_raw_coverage_e2e.rs
index 9c7bda6efb..e362ce9de6 100644
--- a/tests/raw_coverage/channels_round26_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/channels_round26_raw_coverage_e2e.rs
@@ -5,6 +5,7 @@
//!
//! Yuanbao biz-codec cases live in `vendor/tinychannels` (`yuanbao/proto_biz.rs`).
+use crate::env_guard::EnvVarGuard;
use axum::{
body::Bytes,
extract::State,
@@ -135,30 +136,7 @@ async fn telegram_media(
)
}
-struct EnvGuard {
- key: &'static str,
- prior: Option<String>,
-}
-
-impl EnvGuard {
- fn set(key: &'static str, value: String) -> Self {
- let prior = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, prior }
- }
-}
-
-impl Drop for EnvGuard {
- fn drop(&mut self) {
- if let Some(value) = self.prior.take() {
- std::env::set_var(self.key, value);
- } else {
- std::env::remove_var(self.key);
- }
- }
-}
-
-impl EnvGuard {
+impl EnvVarGuard {
fn unset(key: &'static str) -> Self {
let prior = std::env::var(key).ok();
std::env::remove_var(key);
@@ -177,8 +155,8 @@ fn env_lock() -> std::sync::MutexGuard<'static, ()> {
async fn telegram_loopback_covers_reaction_text_fallback_and_media_send_paths() {
let _env = env_lock();
let (base, state) = spawn_telegram_mock().await;
- let _guard = EnvGuard::set("OPENHUMAN_TELEGRAM_BOT_API_BASE", base);
- let _legacy_guard = EnvGuard::unset("OPENHUMAN_TELEGRAM_API_BASE");
+ let _guard = EnvVarGuard::set("OPENHUMAN_TELEGRAM_BOT_API_BASE", base);
+ let _legacy_guard = EnvVarGuard::unset("OPENHUMAN_TELEGRAM_API_BASE");
let channel = TelegramChannel::new("round26".to_string(), vec!["alice".to_string()], false);
channel
diff --git a/tests/raw_coverage/connectivity_raw_coverage_e2e.rs b/tests/raw_coverage/connectivity_raw_coverage_e2e.rs
index f3c7b1581f..49ff9f5eae 100644
--- a/tests/raw_coverage/connectivity_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/connectivity_raw_coverage_e2e.rs
@@ -5,6 +5,7 @@
//! both through exported production APIs so the E2E lcov captures the real
//! success and error branches.
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Mutex, OnceLock};
@@ -25,40 +26,6 @@ const TEST_RPC_TOKEN: &str = "connectivity-raw-coverage-e2e-token";
static AUTH_INIT: OnceLock<()> = OnceLock::new();
static ENV_LOCK: &OnceLock<Mutex<()>> = &crate::SHARED_ENV_LOCK;
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
struct TestHarness {
_tmp: TempDir,
_guards: Vec<EnvVarGuard>,
diff --git a/tests/raw_coverage/flows_lifecycle_e2e.rs b/tests/raw_coverage/flows_lifecycle_e2e.rs
index e6757ecd70..077f31d950 100644
--- a/tests/raw_coverage/flows_lifecycle_e2e.rs
+++ b/tests/raw_coverage/flows_lifecycle_e2e.rs
@@ -14,6 +14,7 @@
#![cfg(feature = "flows")]
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Mutex, OnceLock};
@@ -45,40 +46,6 @@ static AUTH_INIT: OnceLock<()> = OnceLock::new();
/// and a lock local to this file would isolate nothing.
static ENV_LOCK: &OnceLock<Mutex<()>> = &crate::SHARED_ENV_LOCK;
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
/// Serializes every case in this binary: `HOME` and the backend-URL overrides
/// are process-global, so two cases running in parallel would resolve each
/// other's `config.toml` and each other's flows database.
diff --git a/tests/raw_coverage/inference_local_admin_raw_coverage_e2e.rs b/tests/raw_coverage/inference_local_admin_raw_coverage_e2e.rs
index b8869421e5..4aca2f7915 100644
--- a/tests/raw_coverage/inference_local_admin_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/inference_local_admin_raw_coverage_e2e.rs
@@ -4,6 +4,7 @@
//! only. It must not call host Ollama, Piper, Whisper, Python, or MLX binaries,
//! and asserts that OpenHuman itself never launches one either.
+use crate::env_guard::EnvVarGuard;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
@@ -32,42 +33,6 @@ struct MockState {
ollama_models: Arc<Mutex<Vec<String>>>,
}
-struct EnvVarGuard {
- key: &'static str,
- previous: Option<std::ffi::OsString>,
-}
-
-impl EnvVarGuard {
- fn set(key: &'static str, value: impl AsRef<std::ffi::OsStr>) -> Self {
- let previous = std::env::var_os(key);
- // SAFETY: tests that mutate environment variables hold env_lock().
- unsafe { std::env::set_var(key, value) };
- Self { key, previous }
- }
-
- fn unset(key: &'static str) -> Self {
- let previous = std::env::var_os(key);
- // SAFETY: tests that mutate environment variables hold env_lock().
- unsafe { std::env::remove_var(key) };
- Self { key, previous }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.previous {
- Some(value) => {
- // SAFETY: tests that mutate environment variables hold env_lock().
- unsafe { std::env::set_var(self.key, value) }
- }
- None => {
- // SAFETY: tests that mutate environment variables hold env_lock().
- unsafe { std::env::remove_var(self.key) }
- }
- }
- }
-}
-
/// Process-wide lock serializing tests that mutate global environment
/// variables through [`EnvVarGuard`]. `cargo llvm-cov` runs integration tests
/// multi-threaded (it does not pass `--test-threads=1`), so without this guard
diff --git a/tests/raw_coverage/inference_local_controllers_rpc_raw_coverage_e2e.rs b/tests/raw_coverage/inference_local_controllers_rpc_raw_coverage_e2e.rs
index c5290a2138..cdd3fc81a1 100644
--- a/tests/raw_coverage/inference_local_controllers_rpc_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/inference_local_controllers_rpc_raw_coverage_e2e.rs
@@ -45,6 +45,7 @@
//! serialization mechanism, the same reasoning as `tests/agent_harness_e2e.rs:8-12`.
#![allow(clippy::await_holding_lock)]
+use crate::env_guard::EnvVarGuard;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
@@ -63,39 +64,6 @@ fn env_lock() -> std::sync::MutexGuard<'static, ()> {
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
-struct EnvVarGuard {
- key: &'static str,
- previous: Option<std::ffi::OsString>,
-}
-
-impl EnvVarGuard {
- fn set(key: &'static str, value: impl AsRef<std::ffi::OsStr>) -> Self {
- let previous = std::env::var_os(key);
- // SAFETY: every test here holds `env_lock()`, so no other aggregated
- // suite mutates the environment concurrently.
- unsafe { std::env::set_var(key, value) };
- Self { key, previous }
- }
-
- fn unset(key: &'static str) -> Self {
- let previous = std::env::var_os(key);
- // SAFETY: as above.
- unsafe { std::env::remove_var(key) };
- Self { key, previous }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.previous {
- // SAFETY: as above.
- Some(value) => unsafe { std::env::set_var(self.key, value) },
- // SAFETY: as above.
- None => unsafe { std::env::remove_var(self.key) },
- }
- }
-}
-
/// Point the Ollama health probe at a closed loopback port and clear the
/// ambient override, so `assets_status` fails its probe instantly instead of
/// dialling a real service (or waiting out a two-second timeout).
diff --git a/tests/raw_coverage/inference_provider_admin_round22_raw_coverage_e2e.rs b/tests/raw_coverage/inference_provider_admin_round22_raw_coverage_e2e.rs
index db5901531b..2d35f61c6b 100644
--- a/tests/raw_coverage/inference_provider_admin_round22_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/inference_provider_admin_round22_raw_coverage_e2e.rs
@@ -4,6 +4,7 @@
//! and temp PATH binaries. This suite must not invoke real Ollama, MLX, Python,
//! whisper, piper, local AI binaries, models, or downloads.
+use crate::env_guard::EnvVarGuard;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
@@ -44,42 +45,6 @@ struct SeenRequest {
body: Value,
}
-struct EnvVarGuard {
- key: &'static str,
- previous: Option<std::ffi::OsString>,
-}
-
-impl EnvVarGuard {
- fn set(key: &'static str, value: impl AsRef<std::ffi::OsStr>) -> Self {
- let previous = std::env::var_os(key);
- // SAFETY: this integration test is validated with --test-threads=1.
- unsafe { std::env::set_var(key, value) };
- Self { key, previous }
- }
-
- fn unset(key: &'static str) -> Self {
- let previous = std::env::var_os(key);
- // SAFETY: this integration test is validated with --test-threads=1.
- unsafe { std::env::remove_var(key) };
- Self { key, previous }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.previous {
- Some(value) => {
- // SAFETY: mutation is serialized by `env_lock()` (see below).
- unsafe { std::env::set_var(self.key, value) }
- }
- None => {
- // SAFETY: mutation is serialized by `env_lock()` (see below).
- unsafe { std::env::remove_var(self.key) }
- }
- }
- }
-}
-
/// Serializes the whole suite's process-global env access.
///
/// Several tests mutate `OPENHUMAN_WORKSPACE` / `OPENHUMAN_OLLAMA_BASE_URL` /
diff --git a/tests/raw_coverage/inference_provider_auth_e2e.rs b/tests/raw_coverage/inference_provider_auth_e2e.rs
index efd9ced06b..b5b58edb5e 100644
--- a/tests/raw_coverage/inference_provider_auth_e2e.rs
+++ b/tests/raw_coverage/inference_provider_auth_e2e.rs
@@ -27,6 +27,7 @@
//! Env is process-global and every aggregated suite shares one process, so
//! each case takes the **crate-wide** [`env_lock`] for its whole body.
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Mutex, OnceLock};
@@ -56,40 +57,6 @@ fn env_lock() -> std::sync::MutexGuard<'static, ()> {
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
-struct EnvVarGuard {
- key: &'static str,
- old: Option<std::ffi::OsString>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var_os(key);
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var_os(key);
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var_os(key);
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
/// `core::auth::RPC_TOKEN`
/// is a process-global `OnceLock` and `init_rpc_token` is idempotent, so inside
/// the aggregated binary the first suite to initialise pins the bearer. Use the
diff --git a/tests/raw_coverage/inference_provider_raw_coverage_e2e.rs b/tests/raw_coverage/inference_provider_raw_coverage_e2e.rs
index e4f2f965db..33beafc86d 100644
--- a/tests/raw_coverage/inference_provider_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/inference_provider_raw_coverage_e2e.rs
@@ -3,6 +3,7 @@
//! These tests use only loopback HTTP mocks and temp workspaces. They do not
//! require real Ollama, LM Studio, Piper, Whisper, Python, or model binaries.
+use crate::env_guard::EnvVarGuard;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
@@ -34,37 +35,8 @@ struct MockState {
requests: Arc<Mutex<Vec<(String, Option<String>, Value)>>>,
}
-struct EnvVarGuard {
- key: &'static str,
- previous: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set(key: &'static str, value: impl AsRef<std::ffi::OsStr>) -> Self {
- let previous = std::env::var(key).ok();
- // SAFETY: this test binary is run with --test-threads=1 in validation.
- unsafe { std::env::set_var(key, value) };
- Self { key, previous }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.previous {
- Some(value) => {
- // SAFETY: this test binary is run with --test-threads=1 in validation.
- unsafe { std::env::set_var(self.key, value) }
- }
- None => {
- // SAFETY: this test binary is run with --test-threads=1 in validation.
- unsafe { std::env::remove_var(self.key) }
- }
- }
- }
-}
-
// Serialize env mutation against every other aggregated suite via the
-// single crate-wide SHARED_ENV_LOCK (these tests use an `EnvGuard` struct
+// single crate-wide SHARED_ENV_LOCK (these tests use an `EnvVarGuard` struct
// that does not itself hold a lock). Poison is recovered so a panic
// elsewhere cannot wedge the suite.
fn __shared_env_lock() -> std::sync::MutexGuard<'static, ()> {
diff --git a/tests/raw_coverage/inference_voice_http_round23_raw_coverage_e2e.rs b/tests/raw_coverage/inference_voice_http_round23_raw_coverage_e2e.rs
index 5697520fc7..55efaf54d3 100644
--- a/tests/raw_coverage/inference_voice_http_round23_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/inference_voice_http_round23_raw_coverage_e2e.rs
@@ -4,6 +4,7 @@
//! only. It must not call host Ollama, MLX, Python, Whisper, Piper, models, or
//! download endpoints, and OpenHuman itself must not launch any of them.
+use crate::env_guard::EnvVarGuard;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
@@ -38,42 +39,6 @@ struct MockState {
requests: Arc<Mutex<Vec<(String, Value)>>>,
}
-struct EnvVarGuard {
- key: &'static str,
- previous: Option<std::ffi::OsString>,
-}
-
-impl EnvVarGuard {
- fn set(key: &'static str, value: impl AsRef<std::ffi::OsStr>) -> Self {
- let previous = std::env::var_os(key);
- // SAFETY: mutation is serialized by `env_lock()` (see below).
- unsafe { std::env::set_var(key, value) };
- Self { key, previous }
- }
-
- fn unset(key: &'static str) -> Self {
- let previous = std::env::var_os(key);
- // SAFETY: mutation is serialized by `env_lock()` (see below).
- unsafe { std::env::remove_var(key) };
- Self { key, previous }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.previous {
- Some(value) => {
- // SAFETY: mutation is serialized by `env_lock()` (see below).
- unsafe { std::env::set_var(self.key, value) }
- }
- None => {
- // SAFETY: mutation is serialized by `env_lock()` (see below).
- unsafe { std::env::remove_var(self.key) }
- }
- }
- }
-}
-
/// Serializes the whole suite's process-global env access.
///
/// `cargo test` and `cargo llvm-cov` run a binary's tests on multiple threads
diff --git a/tests/raw_coverage/learning_facets_e2e.rs b/tests/raw_coverage/learning_facets_e2e.rs
index d8e067f701..dd3d84e082 100644
--- a/tests/raw_coverage/learning_facets_e2e.rs
+++ b/tests/raw_coverage/learning_facets_e2e.rs
@@ -29,6 +29,7 @@
//! `cache_stats` is asserted as "at least mine", and `reset_cache` is checked
//! by what happened to this suite's two facets, not by the totals.
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
@@ -70,40 +71,6 @@ fn env_lock() -> std::sync::MutexGuard<'static, ()> {
// ── Env isolation ─────────────────────────────────────────────────────────
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
// ── Shared memory workspace ───────────────────────────────────────────────
/// The transport-only JSON-RPC router builds no core runtime context, so a
diff --git a/tests/raw_coverage/memory_sync_round23_raw_coverage_e2e.rs b/tests/raw_coverage/memory_sync_round23_raw_coverage_e2e.rs
index 3af15e45e6..e2356192dc 100644
--- a/tests/raw_coverage/memory_sync_round23_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/memory_sync_round23_raw_coverage_e2e.rs
@@ -50,6 +50,7 @@
//! assertions are gone from this test as a result; see the src-side gap note
//! in the migration report.
+use crate::env_guard::EnvVarGuard;
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
@@ -86,39 +87,6 @@ fn env_lock() -> std::sync::MutexGuard<'static, ()> {
.unwrap_or_else(|e| e.into_inner())
}
-struct EnvGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvGuard {
- fn set(key: &'static str, value: impl Into<String>) -> Self {
- let old = std::env::var(key).ok();
- unsafe { std::env::set_var(key, value.into()) };
- Self { key, old }
- }
-
- fn set_path(key: &'static str, value: &std::path::Path) -> Self {
- Self::set(key, value.to_string_lossy().into_owned())
- }
-
- #[allow(dead_code)]
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- unsafe { std::env::remove_var(key) };
- Self { key, old }
- }
-}
-
-impl Drop for EnvGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => unsafe { std::env::set_var(self.key, value) },
- None => unsafe { std::env::remove_var(self.key) },
- }
- }
-}
-
fn config_in(tmp: &TempDir) -> Config {
ensure_memory_seams();
let mut config = Config {
@@ -162,8 +130,8 @@ fn store_session(config: &Config) {
async fn composio_get_user_profile_refuses_cleanly_without_a_loaded_module() {
let _guard = env_lock();
let tmp = TempDir::new().expect("tempdir");
- let _workspace = EnvGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
- let _home = EnvGuard::set_path("HOME", tmp.path());
+ let _workspace = EnvVarGuard::set_path("OPENHUMAN_WORKSPACE", tmp.path());
+ let _home = EnvVarGuard::set_path("HOME", tmp.path());
let mut config = config_in(&tmp);
config.modules.enabled = false;
diff --git a/tests/raw_coverage/notification_platform_e2e.rs b/tests/raw_coverage/notification_platform_e2e.rs
index aea1ae4495..dcb913701a 100644
--- a/tests/raw_coverage/notification_platform_e2e.rs
+++ b/tests/raw_coverage/notification_platform_e2e.rs
@@ -17,6 +17,7 @@
//! ~/tinyhuman/ci-slot.sh cargo test --test raw_coverage_all \
//! --features "$(bash scripts/ci/product-features.sh)" notification_platform
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
@@ -75,34 +76,6 @@ fn ensure_rpc_auth() {
let _ = rpc_bearer();
}
-struct EnvGuard {
- key: &'static str,
- prev: Option<String>,
-}
-
-impl EnvGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let prev = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, prev }
- }
-
- fn unset(key: &'static str) -> Self {
- let prev = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, prev }
- }
-}
-
-impl Drop for EnvGuard {
- fn drop(&mut self) {
- match &self.prev {
- Some(v) => std::env::set_var(self.key, v),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
// ── mock backend ─────────────────────────────────────────────────────────────
/// What the mock should answer on `GET /announcements/latest`.
@@ -286,10 +259,10 @@ struct Harness {
openhuman_home: std::path::PathBuf,
mock_join: tokio::task::JoinHandle<()>,
rpc_join: tokio::task::JoinHandle<()>,
- _home: EnvGuard,
- _ws: EnvGuard,
- _backend: EnvGuard,
- _vite: EnvGuard,
+ _home: EnvVarGuard,
+ _ws: EnvVarGuard,
+ _backend: EnvVarGuard,
+ _vite: EnvVarGuard,
_tmp: tempfile::TempDir,
}
@@ -298,10 +271,10 @@ impl Harness {
let tmp = tempdir().expect("tempdir");
let home = tmp.path().to_path_buf();
let openhuman_home = home.join(".openhuman");
- let _home = EnvGuard::set_to_path("HOME", &home);
- let _ws = EnvGuard::unset("OPENHUMAN_WORKSPACE");
- let _backend = EnvGuard::unset("BACKEND_URL");
- let _vite = EnvGuard::unset("VITE_BACKEND_URL");
+ let _home = EnvVarGuard::set_to_path("HOME", &home);
+ let _ws = EnvVarGuard::unset("OPENHUMAN_WORKSPACE");
+ let _backend = EnvVarGuard::unset("BACKEND_URL");
+ let _vite = EnvVarGuard::unset("VITE_BACKEND_URL");
let (mock_addr, mock_join) = serve_ephemeral(mock_backend_router(state)).await;
write_test_config(&openhuman_home, &format!("http://{mock_addr}"));
diff --git a/tests/raw_coverage/session_store_e2e.rs b/tests/raw_coverage/session_store_e2e.rs
index 3548f4bf87..fdbecac848 100644
--- a/tests/raw_coverage/session_store_e2e.rs
+++ b/tests/raw_coverage/session_store_e2e.rs
@@ -25,6 +25,7 @@
//! destructive `test_reset` never ships — so the case here asserts the
//! absence, and the positive path is compiled in only when the feature is.
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
@@ -55,40 +56,6 @@ static ENV_LOCK: &OnceLock<Mutex<()>> = &crate::SHARED_ENV_LOCK;
// ── Env isolation ─────────────────────────────────────────────────────────
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
/// `HOME` / `OPENHUMAN_WORKSPACE` are process-global, so every case in this
/// binary is serialised behind one lock.
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
diff --git a/tests/raw_coverage/tool_registry_approval_raw_coverage_e2e.rs b/tests/raw_coverage/tool_registry_approval_raw_coverage_e2e.rs
index 3ea3f1dfe3..95a8eeb000 100644
--- a/tests/raw_coverage/tool_registry_approval_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/tool_registry_approval_raw_coverage_e2e.rs
@@ -5,6 +5,7 @@
//! direct public API calls cover persistence/redaction/provider branches that
//! are otherwise only indirectly reachable from the controllers.
+use crate::env_guard::EnvVarGuard;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};
@@ -36,40 +37,6 @@ const TEST_RPC_TOKEN: &str = "tool-registry-approval-raw-e2e-token";
static AUTH_INIT: OnceLock<()> = OnceLock::new();
static ENV_LOCK: &OnceLock<Mutex<()>> = &crate::SHARED_ENV_LOCK;
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
struct TestHarness {
_tmp: TempDir,
_guards: Vec<EnvVarGuard>,
diff --git a/tests/raw_coverage/tools_composio_large_round25_raw_coverage_e2e.rs b/tests/raw_coverage/tools_composio_large_round25_raw_coverage_e2e.rs
index 103cf7262b..d0271a961f 100644
--- a/tests/raw_coverage/tools_composio_large_round25_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/tools_composio_large_round25_raw_coverage_e2e.rs
@@ -4,6 +4,7 @@
//! This test uses debug-only loopback base overrides and temp config stores so
//! no real network, keychain, or backend session is required.
+use crate::env_guard::EnvVarGuard;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
@@ -43,44 +44,10 @@ struct MockState {
requests: Arc<Mutex<Vec<RecordedRequest>>>,
}
-struct EnvGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvGuard {
- fn set(key: &'static str, value: impl AsRef<str>) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value.as_ref());
- Self { key, old }
- }
-
- fn set_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
struct Harness {
_tmp: TempDir,
config: Config,
- _guards: Vec<EnvGuard>,
+ _guards: Vec<EnvVarGuard>,
}
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
@@ -103,22 +70,22 @@ async fn setup_direct_config(base: &str) -> Harness {
std::fs::create_dir_all(&workspace).expect("workspace dir");
let guards = vec![
- EnvGuard::set_path("OPENHUMAN_WORKSPACE", &root),
- EnvGuard::set_path("HOME", tmp.path()),
- EnvGuard::set("OPENHUMAN_KEYRING_BACKEND", "file"),
- EnvGuard::set(
+ EnvVarGuard::set_path("OPENHUMAN_WORKSPACE", &root),
+ EnvVarGuard::set_path("HOME", tmp.path()),
+ EnvVarGuard::set("OPENHUMAN_KEYRING_BACKEND", "file"),
+ EnvVarGuard::set(
"OPENHUMAN_COMPOSIO_DIRECT_BASE_V2",
format!("{base}/api/v2"),
),
- EnvGuard::set(
+ EnvVarGuard::set(
"OPENHUMAN_COMPOSIO_DIRECT_BASE_V3",
format!("{base}/api/v3"),
),
- EnvGuard::unset("BACKEND_URL"),
- EnvGuard::unset("VITE_BACKEND_URL"),
- EnvGuard::unset("OPENHUMAN_API_URL"),
- EnvGuard::unset("OPENHUMAN_CORE_RPC_URL"),
- EnvGuard::unset("OPENHUMAN_CORE_PORT"),
+ EnvVarGuard::unset("BACKEND_URL"),
+ EnvVarGuard::unset("VITE_BACKEND_URL"),
+ EnvVarGuard::unset("OPENHUMAN_API_URL"),
+ EnvVarGuard::unset("OPENHUMAN_CORE_RPC_URL"),
+ EnvVarGuard::unset("OPENHUMAN_CORE_PORT"),
];
let mut config = Config {
diff --git a/tests/raw_coverage/tools_composio_network_leftovers_raw_coverage_e2e.rs b/tests/raw_coverage/tools_composio_network_leftovers_raw_coverage_e2e.rs
index 629c335e15..5cd97ac609 100644
--- a/tests/raw_coverage/tools_composio_network_leftovers_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/tools_composio_network_leftovers_raw_coverage_e2e.rs
@@ -1,6 +1,7 @@
//! Round20 raw/E2E coverage for Composio tool leftovers and adjacent
//! network-tool branches. All HTTP traffic stays on loopback mocks.
+use crate::env_guard::EnvVarGuard;
use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
@@ -41,38 +42,10 @@ struct MockState {
connections_fail: Arc<Mutex<bool>>,
}
-struct EnvGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvGuard {
- fn set_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
struct Harness {
_tmp: TempDir,
config: Config,
- _guards: Vec<EnvGuard>,
+ _guards: Vec<EnvVarGuard>,
}
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
@@ -99,14 +72,14 @@ async fn setup_config() -> Harness {
std::fs::create_dir_all(&workspace).expect("workspace dir");
let guards = vec![
- EnvGuard::set_path("OPENHUMAN_WORKSPACE", &root),
- EnvGuard::set_path("HOME", tmp.path()),
- EnvGuard::unset("BACKEND_URL"),
- EnvGuard::unset("VITE_BACKEND_URL"),
- EnvGuard::unset("OPENHUMAN_API_URL"),
- EnvGuard::unset("OPENHUMAN_CORE_RPC_URL"),
- EnvGuard::unset("OPENHUMAN_CORE_PORT"),
- EnvGuard::unset("OPENHUMAN_LSP_ENABLED"),
+ EnvVarGuard::set_path("OPENHUMAN_WORKSPACE", &root),
+ EnvVarGuard::set_path("HOME", tmp.path()),
+ EnvVarGuard::unset("BACKEND_URL"),
+ EnvVarGuard::unset("VITE_BACKEND_URL"),
+ EnvVarGuard::unset("OPENHUMAN_API_URL"),
+ EnvVarGuard::unset("OPENHUMAN_CORE_RPC_URL"),
+ EnvVarGuard::unset("OPENHUMAN_CORE_PORT"),
+ EnvVarGuard::unset("OPENHUMAN_LSP_ENABLED"),
];
let mut config = Config {
diff --git a/tests/raw_coverage/w4_shared/mod.rs b/tests/raw_coverage/w4_shared/mod.rs
index ed1da7032f..125765c0bf 100644
--- a/tests/raw_coverage/w4_shared/mod.rs
+++ b/tests/raw_coverage/w4_shared/mod.rs
@@ -18,6 +18,7 @@
//! initialised first. `rpc_token()` below asks for whatever is actually
//! active. See `~/tinyhuman/bugs/e2e-wave-raw-coverage-shared-rpc-token.md`.
+use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
@@ -76,40 +77,6 @@ fn rpc_token() -> &'static str {
})
}
-pub struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- pub fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- pub fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- pub fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(v) => std::env::set_var(self.key, v),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
pub async fn serve_on_ephemeral(
app: Router,
) -> (
diff --git a/tests/raw_coverage/worker_b_raw_coverage_e2e.rs b/tests/raw_coverage/worker_b_raw_coverage_e2e.rs
index febdef332c..3e3780e8a9 100644
--- a/tests/raw_coverage/worker_b_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/worker_b_raw_coverage_e2e.rs
@@ -5,6 +5,7 @@
//! to drive implementation branches that the controller reachability tests only
//! touch at validation boundaries.
+use crate::env_guard::EnvVarGuard;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::path::Path;
@@ -41,40 +42,6 @@ struct MockState {
requests: Arc<Mutex<Vec<Value>>>,
}
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set_to_path(key: &'static str, path: &Path) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, path.as_os_str());
- Self { key, old }
- }
-
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- std::env::set_var(key, value);
- Self { key, old }
- }
-
- fn unset(key: &'static str) -> Self {
- let old = std::env::var(key).ok();
- std::env::remove_var(key);
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => std::env::set_var(self.key, value),
- None => std::env::remove_var(self.key),
- }
- }
-}
-
struct TestHarness {
_tmp: TempDir,
_guards: Vec<EnvVarGuard>,
diff --git a/tests/skills_mcp_registry_live.rs b/tests/skills_mcp_registry_live.rs
index 9bc6597e43..24a8b5a21d 100644
--- a/tests/skills_mcp_registry_live.rs
+++ b/tests/skills_mcp_registry_live.rs
@@ -16,6 +16,9 @@
//!
//! `OPENHUMAN_LIVE_MCP_QUERY` picks the MCP registry search (default `everything`).
+#[path = "support/env_guard.rs"]
+mod env_guard;
+use env_guard::EnvVarGuard;
use std::collections::BTreeMap;
use std::net::SocketAddr;
use std::path::Path;
@@ -41,28 +44,6 @@ fn env_lock() -> std::sync::MutexGuard<'static, ()> {
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
-struct EnvVarGuard {
- key: &'static str,
- old: Option<String>,
-}
-
-impl EnvVarGuard {
- fn set(key: &'static str, value: &str) -> Self {
- let old = std::env::var(key).ok();
- unsafe { std::env::set_var(key, value) };
- Self { key, old }
- }
-}
-
-impl Drop for EnvVarGuard {
- fn drop(&mut self) {
- match &self.old {
- Some(value) => unsafe { std::env::set_var(self.key, value) },
- None => unsafe { std::env::remove_var(self.key) },
- }
- }
-}
-
/// A core RPC stack over a throwaway `HOME`, with no registry overrides.
struct LiveStack {
rpc_base: String,