Skip to content

Commit bb9677f

Browse files
authored
Merge pull request DeusData#1067 from tmonestudio/codex/test-isolate-cache
fix(tests): isolate integration caches from user state
2 parents 548927a + feb5cd8 commit bb9677f

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎tests/test_lang_contract.c‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,18 @@ static cbm_store_t *lang_open_indexed(LangProj *lp) {
7171
if (!lp->project) {
7272
return NULL;
7373
}
74-
const char *home = getenv("HOME");
75-
if (!home) {
76-
home = "/tmp";
77-
}
7874
char cache_dir[512];
79-
snprintf(cache_dir, sizeof(cache_dir), "%s/.cache/codebase-memory-mcp", home);
80-
cbm_mkdir(cache_dir);
75+
const char *configured_cache = getenv("CBM_CACHE_DIR");
76+
if (configured_cache && configured_cache[0]) {
77+
snprintf(cache_dir, sizeof(cache_dir), "%s", configured_cache);
78+
} else {
79+
const char *home = getenv("HOME");
80+
if (!home) {
81+
home = "/tmp";
82+
}
83+
snprintf(cache_dir, sizeof(cache_dir), "%s/.cache/codebase-memory-mcp", home);
84+
}
85+
cbm_mkdir_p(cache_dir, 0755);
8186
snprintf(lp->dbpath, sizeof(lp->dbpath), "%s/%s.db", cache_dir, lp->project);
8287
unlink(lp->dbpath);
8388
lp->srv = cbm_mcp_server_new(NULL);

‎tests/test_main.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ int tf_fail_count = 0;
99
int tf_skip_count = 0;
1010

1111
#include "test_framework.h"
12+
#include "test_helpers.h"
1213
#include "foundation/compat.h" /* cbm_setenv — #845 supervisor kill switch */
1314
#include "foundation/compat_fs.h" /* cbm_fopen — worker response file */
1415
#include "foundation/mem.h" /* cbm_mem_init — worker budget */
@@ -24,6 +25,32 @@ int tf_skip_count = 0;
2425
#include <winsock2.h> /* #798 follow-up: socket-isolation re-exec probe */
2526
#endif
2627

28+
/* Test handlers that exercise the production index_repository flow must never
29+
* inherit the user's real cache. Individual tests may temporarily override
30+
* this sentinel and restore it; atexit removes anything left behind by an
31+
* assertion that returns before fixture cleanup. */
32+
static char tf_home_sentinel[512];
33+
34+
static void tf_cleanup_cache_sentinel(void) {
35+
if (tf_home_sentinel[0]) {
36+
th_rmtree(tf_home_sentinel);
37+
}
38+
}
39+
40+
static bool tf_setup_cache_sentinel(void) {
41+
snprintf(tf_home_sentinel, sizeof(tf_home_sentinel), "/tmp/cbm-test-home-XXXXXX");
42+
if (!cbm_mkdtemp(tf_home_sentinel)) {
43+
return false;
44+
}
45+
/* Legacy integration fixtures derive DB paths from HOME, while production
46+
* cache_dir() prefers CBM_CACHE_DIR. A private HOME plus no inherited cache
47+
* override keeps both conventions pointed at the same isolated tree. */
48+
cbm_setenv("HOME", tf_home_sentinel, 1);
49+
cbm_unsetenv("CBM_CACHE_DIR");
50+
atexit(tf_cleanup_cache_sentinel);
51+
return true;
52+
}
53+
2754
/* #832 guard support: when the index supervisor spawns THIS binary as
2855
* `<self> cli --index-worker index_repository <args_json> --response-out <file>`
2956
* (exactly the argv cbm_index_spawn_worker builds), act as a faithful in-process
@@ -276,6 +303,10 @@ int main(int argc, char **argv) {
276303
* binary as `<self> cli --index-worker …` and recursively re-run suites.
277304
* A test that exercises the supervisor must explicitly re-enable it. */
278305
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
306+
if (!tf_setup_cache_sentinel()) {
307+
fprintf(stderr, "failed to create isolated test cache\n");
308+
return 2;
309+
}
279310

280311
g_suite_argc = argc;
281312
g_suite_argv = argv;

0 commit comments

Comments
 (0)