ww test: @test name-filter via fnmatch (both stages)

`ww test <file> <pattern>` runs only the @test fns whose names match the
fnmatch glob; no pattern runs all (byte-for-byte the pre-filter path);
zero matches prints "No tests run" and exits 0 (Hare ground truth
ref/hare/test/+test.ha:114-117). A pattern in directory mode is rejected
"ww test: pattern needs a single test file" (rc 2), identical wording in
both twins (cmd/ww/main.c do_test + selfhost/cmd/ww/main.ww dotest).

Mechanism (a): rt/start.s stashes argc/argv into rt_argc/rt_argv getters
(rt_envp twin shape, -T synth untouched so 990-997 byte-id holds);
lib/os.args() rebuilds the []str view, build-once-cached; lib/test/run.ww
imports fnmatch and filters av[1..] (argv[0] is the binary path). The
driver forwards the 2nd positional as argv[1] via fork/execv (cstage) /
procrun (wwstage) so glob metachars aren't shell-expanded.

os.args() is the first `alloc`-caller in the base os module, so os.ww now
imports rt — the `alloc` builtin's malloc lowers to rt_malloc only when
the rt binding is bundled (mirror lib/strings/strings.ww:30); without it a
plain `ww build` of any os-importing program links bare libc `malloc`
(undefined). os is bundled by ~every program, so this is load-bearing.

