Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Fix 12 more Windows test failures: mkstemp, temp paths, TRE config

Martin Vogel18c23d438fa7
diff --git a/src/foundation/compat.c b/src/foundation/compat.c
index cd826f133..c91549549 100644
--- a/src/foundation/compat.c
+++ b/src/foundation/compat.c
@@ -8,6 +8,11 @@
#include <stdlib.h>
#include <string.h>
+#ifdef _WIN32
+#include <io.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#endif
/* ── strndup (Windows lacks it) ───────────────────────────────── */
@@ -72,6 +77,31 @@ char *cbm_mkdtemp(char *tmpl) {
}
#endif
+/* ── mkstemp (Windows lacks it) ───────────────────────────────── */
+
+#ifdef _WIN32
+int cbm_mkstemp(char *tmpl) {
+ /* Rewrite /tmp/ to %TEMP%\ like cbm_mkdtemp */
+ static char buf[512];
+ if (strncmp(tmpl, "/tmp/", 5) == 0) {
+ const char *tmp = getenv("TEMP");
+ if (!tmp)
+ tmp = getenv("TMP");
+ if (!tmp)
+ tmp = ".";
+ snprintf(buf, sizeof(buf), "%s\\%s", tmp, tmpl + 5);
+ } else {
+ snprintf(buf, sizeof(buf), "%s", tmpl);
+ }
+ if (!_mktemp(buf))
+ return -1;
+ int fd = _open(buf, _O_CREAT | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE);
+ if (fd >= 0)
+ strcpy(tmpl, buf);
+ return fd;
+}
+#endif
+
/* ── clock_gettime (Windows lacks it) ─────────────────────────── */
#ifdef _WIN32
diff --git a/src/foundation/compat.h b/src/foundation/compat.h
index d15d48df1..1b5161466 100644
--- a/src/foundation/compat.h
+++ b/src/foundation/compat.h
@@ -97,6 +97,13 @@ char *cbm_mkdtemp(char *tmpl);
#define cbm_mkdtemp mkdtemp
#endif
+/* ── mkstemp (Windows lacks it) ──────────────────────────────── */
+#ifdef _WIN32
+int cbm_mkstemp(char *tmpl);
+#else
+#define cbm_mkstemp mkstemp
+#endif
+
/* ── setenv / unsetenv (Windows lacks them) ──────────────────── */
#ifdef _WIN32
static inline int cbm_setenv(const char *name, const char *value, int overwrite) {
@@ -120,6 +127,18 @@ static inline int cbm_unsetenv(const char *name) {
#define cbm_pipe(fds) pipe(fds)
#endif
+/* ── Temp directory helper ───────────────────────────────────── */
+static inline const char *cbm_tmpdir(void) {
+#ifdef _WIN32
+ const char *t = getenv("TEMP");
+ if (!t)
+ t = getenv("TMP");
+ return t ? t : ".";
+#else
+ return "/tmp";
+#endif
+}
+
/* ── Signal handling ──────────────────────────────────────────── */
/* Windows doesn't have sigaction; provide macro to select signal API. */
#ifdef _WIN32
diff --git a/tests/test_gitignore.c b/tests/test_gitignore.c
index 54edfa327..391eb189c 100644
--- a/tests/test_gitignore.c
+++ b/tests/test_gitignore.c
@@ -3,6 +3,7 @@
*
* RED phase: These tests define the expected pattern matching behavior.
*/
+#include "../src/foundation/compat.h"
#include "test_framework.h"
#include "discover/discover.h"
@@ -162,7 +163,7 @@ TEST(gi_null_safe_free) {
/* ── Load from file ────────────────────────────────────────────── */
TEST(gi_load_file) {
- const char *path = "/tmp/test_gitignore_file";
+ char path[256]; snprintf(path, sizeof(path), "%s/test_gitignore_file", cbm_tmpdir());
FILE *f = fopen(path, "w");
ASSERT_NOT_NULL(f);
fprintf(f, "*.o\nbuild/\n");
@@ -178,7 +179,9 @@ TEST(gi_load_file) {
}
TEST(gi_load_nonexistent) {
- cbm_gitignore_t *gi = cbm_gitignore_load("/tmp/nonexistent_gitignore_12345");
+ char np[256];
+ snprintf(np, sizeof(np), "%s/nonexistent_gitignore_12345", cbm_tmpdir());
+ cbm_gitignore_t *gi = cbm_gitignore_load(np);
ASSERT_NULL(gi);
PASS();
}
diff --git a/tests/test_language.c b/tests/test_language.c
index 7dce46670..83999502a 100644
--- a/tests/test_language.c
+++ b/tests/test_language.c
@@ -3,6 +3,7 @@
*
* RED phase: These tests define the expected behavior for all 64 languages.
*/
+#include "../src/foundation/compat.h"
#include "test_framework.h"
#include "discover/discover.h"
@@ -542,7 +543,7 @@ TEST(lang_name_unknown) {
/* These tests need temp files with content markers */
TEST(lang_m_objc) {
/* Write a temp file with Objective-C markers */
- const char *path = "/tmp/test_lang_objc.m";
+ char path[256]; snprintf(path, sizeof(path), "%s/test_lang_objc.m", cbm_tmpdir());
FILE *f = fopen(path, "w");
ASSERT_NOT_NULL(f);
fprintf(f, "#import <Foundation/Foundation.h>\n@interface Foo : NSObject\n@end\n");
@@ -554,7 +555,7 @@ TEST(lang_m_objc) {
}
TEST(lang_m_magma) {
- const char *path = "/tmp/test_lang_magma.m";
+ char path[256]; snprintf(path, sizeof(path), "%s/test_lang_magma.m", cbm_tmpdir());
FILE *f = fopen(path, "w");
ASSERT_NOT_NULL(f);
fprintf(f, "function MyFunc(x)\n return x^2;\nend function;\n");
@@ -566,7 +567,7 @@ TEST(lang_m_magma) {
}
TEST(lang_m_matlab) {
- const char *path = "/tmp/test_lang_matlab.m";
+ char path[256]; snprintf(path, sizeof(path), "%s/test_lang_matlab.m", cbm_tmpdir());
FILE *f = fopen(path, "w");
ASSERT_NOT_NULL(f);
fprintf(f, "function y = square(x)\n y = x.^2;\nend\n");
diff --git a/tests/test_sqlite_writer.c b/tests/test_sqlite_writer.c
index 7e926fa26..f45a06689 100644
--- a/tests/test_sqlite_writer.c
+++ b/tests/test_sqlite_writer.c
@@ -7,6 +7,7 @@
* The page writer (cbm_write_db) constructs B-tree pages directly,
* bypassing the SQL parser entirely. These tests verify integrity.
*/
+#include "../src/foundation/compat.h"
#include "test_framework.h"
/* sqlite_writer.h is at internal/cbm/ — Makefile adds -Iinternal/cbm */
#include "sqlite_writer.h" /* CBMDumpNode, CBMDumpEdge, cbm_write_db */
@@ -17,7 +18,7 @@
static int make_temp_db(char *path, size_t pathsz) {
snprintf(path, pathsz, "/tmp/cbm_sw_test_XXXXXX");
- int fd = mkstemp(path);
+ int fd = cbm_mkstemp(path);
if (fd < 0)
return -1;
close(fd);
diff --git a/tests/test_store_search.c b/tests/test_store_search.c
index c78a3e923..f3ebeb6fd 100644
--- a/tests/test_store_search.c
+++ b/tests/test_store_search.c
@@ -3,6 +3,7 @@
*
* Ported from internal/store/store_test.go (TestSearch, TestBFS, etc.)
*/
+#include "../src/foundation/compat.h"
#include "test_framework.h"
#include <store/store.h>
#include <string.h>
@@ -434,8 +435,8 @@ TEST(store_dump_to_file) {
ASSERT_TRUE(id > 0);
/* Dump to temp file */
- char path[] = "/tmp/cbm_test_dump_XXXXXX";
- int fd = mkstemp(path);
+ char path[256]; snprintf(path, sizeof(path), "/tmp/cbm_test_dump_XXXXXX");
+ int fd = cbm_mkstemp(path);
ASSERT_TRUE(fd >= 0);
close(fd);