Files
ww/test/wcc/975_dirs_run.c
Hojun-Cho a95a7a316b test/wcc: carrier ownership repair and driver-contract adaptation
Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/
fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT-
tolerant checked unlinks, exact-path deletion (rm -rf only for an
owned pid-keyed dir or a .sepwork beneath one), and cleanup failure
fails a passing carrier without overwriting its diagnostic. In the
same pass the carriers adapt to the driver contract this branch lands:
--sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch
contract are asserted, and rows whose runtime or reject coverage moved
to test/wcc/data fixtures or test/lang @test owners are trimmed to the
byte/artifact/diagnostic observations only they can make.

Repair and adaptation ride together because most files interleave both
in the same hunks; splitting would manufacture intermediate carrier
states that never existed and cannot run against either driver.
2026-08-07 23:21:04 +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/dirstest.ww
* file header for the contract).
* 3. exec `ww test lib/dirs/dirstest.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/dirstest.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;
}