test(core): isolate temp fixtures from ancestor state

This commit is contained in:
Adam Perry
2026-06-09 17:42:54 +00:00
parent d055940f16
commit 187dabf3bc
4 changed files with 36 additions and 14 deletions
@@ -36,6 +36,7 @@ use codex_protocol::models::BUILT_IN_PERMISSION_PROFILE_WORKSPACE;
use codex_protocol::models::PermissionProfile;
use codex_protocol::protocol::AskForApproval;
use codex_utils_absolute_path::AbsolutePathBuf;
use core_test_support::tempdir_with_git_boundary;
use pretty_assertions::assert_eq;
use std::collections::BTreeMap;
use std::collections::HashMap;
@@ -2671,8 +2672,8 @@ async fn project_layer_is_added_when_dot_codex_exists_without_config_toml() -> s
#[tokio::test]
async fn codex_home_is_not_loaded_as_project_layer_from_home_dir() -> std::io::Result<()> {
let tmp = tempdir()?;
let home_dir = tmp.path().join("home");
let tmp = tempdir_with_git_boundary()?;
let home_dir = tmp.path().join("fixture/home");
let codex_home = home_dir.join(".codex");
tokio::fs::create_dir_all(&codex_home).await?;
tokio::fs::write(
@@ -2789,10 +2790,12 @@ async fn codex_home_within_project_tree_is_not_double_loaded() -> std::io::Resul
#[tokio::test]
async fn project_layers_disabled_when_untrusted_or_unknown() -> std::io::Result<()> {
let tmp = tempdir()?;
let project_root = tmp.path().join("project");
let tmp = tempdir_with_git_boundary()?;
let fixture_root = tmp.path().join("fixture");
let project_root = fixture_root.join("project");
let nested = project_root.join("child");
tokio::fs::create_dir_all(nested.join(".codex")).await?;
tokio::fs::create_dir(project_root.join(".git")).await?;
tokio::fs::write(
nested.join(".codex").join(CONFIG_TOML_FILE),
r#"foo = "child"
@@ -2803,7 +2806,7 @@ profile = "ignored"
let cwd = AbsolutePathBuf::from_absolute_path(&nested)?;
let codex_home_untrusted = tmp.path().join("home_untrusted");
let codex_home_untrusted = fixture_root.join("home_untrusted");
tokio::fs::create_dir_all(&codex_home_untrusted).await?;
make_config_for_test(
&codex_home_untrusted,
@@ -2860,7 +2863,7 @@ profile = "ignored"
let empty_warnings: &[String] = &[];
assert_eq!(layers_untrusted.startup_warnings(), Some(empty_warnings));
let codex_home_unknown = tmp.path().join("home_unknown");
let codex_home_unknown = fixture_root.join("home_unknown");
tokio::fs::create_dir_all(&codex_home_unknown).await?;
tokio::fs::write(
codex_home_unknown.join(CONFIG_TOML_FILE),
+5 -3
View File
@@ -9,8 +9,8 @@ use codex_git_utils::recent_commits;
use codex_git_utils::resolve_root_git_project_for_trust;
use codex_utils_path::normalize_for_path_comparison;
use core_test_support::PathBufExt;
use core_test_support::PathExt;
use core_test_support::skip_if_sandbox;
use core_test_support::tempdir_with_git_boundary;
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
@@ -479,9 +479,11 @@ async fn test_get_git_working_tree_state_branch_fallback() {
#[tokio::test]
async fn resolve_root_git_project_for_trust_returns_none_outside_repo() {
let tmp = TempDir::new().expect("tempdir");
let tmp = tempdir_with_git_boundary().expect("tempdir");
let outside = tmp.path().join("outside");
fs::create_dir(&outside).expect("create outside dir");
assert!(
resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &tmp.path().abs())
resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &outside.abs())
.await
.is_none()
);
+10 -5
View File
@@ -21,6 +21,7 @@ use codex_protocol::protocol::SessionSource;
use codex_thread_store::StoredThread;
use core_test_support::PathBufExt;
use core_test_support::PathExt;
use core_test_support::tempdir_with_git_boundary;
use pretty_assertions::assert_eq;
use std::fs;
use std::path::PathBuf;
@@ -236,9 +237,11 @@ fn fixed_section_budgets_apply_per_section_without_total_blob_truncation() {
#[tokio::test]
async fn workspace_section_requires_meaningful_structure() {
let cwd = TempDir::new().expect("tempdir");
let root = tempdir_with_git_boundary().expect("tempdir");
let cwd = root.path().join("cwd");
fs::create_dir(&cwd).expect("create cwd");
assert_eq!(
build_workspace_section_with_user_root(&cwd.path().abs(), /*user_root*/ None).await,
build_workspace_section_with_user_root(&cwd.abs(), /*user_root*/ None).await,
None
);
}
@@ -282,12 +285,14 @@ async fn workspace_section_includes_user_root_tree_when_distinct() {
#[tokio::test]
async fn recent_work_section_groups_threads_by_cwd() {
let root = TempDir::new().expect("tempdir");
let repo = root.path().join("repo");
let tmp = tempdir_with_git_boundary().expect("tempdir");
let root = tmp.path().join("fixture");
let repo = root.join("repo");
let workspace_a = repo.join("workspace-a");
let workspace_b = repo.join("workspace-b");
let outside = root.path().join("outside");
let outside = root.join("outside");
fs::create_dir(&root).expect("create fixture root");
fs::create_dir(&repo).expect("create repo dir");
Command::new("git")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
+12
View File
@@ -135,6 +135,18 @@ impl TempDirExt for TempDir {
}
}
/// Creates a temporary parent that stops `.git` ancestor searches from escaping
/// into shared temporary-directory state.
///
/// Tests should place their fixture in a child of the returned directory. The
/// invalid `.git` file acts as an ancestor-search boundary without resolving as
/// a Git repository.
pub fn tempdir_with_git_boundary() -> std::io::Result<TempDir> {
let temp_dir = TempDir::new()?;
std::fs::write(temp_dir.path().join(".git"), "test fixture boundary\n")?;
Ok(temp_dir)
}
pub fn test_tmp_path() -> AbsolutePathBuf {
test_absolute_path_with_windows("/tmp", Some(r"C:\Users\codex\AppData\Local\Temp"))
}