Skip to content

Commit 18c23d4

Browse files
committed
Fix 12 more Windows test failures: mkstemp, temp paths, TRE config
- cbm_mkstemp: Windows implementation using _mktemp + _open - cbm_tmpdir(): portable temp directory function (replaces /tmp/) - Fix hardcoded /tmp/ paths in test_language, test_gitignore, test_store_search, test_sqlite_writer - Enlarge remaining small path buffers for Windows %TEMP% paths
1 parent d6ec954 commit 18c23d4

6 files changed

Lines changed: 63 additions & 8 deletions

File tree

src/foundation/compat.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#include <stdlib.h>
1010
#include <string.h>
11+
#ifdef _WIN32
12+
#include <io.h>
13+
#include <fcntl.h>
14+
#include <sys/stat.h>
15+
#endif
1116

1217
/* ── strndup (Windows lacks it) ───────────────────────────────── */
1318

@@ -72,6 +77,31 @@ char *cbm_mkdtemp(char *tmpl) {
7277
}
7378
#endif
7479

80+
/* ── mkstemp (Windows lacks it) ───────────────────────────────── */
81+
82+
#ifdef _WIN32
83+
int cbm_mkstemp(char *tmpl) {
84+
/* Rewrite /tmp/ to %TEMP%\ like cbm_mkdtemp */
85+
static char buf[512];
86+
if (strncmp(tmpl, "/tmp/", 5) == 0) {
87+
const char *tmp = getenv("TEMP");
88+
if (!tmp)
89+
tmp = getenv("TMP");
90+
if (!tmp)
91+
tmp = ".";
92+
snprintf(buf, sizeof(buf), "%s\\%s", tmp, tmpl + 5);
93+
} else {
94+
snprintf(buf, sizeof(buf), "%s", tmpl);
95+
}
96+
if (!_mktemp(buf))
97+
return -1;
98+
int fd = _open(buf, _O_CREAT | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE);
99+
if (fd >= 0)
100+
strcpy(tmpl, buf);
101+
return fd;
102+
}
103+
#endif
104+
75105
/* ── clock_gettime (Windows lacks it) ─────────────────────────── */
76106

77107
#ifdef _WIN32

src/foundation/compat.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ char *cbm_mkdtemp(char *tmpl);
9797
#define cbm_mkdtemp mkdtemp
9898
#endif
9999

