Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions loom-daemon/src/env_test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
25 changes: 0 additions & 25 deletions loom-daemon/src/role_runner/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,31 +160,6 @@ fn had_ever_succeeded_is_independent_per_role_and_root() {
// `LOOM_RUNTIME_<ROLE>` 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<String>);

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() {
Expand Down
3 changes: 2 additions & 1 deletion loom-daemon/src/role_runner/tests/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

// ===================================================================
Expand Down Expand Up @@ -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();
Expand Down
29 changes: 2 additions & 27 deletions loom-daemon/src/sweep_registry/dispatch/tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,32 +15,6 @@ use tempfile::tempdir;
// `LOOM_RUNTIME_<ROLE>` 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<String>);

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() {
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion loom-daemon/src/sweep_registry/guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,7 @@ pub(crate) fn repo_slug_from_env() -> Option<String> {
)]
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;
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion loom-daemon/src/sweep_registry/quarantine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
40 changes: 0 additions & 40 deletions loom-daemon/src/sweep_registry/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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=<unset>`.
///
/// 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<String>);

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)
Expand Down
3 changes: 2 additions & 1 deletion loom-daemon/src/sweep_registry/watchdog/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 2 additions & 4 deletions loom-daemon/src/watchdog/escalate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>);

impl PinnedEscalateKnob {
Expand Down
Loading