diff --git a/loom-daemon/src/env_test_support.rs b/loom-daemon/src/env_test_support.rs index 539ffd6bcc..f5d9ac0cb0 100644 --- a/loom-daemon/src/env_test_support.rs +++ b/loom-daemon/src/env_test_support.rs @@ -82,6 +82,22 @@ mod tests { std::env::remove_var(A); } + #[cfg(unix)] + #[test] + #[serial] + fn clear_restores_non_utf8_bytes() { + use std::os::unix::ffi::OsStrExt; + + const KEY: &str = "LOOM_ENV_TEST_SUPPORT_NON_UTF8"; + const ORIGINAL: &[u8] = b"original-\xff-path"; + let _seed = EnvGuard::set(KEY, OsStr::from_bytes(ORIGINAL)); + { + let _clear = EnvGuard::clear(&[KEY]); + assert!(std::env::var_os(KEY).is_none()); + } + assert_eq!(std::env::var_os(KEY).unwrap().as_bytes(), ORIGINAL); + } + #[test] #[serial] fn set_overrides_and_repeated_keys_restore_the_original() { diff --git a/loom-daemon/src/role_runner/tests.rs b/loom-daemon/src/role_runner/tests.rs index ce48a07129..faccace9c2 100644 --- a/loom-daemon/src/role_runner/tests.rs +++ b/loom-daemon/src/role_runner/tests.rs @@ -160,31 +160,6 @@ fn had_ever_succeeded_is_independent_per_role_and_root() { // `LOOM_RUNTIME_` pins must go too. use crate::runtime_selection_test_support::ClearedRuntimeSelectionEnv as ClearedLoomRuntimeEnv; -/// As [`ClearedLoomRuntimeEnv`] but for `GH_CONFIG_DIR` (#5508): the test -/// process may itself be running under a `GH_CONFIG_DIR` (a developer -/// shell, or the daemon's own #4458 process-global default), which would -/// otherwise leak into a spawned child's environment and make the -/// "unregistered root leaves GH_CONFIG_DIR untouched" test observe an -/// ambient value instead of a genuine absence. -struct ClearedGhConfigDirEnv(Option); - -impl ClearedGhConfigDirEnv { - fn new() -> Self { - let prior = std::env::var("GH_CONFIG_DIR").ok(); - std::env::remove_var("GH_CONFIG_DIR"); - Self(prior) - } -} - -impl Drop for ClearedGhConfigDirEnv { - fn drop(&mut self) { - match self.0.take() { - Some(v) => std::env::set_var("GH_CONFIG_DIR", v), - None => std::env::remove_var("GH_CONFIG_DIR"), - } - } -} - #[test] #[serial] fn mixed_runtime_role_launch_is_admitted_and_pinned_before_spawn() { diff --git a/loom-daemon/src/role_runner/tests/invoke.rs b/loom-daemon/src/role_runner/tests/invoke.rs index 7657ed9417..deb305eda4 100644 --- a/loom-daemon/src/role_runner/tests/invoke.rs +++ b/loom-daemon/src/role_runner/tests/invoke.rs @@ -6,6 +6,7 @@ //! in front of every tick). use super::*; +use crate::env_test_support::EnvGuard; // #9548: gate-reaching tests hold the default serial key; see `crate::write_scope_test_support`. // =================================================================== @@ -312,7 +313,7 @@ fn run_role_with_timeout_forwards_owner_gh_config_dir_for_a_registered_root() { #[test] #[serial] fn run_role_with_timeout_leaves_gh_config_dir_untouched_for_an_unregistered_root() { - let _env_guard = ClearedGhConfigDirEnv::new(); + let _env_guard = EnvGuard::clear(&["GH_CONFIG_DIR"]); crate::credential_preflight::clear_owner_root_registry(); let tmp = tempfile::tempdir().unwrap(); let root = tmp.path().to_path_buf(); diff --git a/loom-daemon/src/sweep_registry/dispatch/tests.rs b/loom-daemon/src/sweep_registry/dispatch/tests.rs index b22377f0f8..532ade9909 100644 --- a/loom-daemon/src/sweep_registry/dispatch/tests.rs +++ b/loom-daemon/src/sweep_registry/dispatch/tests.rs @@ -1,4 +1,5 @@ use super::*; +use crate::env_test_support::EnvGuard; use crate::sweep_registry::test_support::*; use serial_test::serial; use std::os::unix::fs::PermissionsExt; @@ -14,32 +15,6 @@ use tempfile::tempdir; // `LOOM_RUNTIME_` pin that decides the same binding (#9360). use crate::runtime_selection_test_support::ClearedRuntimeSelectionEnv as ClearedLoomRuntimeEnv; -/// As [`ClearedLoomRuntimeEnv`] but for `GH_CONFIG_DIR` (#6529): the test -/// process — or a PREVIOUS test in this same `#[serial]` suite — may -/// itself have a `GH_CONFIG_DIR` set, which would otherwise leak into a -/// spawned fixture child's environment and make the "unregistered root -/// leaves GH_CONFIG_DIR untouched" test below observe an ambient value -/// instead of a genuine absence. Mirrors `role_runner::tests:: -/// ClearedGhConfigDirEnv`, added for the same reason by #5522. -struct ClearedGhConfigDirEnv(Option); - -impl ClearedGhConfigDirEnv { - fn new() -> Self { - let prior = std::env::var("GH_CONFIG_DIR").ok(); - std::env::remove_var("GH_CONFIG_DIR"); - Self(prior) - } -} - -impl Drop for ClearedGhConfigDirEnv { - fn drop(&mut self) { - match self.0.take() { - Some(v) => std::env::set_var("GH_CONFIG_DIR", v), - None => std::env::remove_var("GH_CONFIG_DIR"), - } - } -} - #[test] #[serial] fn runtime_rejection_precedes_every_dispatch_side_effect() { @@ -799,7 +774,7 @@ fn dispatch_forwards_owner_gh_config_dir_for_a_registered_root() { #[test] #[serial] fn dispatch_leaves_gh_config_dir_untouched_for_an_unregistered_root() { - let _env_guard = ClearedGhConfigDirEnv::new(); + let _env_guard = EnvGuard::clear(&["GH_CONFIG_DIR"]); let dir = tempdir().unwrap(); crate::credential_preflight::clear_owner_root_registry(); diff --git a/loom-daemon/src/sweep_registry/guards.rs b/loom-daemon/src/sweep_registry/guards.rs index 9ca8730195..d2da9385f9 100644 --- a/loom-daemon/src/sweep_registry/guards.rs +++ b/loom-daemon/src/sweep_registry/guards.rs @@ -1959,6 +1959,7 @@ pub(crate) fn repo_slug_from_env() -> Option { )] mod tests { use super::*; + use crate::env_test_support::EnvGuard; use crate::sweep_registry::test_support::*; use serial_test::serial; use std::os::unix::fs::PermissionsExt; @@ -2749,7 +2750,7 @@ exit 0 // process-wide for the daemon, #4458) and the assertion below fails // even though the no-op production behavior it exercises is // unaffected. - let _env_guard = ClearedGhConfigDirEnv::new(); + let _env_guard = EnvGuard::clear(&["GH_CONFIG_DIR"]); crate::credential_preflight::clear_owner_root_registry(); let dir = tempdir().unwrap(); let gh_log = dir.path().join("gh.log"); diff --git a/loom-daemon/src/sweep_registry/quarantine.rs b/loom-daemon/src/sweep_registry/quarantine.rs index 713c1250f1..e38040fccf 100644 --- a/loom-daemon/src/sweep_registry/quarantine.rs +++ b/loom-daemon/src/sweep_registry/quarantine.rs @@ -1172,6 +1172,7 @@ impl SweepRegistry { )] mod tests { use super::*; + use crate::env_test_support::EnvGuard; use crate::sweep_registry::test_support::*; use serial_test::serial; use std::os::unix::fs::PermissionsExt; @@ -2527,7 +2528,7 @@ exit 0 // process-wide for the daemon, #4458) and the assertion below fails // even though the no-op production behavior it exercises is // unaffected. - let _env_guard = ClearedGhConfigDirEnv::new(); + let _env_guard = EnvGuard::clear(&["GH_CONFIG_DIR"]); crate::credential_preflight::clear_owner_root_registry(); let dir = tempdir().unwrap(); let gh_log = dir.path().join("gh.log"); diff --git a/loom-daemon/src/sweep_registry/test_support.rs b/loom-daemon/src/sweep_registry/test_support.rs index 8020d438ad..1ea6241815 100644 --- a/loom-daemon/src/sweep_registry/test_support.rs +++ b/loom-daemon/src/sweep_registry/test_support.rs @@ -1115,46 +1115,6 @@ pub(crate) fn install_fake_gh_env_logger( fake_gh } -/// RAII guard that scrubs `GH_CONFIG_DIR` from the *test process's own* -/// environment for the lifetime of the guard, restoring whatever value (or -/// absence) preceded it on drop. -/// -/// Mirrors `role_runner.rs`'s private `ClearedGhConfigDirEnv` (added for -/// #5508), shared here for the `sweep_registry` "unregistered workspace root -/// is a no-op" tests (guards.rs / quarantine.rs / watchdog.rs) that spawn -/// [`install_fake_gh_env_logger`]'s fake `gh` and assert its child inherits -/// `GH_CONFIG_DIR=`. -/// -/// Without this guard, the assertion only holds on a clean CI runner. A real -/// Loom fleet worker host runs the daemon (and any `cargo nextest` invoked -/// directly on that host) with `GH_CONFIG_DIR` already exported -/// host/process-wide (#4458) — that ambient value leaks straight through -/// [`install_fake_gh_env_logger`]'s fake `gh` script, which logs its own -/// *inherited* `GH_CONFIG_DIR`, and the "unset" assertion fails even though -/// the production no-op behavior it exercises (`credential_preflight.rs`'s -/// "child inherits the process-global `GH_CONFIG_DIR` untouched" contract) -/// is unchanged (#5651). Every call site pairs this with the existing -/// `#[serial]` attribute so the process-global scrub cannot race a -/// concurrent test that depends on `GH_CONFIG_DIR`. -pub(crate) struct ClearedGhConfigDirEnv(Option); - -impl ClearedGhConfigDirEnv { - pub(crate) fn new() -> Self { - let prior = std::env::var("GH_CONFIG_DIR").ok(); - std::env::remove_var("GH_CONFIG_DIR"); - Self(prior) - } -} - -impl Drop for ClearedGhConfigDirEnv { - fn drop(&mut self) { - match self.0.take() { - Some(v) => std::env::set_var("GH_CONFIG_DIR", v), - None => std::env::remove_var("GH_CONFIG_DIR"), - } - } -} - // --- dispatch-time live-claim guard (Issue #4556) --- /// Seed the machine-level sweep journal (confined to this fixture's tempdir) diff --git a/loom-daemon/src/sweep_registry/watchdog/tests.rs b/loom-daemon/src/sweep_registry/watchdog/tests.rs index eccf112f53..1350d83395 100644 --- a/loom-daemon/src/sweep_registry/watchdog/tests.rs +++ b/loom-daemon/src/sweep_registry/watchdog/tests.rs @@ -46,6 +46,7 @@ //! whole point. use super::*; +use crate::env_test_support::EnvGuard; use crate::sweep_registry::test_support::*; use serial_test::serial; use std::os::unix::fs::PermissionsExt; @@ -2423,7 +2424,7 @@ fn post_watchdog_gaveup_comment_is_a_noop_for_unregistered_workspace_root() { // process-wide for the daemon, #4458) and the assertion below fails // even though the no-op production behavior it exercises is // unaffected. - let _env_guard = ClearedGhConfigDirEnv::new(); + let _env_guard = EnvGuard::clear(&["GH_CONFIG_DIR"]); crate::credential_preflight::clear_owner_root_registry(); let dir = tempdir().unwrap(); let gh_log = dir.path().join("gh.log"); diff --git a/loom-daemon/src/watchdog/escalate.rs b/loom-daemon/src/watchdog/escalate.rs index a54884abac..5b76c80295 100644 --- a/loom-daemon/src/watchdog/escalate.rs +++ b/loom-daemon/src/watchdog/escalate.rs @@ -341,10 +341,8 @@ mod tests { } } - /// Save/restore guard for the knob a live daemon on this host may have - /// exported into this very process (#8328) — the `ClearedGhConfigDirEnv` - /// pattern from `role_runner::tests`: pin the exact hostile value, put - /// back whatever was there afterward. + /// Pin the knob a live daemon may have exported into this process + /// (#8328), and restore its previous value on drop. struct PinnedEscalateKnob(Option); impl PinnedEscalateKnob {