The lib/test floor rises os-only -> os+fnmatch+ascii+strings in every -T
build; the bundled `ascii` module vs a `@test fn ascii` collision that
exposed is closed by the preceding #30 promote commit. 989_test_filter
pins the full matrix on both twins byte-identically; 949 gains the
dir-mode reject row. (#17)
This commit is contained in:
2026-06-11 04:51:17 +09:00
parent 64f0ddf01b
commit b9c4562135
16 changed files with 853 additions and 165 deletions

View File

@@ -9,7 +9,9 @@
* rejected with "ww test: unknown flag" (rc 2); a lone -I/-o
* is "ww test: -X needs an argument" (rc 2); -c/-o without a
* single test file is "ww test: -c/-o need a single test file"
* (rc 2, #17).
* (rc 2, #17); a 2nd positional (fnmatch name-filter pattern)
* in directory mode is "ww test: pattern needs a single test
* file" (rc 2, #17).
* Each row asserts the expected rc + stderr substring AND that the two
* drivers are byte-identical (rule 10): the cstage parse_build_flags /
* do_test must match the wwstage main.ww dobuild/dorun/dotest verbatim.
@@ -46,6 +48,10 @@ static const struct row rows[] = {
{ "test -o", 2, "ww test: -o needs an argument" },
{ "test -o x", 2, "ww test: -c/-o need a single test file" },
{ "test -c", 2, "ww test: -c/-o need a single test file" },
/* #17: a 2nd positional is a fnmatch name-filter pattern, valid only
* for a single test file/module; in directory mode (target ".") it
* has no single binary to route to and is rejected (rc 2). */
{ "test . zzz", 2, "ww test: pattern needs a single test file" },
};
/* Run "<bin>/<drv> <args>" capturing rc + stderr text into err (NUL-

142
test/wcc/989_test_filter.c Normal file
View File

@@ -0,0 +1,142 @@
/*
* 989_test_filter — the `ww test <file> [pattern]` fnmatch NAME-FILTER
* (task #17 commit-3, drew spec §d). Drives the full driver→binary→
* lib/test path on BOTH driver twins (cstage `ww`, wwstage `ww_ww`):
*
* - no pattern → every @test runs (byte-for-byte the pre-filter
* behavior: "N passed, M failed").
* - "beta" → only the matching @test runs; the others are
* absent from the per-test output.
* - "ga*" → glob selects gamma only.
* - "zzz" → zero matches → "No tests run", rc 0 (Hare ground
* truth, ref/hare/test/+test.ha:114-117).
*
* Each row asserts rc + required/forbidden stdout substrings AND that the
* two driver twins emit byte-identical stdout (rule 10). The fixture is
* test/wcc/data/filter_fixture.ww (three trivially-passing @test fns
* alpha/beta/gamma). The filter lives in lib/test run() reading os.args;
* the -T synth + value table are unchanged, so 990-997 byte-id holds.
*
* Driver-arg ERROR forms (lone/dir-mode pattern) live in
* 949_driver_flagargs. Light driver test, intermediates go to the
* driver's own /tmp temp (rule 14), any NNN.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
struct row {
const char *pat; /* pattern arg, or NULL for none */
int rc; /* expected exit code */
const char *must[4]; /* substrings required in stdout */
const char *mustnot[4]; /* substrings forbidden in stdout */
};
static const struct row rows[] = {
/* no pattern → all three, full count */
{ NULL, 0, { "alpha ... ok", "beta ... ok", "gamma ... ok",
"3 passed, 0 failed" }, { NULL } },
/* exact name → only beta */
{ "beta", 0, { "beta ... ok", "1 passed, 0 failed", NULL },
{ "alpha", "gamma", NULL } },
/* glob → only gamma */
{ "ga*", 0, { "gamma ... ok", "1 passed, 0 failed", NULL },
{ "alpha", "beta", NULL } },
/* no match → "No tests run", success */
{ "zzz", 0, { "No tests run", NULL },
{ "alpha", "beta", "gamma", "passed", NULL } },
/* star matches everything */
{ "*", 0, { "alpha ... ok", "beta ... ok", "gamma ... ok",
"3 passed, 0 failed" }, { NULL } },
};
/* Run "<bin>/<drv> test <fixture> [pat]" capturing rc + stdout into out
* (NUL-terminated). Returns the child's exit code (or -1 on spawn fail). */
static int
run_drv(const char *bin, const char *drv, const char *pat,
char *out, size_t outsz)
{
int pid = getpid();
char outf[64], cmd[2048];
snprintf(outf, sizeof outf, "/tmp/tf989_%d.out", pid);
if (pat) {
snprintf(cmd, sizeof cmd,
"%s/%s test test/wcc/data/filter_fixture.ww '%s' >%s 2>/dev/null",
bin, drv, pat, outf);
} else {
snprintf(cmd, sizeof cmd,
"%s/%s test test/wcc/data/filter_fixture.ww >%s 2>/dev/null",
bin, drv, outf);
}
int rc = system(cmd);
if (rc == -1) { unlink(outf); return -1; }
rc = WIFEXITED(rc) ? WEXITSTATUS(rc) : 1;
out[0] = '\0';
FILE *f = fopen(outf, "r");
if (f) {
size_t got = fread(out, 1, outsz - 1, f);
out[got] = '\0';
fclose(f);
}
unlink(outf);
return rc;
}
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;
}
int fail = 0;
size_t n = sizeof rows / sizeof rows[0];
for (size_t i = 0; i < n; i++) {
const char *pat = rows[i].pat ? rows[i].pat : "(none)";
char cout[8192], wout[8192];
int crc = run_drv(bin, "ww", rows[i].pat, cout, sizeof cout);
int wrc = run_drv(bin, "ww_ww", rows[i].pat, wout, sizeof wout);
if (crc != rows[i].rc) {
fprintf(stderr, "989 FAIL: ww pat=%s rc=%d want=%d\n",
pat, crc, rows[i].rc);
fail = 1;
}
for (int k = 0; k < 4 && rows[i].must[k]; k++)
if (!strstr(cout, rows[i].must[k])) {
fprintf(stderr, "989 FAIL: ww pat=%s stdout missing "
"'%s' (got '%s')\n", pat, rows[i].must[k], cout);
fail = 1;
}
for (int k = 0; k < 4 && rows[i].mustnot[k]; k++)
if (strstr(cout, rows[i].mustnot[k])) {
fprintf(stderr, "989 FAIL: ww pat=%s stdout has "
"forbidden '%s' (got '%s')\n",
pat, rows[i].mustnot[k], cout);
fail = 1;
}
/* twin parity: ww_ww must match ww exactly (rc + stdout). */
if (wrc != crc) {
fprintf(stderr, "989 FAIL: pat=%s ww_ww rc=%d != ww rc=%d\n",
pat, wrc, crc);
fail = 1;
}
if (strcmp(wout, cout) != 0) {
fprintf(stderr, "989 FAIL: pat=%s ww_ww stdout '%s' != ww '%s'\n",
pat, wout, cout);
fail = 1;
}
}
if (fail) return 1;
printf("test name-filter: %zu rows, ww==ww_ww pinned\n", n);
return 0;
}

View File

@@ -0,0 +1,13 @@
// @test fixture for the fnmatch name-filter (task #17 commit-3). Three
// trivially-passing @test fns with distinct names so a glob selects a
// known subset; 989_test_filter drives `ww test <this> [pattern]` and
// asserts which "<name> ... ok" lines appear. Bodies are empty (clean
// return = pass); the test observes SELECTION, not assertion outcomes.
package filterfix;
@test fn alpha() void = { };
@test fn beta() void = { };
@test fn gamma() void = { };