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
22 changes: 21 additions & 1 deletion tests/test_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ static void restore_test_env(const char *name, char *saved) {
}
}

TEST(cli_runner_uses_private_codex_home) {
const char *home = getenv("HOME");
const char *codex_home = getenv("CODEX_HOME");
ASSERT_NOT_NULL(home);
ASSERT_NOT_NULL(codex_home);

char expected[1024];
int expected_len = snprintf(expected, sizeof(expected), "%s/.codex", home);
ASSERT(expected_len >= 0 && (size_t)expected_len < sizeof(expected));
ASSERT_STR_EQ(codex_home, expected);
PASS();
}

/* Helper: mkdirp */
static int test_mkdirp(const char *path) {
char tmp[1024];
Expand Down Expand Up @@ -2354,10 +2367,13 @@ TEST(cli_detect_agents_finds_codex) {
snprintf(dir, sizeof(dir), "%s/.codex", tmpdir);
test_mkdirp(dir);

char *saved_codex = save_test_env("CODEX_HOME");
cbm_unsetenv("CODEX_HOME");
cbm_detected_agents_t agents = cbm_detect_agents(tmpdir);
ASSERT_TRUE(agents.codex);
restore_test_env("CODEX_HOME", saved_codex);

test_rmdir_r(tmpdir);
ASSERT_TRUE(agents.codex);
PASS();
}

Expand Down Expand Up @@ -2395,7 +2411,10 @@ TEST(cli_install_plan_receipt_no_mutation_issue388) {
snprintf(dir, sizeof(dir), "%s/.codex", tmpdir);
test_mkdirp(dir);

char *saved_codex = save_test_env("CODEX_HOME");
cbm_unsetenv("CODEX_HOME");
char *json = cbm_build_install_plan_json(tmpdir, "/usr/local/bin/codebase-memory-mcp");
restore_test_env("CODEX_HOME", saved_codex);
ASSERT_NOT_NULL(json);
ASSERT(strstr(json, "agent.install.plan.v1") != NULL);
ASSERT(strstr(json, "writes_started") != NULL);
Expand Down Expand Up @@ -9643,6 +9662,7 @@ TEST(cli_sha256_file_matches_known_vector) {
* ═══════════════════════════════════════════════════════════════════ */

SUITE(cli) {
RUN_TEST(cli_runner_uses_private_codex_home);
RUN_TEST(cli_sha256_file_matches_known_vector);
/* Version (2 tests — selfupdate_test.go) */
RUN_TEST(cli_compare_versions);
Expand Down
20 changes: 13 additions & 7 deletions tests/test_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ int tf_skip_count = 0;
#include <winsock2.h> /* #798 follow-up: socket-isolation re-exec probe */
#endif

/* Test handlers that exercise the production index_repository flow must never
* inherit the user's real cache. Individual tests may temporarily override
* this sentinel and restore it; atexit removes anything left behind by an
* assertion that returns before fixture cleanup. */
/* Test handlers that exercise production indexing or agent installation must
* never inherit the user's real cache or Codex configuration. Individual tests
* may temporarily override this sentinel and restore it; atexit removes
* anything left behind by an assertion that returns before fixture cleanup. */
static char tf_home_sentinel[512];

static void tf_cleanup_cache_sentinel(void) {
Expand All @@ -45,8 +45,14 @@ static bool tf_setup_cache_sentinel(void) {
/* Legacy integration fixtures derive DB paths from HOME, while production
* cache_dir() prefers CBM_CACHE_DIR. A private HOME plus no inherited cache
* override keeps both conventions pointed at the same isolated tree. */
cbm_setenv("HOME", tf_home_sentinel, 1);
cbm_unsetenv("CBM_CACHE_DIR");
char codex_home[sizeof(tf_home_sentinel) + sizeof("/.codex")];
int codex_home_len = snprintf(codex_home, sizeof(codex_home), "%s/.codex", tf_home_sentinel);
if (codex_home_len < 0 || (size_t)codex_home_len >= sizeof(codex_home) ||
cbm_setenv("HOME", tf_home_sentinel, 1) != 0 ||
cbm_setenv("CODEX_HOME", codex_home, 1) != 0 || cbm_unsetenv("CBM_CACHE_DIR") != 0) {
tf_cleanup_cache_sentinel();
return false;
}
atexit(tf_cleanup_cache_sentinel);
return true;
}
Expand Down Expand Up @@ -304,7 +310,7 @@ int main(int argc, char **argv) {
* A test that exercises the supervisor must explicitly re-enable it. */
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
if (!tf_setup_cache_sentinel()) {
fprintf(stderr, "failed to create isolated test cache\n");
fprintf(stderr, "failed to create isolated test home\n");
return 2;
}

Expand Down
Loading