Files
ww/test/wcc/975_dirs_run.c
Hojun-Cho fa4b9a134b lib: migrate every test file to the canonical *_test.ww name
25 renames (git mv, content untouched). _test.ww is what the package
coordinator's test detection and the sep loader's canonical exclusion
key on; the old *test.ww spellings survived only through the
line-leading-@test compatibility scan. Consumers updated in place:
LIBRARY_TESTS, the libbyteid roster, the 901/974/975/976 carriers that
copy or invoke these files, and the check.c/check.ww + path/ftos
comments that cite them. Closes the open-driver-work migration bullet.
2026-08-08 04:29:31 +09:00

108 lines
3.2 KiB
C

/*
* 975_dirs_run — execute the lib/dirs @test fixture under the C-side
* `ww test` driver and assert exit 0.
*
* Workflow:
* 1. mkdtemp /tmp/wwdirs-XXXXXX — fresh per run, no cross-run
* stale state, no parallel-test conflicts.
* 2. setenv the four cohort env states (see lib/dirs/dirs_test.ww
* file header for the contract).
* 3. exec `ww test lib/dirs/dirs_test.ww`. The fixture reads
* WW_TEST_TMPDIR + the XDG_/HOME envs and asserts.
* 4. Remove the exact auto-created child dirs bottom-up, then the
* mkdtemp-owned root.
*
* Same wrapper shape as 970-974; cleanup runs on success and failure.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return 1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* mkdtemp the per-run scratch root. */
char tmpl[64];
strcpy(tmpl, "/tmp/wwdirs-XXXXXX");
if (mkdtemp(tmpl) == NULL) {
perror("mkdtemp");
return 1;
}
/* Pre-arrange env. The child `ww test` inherits these. */
char cfg[128];
snprintf(cfg, sizeof cfg, "%s/cfg", tmpl);
setenv("HOME", tmpl, 1);
setenv("XDG_CONFIG_HOME", cfg, 1);
setenv("XDG_CACHE_HOME", "relative/path", 1); /* non-abs → fallback */
unsetenv("XDG_DATA_HOME");
unsetenv("XDG_STATE_HOME");
setenv("WW_TEST_TMPDIR", tmpl, 1);
const char *src = "lib/dirs/dirs_test.ww";
char path[1024], cmd[2048];
snprintf(path, sizeof path, "%s/%s", cwd, src);
snprintf(cmd, sizeof cmd, "%s/ww test %s", bin, path);
int rc = runwait(cmd);
int fail = 0;
if (rc != 0) {
fprintf(stderr, "dirs_run FAIL: %s exited %d\n", src, rc);
fail = 1;
}
/* dirs.* recursively creates these exact directory chains. */
char p[256];
int cleanbad = 0;
snprintf(p, sizeof p, "%s/cfg/myapp", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
snprintf(p, sizeof p, "%s/cfg", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
snprintf(p, sizeof p, "%s/.cache/myapp", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
snprintf(p, sizeof p, "%s/.cache", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
snprintf(p, sizeof p, "%s/.local/share/myapp", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
snprintf(p, sizeof p, "%s/.local/share", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
snprintf(p, sizeof p, "%s/.local/state/myapp", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
snprintf(p, sizeof p, "%s/.local/state", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
snprintf(p, sizeof p, "%s/.local", tmpl);
if (rmdir(p) != 0 && errno != ENOENT) cleanbad = 1;
if (rmdir(tmpl) != 0) cleanbad = 1;
if (cleanbad) {
fprintf(stderr, "dirs_run FAIL: temporary cleanup failed\n");
fail = 1;
}
if (fail) return 1;
printf("dirs_run: %s ok\n", src);
return 0;
}