diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 5b772e0dbe..6fde3e982a 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2168,9 +2168,11 @@ version = "0.0.0" dependencies = [ "cc", "clap", + "codex-arg0", "codex-core", "codex-protocol", "codex-utils-absolute-path", + "ctor 0.6.3", "landlock", "libc", "pkg-config", diff --git a/codex-rs/linux-sandbox/Cargo.toml b/codex-rs/linux-sandbox/Cargo.toml index 88da2d7479..7c4dbff496 100644 --- a/codex-rs/linux-sandbox/Cargo.toml +++ b/codex-rs/linux-sandbox/Cargo.toml @@ -28,6 +28,8 @@ serde_json = { workspace = true } url = { workspace = true } [target.'cfg(target_os = "linux")'.dev-dependencies] +codex-arg0 = { workspace = true } +ctor = { workspace = true } pretty_assertions = { workspace = true } tempfile = { workspace = true } tokio = { workspace = true, features = [ diff --git a/codex-rs/linux-sandbox/tests/all.rs b/codex-rs/linux-sandbox/tests/all.rs index 7e136e4cce..05da90fbb7 100644 --- a/codex-rs/linux-sandbox/tests/all.rs +++ b/codex-rs/linux-sandbox/tests/all.rs @@ -1,3 +1,5 @@ +#![cfg(target_os = "linux")] + // Single integration test binary that aggregates all test modules. // The submodules live in `tests/suite/`. mod suite; diff --git a/codex-rs/linux-sandbox/tests/suite/mod.rs b/codex-rs/linux-sandbox/tests/suite/mod.rs index 8ef44a61a4..8db365575b 100644 --- a/codex-rs/linux-sandbox/tests/suite/mod.rs +++ b/codex-rs/linux-sandbox/tests/suite/mod.rs @@ -1,3 +1,47 @@ // Aggregates all former standalone integration tests as modules. +use codex_arg0::Arg0PathEntryGuard; +use codex_arg0::arg0_dispatch; +use ctor::ctor; +use tempfile::TempDir; + +struct TestCodexAliasesGuard { + _codex_home: TempDir, + _arg0: Arg0PathEntryGuard, +} + +const CODEX_HOME_ENV_VAR: &str = "CODEX_HOME"; + +// This code runs before any other tests are run. +// It allows the test binary to behave like codex-linux-sandbox based on arg0. +#[ctor] +pub static CODEX_ALIASES_TEMP_DIR: TestCodexAliasesGuard = unsafe { + #[allow(clippy::unwrap_used)] + let codex_home = tempfile::Builder::new() + .prefix("codex-linux-sandbox-tests") + .tempdir() + .unwrap(); + let previous_codex_home = std::env::var_os(CODEX_HOME_ENV_VAR); + + // Safety: #[ctor] runs before test threads start. + unsafe { + std::env::set_var(CODEX_HOME_ENV_VAR, codex_home.path()); + } + #[allow(clippy::unwrap_used)] + let arg0 = arg0_dispatch().unwrap(); + match previous_codex_home.as_ref() { + Some(value) => unsafe { + std::env::set_var(CODEX_HOME_ENV_VAR, value); + }, + None => unsafe { + std::env::remove_var(CODEX_HOME_ENV_VAR); + }, + } + + TestCodexAliasesGuard { + _codex_home: codex_home, + _arg0: arg0, + } +}; + mod landlock; mod managed_proxy;