From db52f39ee1a7fabff373de34f817ad757830ca31 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:14:56 +0100 Subject: [PATCH] raztest -w: runs the tests again whenever a .raz file changes Watched are the *.raz in each test file's directory, each directory given and each -I directory. Sys has no inotify, so their times and sizes are looked at every 300 ms, and a change is waited out until they stay still for one look. Every test runs again, not only those a change reaches: raz builds fast, and tracking imports isn't worth its lines. The operands are read again each time, so a new test is found; on a terminal the screen is cleared first. A signal stops it, 128 and its number. summary no longer ends the program; main does. test/check.sh starts -w in the background, breaks its test, sees it fail, and stops it. Step 5 of TESTING-PLAN.md. make check: 1041 passed, 0 failed. --- diff --git a/TESTING-PLAN.md b/TESTING-PLAN.md index 2f6f23b..4a1ab97 100644 --- a/TESTING-PLAN.md +++ b/TESTING-PLAN.md @@ -55,8 +55,9 @@ timeout each, `-r pattern`; times each; shows a test's output only when it fails; colour on a terminal unless `CI` or `NO_COLOR`; a summary and the status. `--tap`, `--junit file` (a module is a case, its `FAIL` -blocks the failure). `-w` reruns what a changed `.raz` reaches, by -polling file times (Sys has no inotify: a gap). `--seed`, `--fuzz 60s` +blocks the failure). `-w` reruns every test when a `.raz` beside the +tests or in `-I` changes (raz builds fast; import tracking isn't worth +its lines), by polling file times (Sys has no inotify: a gap). `--seed`, `--fuzz 60s` set `PROP_*`. Left out: `.cn`, sh and pty tests, which stay in make. A Makefile uses `raztest` when it's there and its own loop when not. diff --git a/doc/raztest.1 b/doc/raztest.1 index 62b6efa..d92dc8a 100644 --- a/doc/raztest.1 +++ b/doc/raztest.1 @@ -12,6 +12,7 @@ .Op Fl t Ar secs .Op Fl o Ar dir .Op Fl r Ar text +.Op Fl w .Op Fl \-tap .Op Fl \-junit Ar file .Ar file.raz Ns | Ns Ar dir ... @@ -124,6 +125,26 @@ .It Fl r Ar text Run only the tests whose name holds .Ar text . +.It Fl w , Fl \-watch +Run the tests, then run them all again whenever a file changes, until +interrupted. +Watched are the +.Pa *.raz +files directly in the directory of each test file given, in each +directory given, and in each +.Fl I +directory, not those below them. +The files' times and sizes are looked at every 300 ms, and a change is +waited out until the files stay as they are for one look, since an +editor writes in steps. +All the tests run again, not only those a change reaches. +The operands are read again each time, so a new test file is found. +On a terminal the screen is cleared before each run, and +.Ql watching N files; ^C stops +is written after the summary. +With +.Fl \-junit +the file is written again after each run. .It Fl \-tap Write TAP instead of the usual lines. .It Fl \-junit Ar file diff --git a/test/check.sh b/test/check.sh index 89d9b24..92fa484 100755 --- a/test/check.sh +++ b/test/check.sh @@ -311,6 +311,32 @@ fi fi +# raztest -w: a passing test, then edited to fail, is run again by the +# watch, which ^C ends with status 130; each wait is bounded to 10 s +waitfor() { # file, text: whether text shows in file within 10 s + for _ in $(seq 100); do + grep -q "$2" $1 && return 0 + sleep 0.1 + done + return 1 +} +if [ -x $out/raztest ]; then + wd=$out/rtw; rm -rf $wd; mkdir -p $wd/t + cp test/raztest/Pass.raz $wd/t/ + RAZ=$raz $out/raztest -w -o $wd/o $wd/t > $wd/log 2>&1 $wd/t/Pass.raz + waitfor $wd/log '0 passed, 1 failed'; ok=$? + else ok=1; fi + kill -INT $wpid 2>/dev/null; wait $wpid; st=$? + if [ $ok = 0 ] && [ $st = 130 ]; then pass=$((pass + 1)) + else + echo "FAIL raztest -w (status $st):"; cat $wd/log + fail=$((fail + 1)) + fi +fi + # CAbi is tools/cabi.c's output, from the C library's own headers: # compiled with cc, and with musl-gcc, each where it is installed, it # writes lib/'s file again diff --git a/tools/Test.raz b/tools/Test.raz index 6bf3a77..0f36479 100644 --- a/tools/Test.raz +++ b/tools/Test.raz @@ -1,6 +1,6 @@ (* Test: raztest, which builds and runs the tests of a raz program. - raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] + raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [-w] [--tap] [--junit file] file.raz|dir ... A test is a module whose main is its test: it passes if it builds @@ -35,9 +35,20 @@ is, so invalid UTF-8 in it passes through into the XML, except that the control bytes XML 1.0 forbids become ?. - Needs only the library. Leaves out: watching files (to come), - arguments to a test, tests not in raz, and the number of processors - as -j's default, which Sys can't tell: it is 1. *) + -w runs the tests, then runs them all again whenever a *.raz file + changes, until a signal stops it. Watched are the *.raz directly in + the directory of each test file, of each directory given and of each + -I directory. Sys has no inotify, so the files' times and sizes are + looked at every 300 ms; a change is waited out until the files stay + as they are for one look, since an editor writes in steps. All the + tests run again, not those a change reaches: raz builds fast, and + tracking imports isn't worth its lines. The operands are read again + each time, so a new test file is found. On a terminal the screen is + cleared before each run. + + Needs only the library. Leaves out: arguments to a test, tests not + in raz, and the number of processors as -j's default, which Sys + can't tell: it is 1. *) module Test; import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files; import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less); @@ -46,15 +57,19 @@ TapKey = 1; (* the keys of --tap and --junit, which have no letter *) JunitKey = 2; Table: []Opt.Row = [ - Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [--tap] [--junit file] file.raz|dir ..."), + Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [-w] [--tap] [--junit file] file.raz|dir ..."), Opt.option("", 'I', "include", "dir", "look for modules in dir too, as raz -I"), Opt.option("", 'j', "jobs", "n", "run n tests at once (1)"), Opt.option("", 't', "timeout", "secs", "kill a build or run after secs (60)"), Opt.option("", 'o', "out", "dir", "put the programs and their .out in dir (build/raztest)"), Opt.option("", 'r', "run", "text", "run only the tests whose name holds text"), + Opt.option("", 'w', "watch", "", "run again whenever a .raz file changes"), Opt.option("", TapKey, "tap", "", "write TAP version 14 instead of the usual lines"), Opt.option("", JunitKey, "junit", "file", "also write the results to file as JUnit XML")]; PollMs = 10; (* how long to wait between looks at the running tests *) + WatchMs = 300; (* how long to wait between looks at the watched files *) + Clear = "\x1b[H\x1b[2J"; (* home the cursor and clear the screen *) + Modulus = 1000000007; (* keeps a fingerprint's sums small *) Second = 1000000000; (* a second in Sys.clock's nanoseconds *) Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *) Red = "\x1b[31m"; @@ -86,6 +101,7 @@ var raz: []U8; (* the compiler: $RAZ, or raz on PATH *) includes: Names.Vec; (* the -I directories *) + operands: Names.Vec; (* the files and directories given *) jobs: INT; (* -j: tests at once *) timeout: I64; (* -t, in nanoseconds *) outDir: []U8; (* -o *) @@ -97,6 +113,8 @@ colour: BOOL; (* the words of the results are coloured *) tap: BOOL; (* --tap: report in TAP *) junit: []U8; (* --junit: the file for the XML, or "" *) + watch: BOOL; (* -w: run again when a file changes *) + clearing: BOOL; (* the screen is cleared before each run of -w *) (* Whether a sorts before b, for sorting a directory's files. *) proc less(a, b: []U8): BOOL; @@ -195,7 +213,7 @@ begin jobs := 1; timeout := 60 * Second; outDir := "build/raztest"; Names.init(includes, Heap.heap); - Jobs.init(tests, Heap.heap); + Names.init(operands, Heap.heap); Opt.init(p, "raztest", Table); while Opt.next(p) do if p.letter = 'I' then Names.push(includes, p.value); @@ -203,6 +221,7 @@ elsif p.letter = 't' then timeout := Opt.number(p, 1, 1000000) * Second; elsif p.letter = 'o' then outDir := p.value; elsif p.letter = 'r' then only := p.value; + elsif p.letter = 'w' then watch := true; elsif p.letter = TapKey then tap := true; elsif p.letter = JunitKey then junit := p.value; end; @@ -210,9 +229,20 @@ arg, ok := Opt.operand(p); if not ok then Opt.fail(p, "no tests named; raztest --help says how"); end; repeat - addOperand(arg); + Names.push(operands, arg); arg, ok := Opt.operand(p); until not ok; +end; + +(* Makes the list of tests, from the operands as they are now. *) +proc loadTests; +var i: INT; +begin + for i := 0 to tests.len - 1 do Buf.done(Jobs.all(tests)[i].why); end; + Jobs.done(tests); + Jobs.init(tests, Heap.heap); + busy := 0; + for i := 0 to operands.len - 1 do addOperand(Names.all(operands)[i]); end; checkNames; end; @@ -245,6 +275,7 @@ signals := Sys.catch([Sys.sigInt, Sys.sigTerm, Sys.sigHup]); if signals < 0 then die("can't catch signals: {}", [Fmt.str(Sys.errorText(signals))]); end; colour := Sys.isTerminal(Sys.stdout) and (LEN(Sys.env("NO_COLOR")) = 0) and not tap; + clearing := Sys.isTerminal(Sys.stdout) and not tap; end; (* Whether the test j is being built or run. *) @@ -516,8 +547,8 @@ if not Files.write(junit, Buf.bytes(b)) then die("can't write {}", [Fmt.str(junit)]); end; end; -(* The summary line, the --junit file, and status 1 if any test failed. *) -proc summary(took: I64); +(* The summary line and the --junit file; whether all the tests passed. *) +proc summary(took: I64): BOOL; var all: []Job; i: INT; count: [3]INT; begin all := Jobs.all(tests); @@ -528,17 +559,130 @@ Fmt.real(F64(took) / F64(Second), 2)]); Out.flush; if LEN(junit) > 0 then writeJunit(took); end; - if count[INT(passed)] < LEN(all) then Rt.exit(1); end; + return count[INT(passed)] = LEN(all); end; -proc main*; +(* Loads, runs and reports the tests once; whether all passed. *) +proc cycle(): BOOL; var began: I64; begin began := Sys.clock(); - readArgs; - setUp; + loadTests; if tap then tapPlan; end; runAll; - summary(Sys.clock() - began); + return summary(Sys.clock() - began); +end; + +(* The directory of the file path. *) +proc dirOf(path: []U8): []U8; +var i: INT; +begin + i := Str.findLast(path, "/"); + if i < 0 then return "."; end; + if i = 0 then return "/"; end; + return path[0..i]; +end; + +(* Adds dir to the directories to watch, unless it is there. *) +proc watchDir(var dirs: Names.Vec; dir: []U8); +var i: INT; +begin + for i := 0 to dirs.len - 1 do + if Names.all(dirs)[i] = dir then return; end; + end; + Names.push(dirs, dir); +end; + +(* The directories to watch: those given, those of the test files given, + and the -I ones. *) +proc watchDirs(var dirs: Names.Vec); +var i, err: INT; f: Sys.Info; path: []U8; +begin + Names.init(dirs, Heap.heap); + for i := 0 to operands.len - 1 do + path := Names.all(operands)[i]; + f, err := Sys.info(path); + if (err = 0) and f.dir then watchDir(dirs, path); else watchDir(dirs, dirOf(path)); end; + end; + for i := 0 to includes.len - 1 do watchDir(dirs, Names.all(includes)[i]); end; +end; + +(* Adds to the fingerprint and the count the *.raz files of dir. It + runs every few hundred milliseconds for as long as -w does, so the + paths are made in one buffer, used again. *) +proc lookIn(dir: []U8; var print: I64; var count: INT); +var d: Sys.Dir; name: []U8; ok: BOOL; f: Sys.Info; err: INT; path: Buf.Buf; +begin + if Sys.openDir(d, dir) < 0 then return; end; + Buf.init(path, Heap.heap); + defer Buf.done(path); + name, ok := Sys.nextEntry(d); + while ok do + Buf.reset(path); + Buf.str(path, dir); Buf.str(path, "/"); Buf.str(path, name); + f, err := Sys.info(Buf.bytes(path)); + if Str.hasSuffix(name, ".raz") and (err = 0) then + INC(count); + print := (print + 1 + f.mtime mod Modulus + f.size mod Modulus) mod Modulus; + end; + name, ok := Sys.nextEntry(d); + end; + Sys.closeDir(d); +end; + +(* A number that changes when a *.raz file in the directories is edited, + added or removed, and how many files there are. *) +proc fingerprint(): (I64, INT); +var dirs: Names.Vec; i, count: INT; print: I64; +begin + watchDirs(dirs); + defer Names.done(dirs); + for i := 0 to dirs.len - 1 do lookIn(Names.all(dirs)[i], print, count); end; + return print, count; +end; + +(* Waits one look's time; a signal that stops raztest ends it at once. *) +proc nap; +begin + if Sys.ready(signals, WatchMs) then interrupted(Sys.caught(signals)); end; +end; + +(* Waits until the fingerprint is not seen, and then does not change + for one look. *) +proc waitChange(seen: I64); +var print: I64; +begin + repeat + nap; + print, _ := fingerprint(); + until print # seen; + repeat + seen := print; + nap; + print, _ := fingerprint(); + until print = seen; +end; + +(* Runs the tests, and again at each change, for ever. *) +proc watchAll; +var seen: I64; n: INT; +begin + while true do + if clearing then Out.str(Clear); end; + seen, n := fingerprint(); + _ := cycle(); + if tap then Out.str("# "); end; + Fmt.print("watching {} files; ^C stops\n", [Fmt.int(n)]); + Out.flush; + waitChange(seen); + end; +end; + +proc main*; +begin + readArgs; + setUp; + if watch then watchAll; end; + if not cycle() then Rt.exit(1); end; end; end Test.