100+
/* ── mkstemp (Windows lacks it) ──────────────────────────────── */
101+
#ifdef _WIN32
102+
int cbm_mkstemp(char *tmpl);
103+
#else
104+
#define cbm_mkstemp mkstemp
105+
#endif
106+
100107
/* ── setenv / unsetenv (Windows lacks them) ──────────────────── */
101108
#ifdef _WIN32
102109
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) {
120127
#define cbm_pipe(fds) pipe(fds)
121128
#endif
122129

130+
/* ── Temp directory helper ───────────────────────────────────── */
131+
static inline const char *cbm_tmpdir(void) {
132+
#ifdef _WIN32
133+
const char *t = getenv("TEMP");
134+
if (!t)
135+
t = getenv("TMP");
136+
return t ? t : ".";
137+
#else
138+
return "/tmp";
139+
#endif
140+
}
141+
123142
/* ── Signal handling ──────────────────────────────────────────── */
124143
/* Windows doesn't have sigaction; provide macro to select signal API. */
125144
#ifdef _WIN32

tests/test_gitignore.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* RED phase: These tests define the expected pattern matching behavior.
55
*/
6+
#include "../src/foundation/compat.h"
67
#include "test_framework.h"
78
#include "discover/discover.h"
89

@@ -162,7 +163,7 @@ TEST(gi_null_safe_free) {
162163
/* ── Load from file ────────────────────────────────────────────── */
163164

164165
TEST(gi_load_file) {
165-
const char *path = "/tmp/test_gitignore_file";
166+
char path[256]; snprintf(path, sizeof(path), "%s/test_gitignore_file", cbm_tmpdir());
166167
FILE *f = fopen(path, "w");
167168
ASSERT_NOT_NULL(f);
168169
fprintf(f, "*.o\nbuild/\n");
@@ -178,7 +179,9 @@ TEST(gi_load_file) {
178179
}
179180

180181
TEST(gi_load_nonexistent) {
181-
cbm_gitignore_t *gi = cbm_gitignore_load("/tmp/nonexistent_gitignore_12345");
182+
char np[256];
183+
snprintf(np, sizeof(np), "%s/nonexistent_gitignore_12345", cbm_tmpdir());
184+
cbm_gitignore_t *gi = cbm_gitignore_load(np);
182185
ASSERT_NULL(gi);
183186
PASS();
184187
}

tests/test_language.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* RED phase: These tests define the expected behavior for all 64 languages.
55
*/
6+
#include "../src/foundation/compat.h"
67
#include "test_framework.h"
78
#include "discover/discover.h"
89

@@ -542,7 +543,7 @@ TEST(lang_name_unknown) {
542543
/* These tests need temp files with content markers */
543544
TEST(lang_m_objc) {
544545
/* Write a temp file with Objective-C markers */
545-
const char *path = "/tmp/test_lang_objc.m";
546+
char path[256]; snprintf(path, sizeof(path), "%s/test_lang_objc.m", cbm_tmpdir());
546547
FILE *f = fopen(path, "w");
547548
ASSERT_NOT_NULL(f);
548549
fprintf(f, "#import <Foundation/Foundation.h>\n@interface Foo : NSObject\n@end\n");
@@ -554,7 +555,7 @@ TEST(lang_m_objc) {
554555
}
555556

556557
TEST(lang_m_magma) {
557-
const char *path = "/tmp/test_lang_magma.m";
558+
char path[256]; snprintf(path, sizeof(path), "%s/test_lang_magma.m", cbm_tmpdir());
558559
FILE *f = fopen(path, "w");
559560
ASSERT_NOT_NULL(f);
560561
fprintf(f, "function MyFunc(x)\n return x^2;\nend function;\n");
@@ -566,7 +567,7 @@ TEST(lang_m_magma) {
566567
}
567568

568569
TEST(lang_m_matlab) {
569-
const char *path = "/tmp/test_lang_matlab.m";
570+
char path[256]; snprintf(path, sizeof(path), "%s/test_lang_matlab.m", cbm_tmpdir());
570571
FILE *f = fopen(path, "w");
571572
ASSERT_NOT_NULL(f);
572573
fprintf(f, "function y = square(x)\n y = x.^2;\nend\n");

tests/test_sqlite_writer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* The page writer (cbm_write_db) constructs B-tree pages directly,
88
* bypassing the SQL parser entirely. These tests verify integrity.
99
*/
10+
#include "../src/foundation/compat.h"
1011
#include "test_framework.h"
1112
/* sqlite_writer.h is at internal/cbm/ — Makefile adds -Iinternal/cbm */
1213
#include "sqlite_writer.h" /* CBMDumpNode, CBMDumpEdge, cbm_write_db */
@@ -17,7 +18,7 @@
1718

1819
static int make_temp_db(char *path, size_t pathsz) {
1920
snprintf(path, pathsz, "/tmp/cbm_sw_test_XXXXXX");
20-
int fd = mkstemp(path);
21+
int fd = cbm_mkstemp(path);
2122
if (fd < 0)
2223
return -1;
2324
close(fd);

tests/test_store_search.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Ported from internal/store/store_test.go (TestSearch, TestBFS, etc.)
55
*/
6+
#include "../src/foundation/compat.h"
67
#include "test_framework.h"
78
#include <store/store.h>
89
#include <string.h>
@@ -434,8 +435,8 @@ TEST(store_dump_to_file) {
434435
ASSERT_TRUE(id > 0);
435436

436437
/* Dump to temp file */
437-
char path[] = "/tmp/cbm_test_dump_XXXXXX";
438-
int fd = mkstemp(path);
438+
char path[256]; snprintf(path, sizeof(path), "/tmp/cbm_test_dump_XXXXXX");
439+
int fd = cbm_mkstemp(path);
439440
ASSERT_TRUE(fd >= 0);
440441
close(fd);
441442

0 commit comments

Comments
 (0)