raz / commit db52f39e

commit
db52f39ee1a7fabff373de34f817ad757830ca31
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
1172b23d
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.

patch · log

4 files changed, +208 -16

TESTING-PLAN.md modified +3 -2

@@ -55,8 +55,9 @@
5555 timeout each, `-r pattern`; times each; shows a test's output only when
5656 it fails; colour on a terminal unless `CI` or `NO_COLOR`; a summary and
5757 the status. `--tap`, `--junit file` (a module is a case, its `FAIL`
58-blocks the failure). `-w` reruns what a changed `.raz` reaches, by
59-polling file times (Sys has no inotify: a gap). `--seed`, `--fuzz 60s`
58+blocks the failure). `-w` reruns every test when a `.raz` beside the
59+tests or in `-I` changes (raz builds fast; import tracking isn't worth
60+its lines), by polling file times (Sys has no inotify: a gap). `--seed`, `--fuzz 60s`
6061 set `PROP_*`. Left out: `.cn`, sh and pty tests, which stay in make. A
6162 Makefile uses `raztest` when it's there and its own loop when not.
6263

doc/raztest.1 (history) modified +21 -0

@@ -12,6 +12,7 @@
1212 .Op Fl t Ar secs
1313 .Op Fl o Ar dir
1414 .Op Fl r Ar text
15+.Op Fl w
1516 .Op Fl \-tap
1617 .Op Fl \-junit Ar file
1718 .Ar file.raz Ns | Ns Ar dir ...
@@ -124,6 +125,26 @@
124125 .It Fl r Ar text
125126 Run only the tests whose name holds
126127 .Ar text .
128+.It Fl w , Fl \-watch
129+Run the tests, then run them all again whenever a file changes, until
130+interrupted.
131+Watched are the
132+.Pa *.raz
133+files directly in the directory of each test file given, in each
134+directory given, and in each
135+.Fl I
136+directory, not those below them.
137+The files' times and sizes are looked at every 300 ms, and a change is
138+waited out until the files stay as they are for one look, since an
139+editor writes in steps.
140+All the tests run again, not only those a change reaches.
141+The operands are read again each time, so a new test file is found.
142+On a terminal the screen is cleared before each run, and
143+.Ql watching N files; ^C stops
144+is written after the summary.
145+With
146+.Fl \-junit
147+the file is written again after each run.
127148 .It Fl \-tap
128149 Write TAP instead of the usual lines.
129150 .It Fl \-junit Ar file

test/check.sh (history) modified +26 -0

@@ -311,6 +311,32 @@
311311 fi
312312 fi
313313
314+# raztest -w: a passing test, then edited to fail, is run again by the
315+# watch, which ^C ends with status 130; each wait is bounded to 10 s
316+waitfor() { # file, text: whether text shows in file within 10 s
317+ for _ in $(seq 100); do
318+ grep -q "$2" $1 && return 0
319+ sleep 0.1
320+ done
321+ return 1
322+}
323+if [ -x $out/raztest ]; then
324+ wd=$out/rtw; rm -rf $wd; mkdir -p $wd/t
325+ cp test/raztest/Pass.raz $wd/t/
326+ RAZ=$raz $out/raztest -w -o $wd/o $wd/t > $wd/log 2>&1 </dev/null &
327+ wpid=$!
328+ if waitfor $wd/log '1 passed'; then
329+ printf 'module Pass;\nimport Rt;\nproc main*;\nbegin\n Rt.exit(1);\nend;\nend Pass.\n' > $wd/t/Pass.raz
330+ waitfor $wd/log '0 passed, 1 failed'; ok=$?
331+ else ok=1; fi
332+ kill -INT $wpid 2>/dev/null; wait $wpid; st=$?
333+ if [ $ok = 0 ] && [ $st = 130 ]; then pass=$((pass + 1))
334+ else
335+ echo "FAIL raztest -w (status $st):"; cat $wd/log
336+ fail=$((fail + 1))
337+ fi
338+fi
339+
314340 # CAbi is tools/cabi.c's output, from the C library's own headers:
315341 # compiled with cc, and with musl-gcc, each where it is installed, it
316342 # writes lib/'s file again

tools/Test.raz (history) modified +158 -14

@@ -1,6 +1,6 @@
11 (* Test: raztest, which builds and runs the tests of a raz program.
22
3- raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text]
3+ raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [-w]
44 [--tap] [--junit file] file.raz|dir ...
55
66 A test is a module whose main is its test: it passes if it builds
@@ -35,9 +35,20 @@
3535 is, so invalid UTF-8 in it passes through into the XML, except that
3636 the control bytes XML 1.0 forbids become ?.
3737
38- Needs only the library. Leaves out: watching files (to come),
39- arguments to a test, tests not in raz, and the number of processors
40- as -j's default, which Sys can't tell: it is 1. *)
38+ -w runs the tests, then runs them all again whenever a *.raz file
39+ changes, until a signal stops it. Watched are the *.raz directly in
40+ the directory of each test file, of each directory given and of each
41+ -I directory. Sys has no inotify, so the files' times and sizes are
42+ looked at every 300 ms; a change is waited out until the files stay
43+ as they are for one look, since an editor writes in steps. All the
44+ tests run again, not those a change reaches: raz builds fast, and
45+ tracking imports isn't worth its lines. The operands are read again
46+ each time, so a new test file is found. On a terminal the screen is
47+ cleared before each run.
48+
49+ Needs only the library. Leaves out: arguments to a test, tests not
50+ in raz, and the number of processors as -j's default, which Sys
51+ can't tell: it is 1. *)
4152 module Test;
4253 import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files;
4354 import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less);
@@ -46,15 +57,19 @@
4657 TapKey = 1; (* the keys of --tap and --junit, which have no letter *)
4758 JunitKey = 2;
4859 Table: []Opt.Row = [
49- Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [--tap] [--junit file] file.raz|dir ..."),
60+ Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [-w] [--tap] [--junit file] file.raz|dir ..."),
5061 Opt.option("", 'I', "include", "dir", "look for modules in dir too, as raz -I"),
5162 Opt.option("", 'j', "jobs", "n", "run n tests at once (1)"),
5263 Opt.option("", 't', "timeout", "secs", "kill a build or run after secs (60)"),
5364 Opt.option("", 'o', "out", "dir", "put the programs and their .out in dir (build/raztest)"),
5465 Opt.option("", 'r', "run", "text", "run only the tests whose name holds text"),
66+ Opt.option("", 'w', "watch", "", "run again whenever a .raz file changes"),
5567 Opt.option("", TapKey, "tap", "", "write TAP version 14 instead of the usual lines"),
5668 Opt.option("", JunitKey, "junit", "file", "also write the results to file as JUnit XML")];
5769 PollMs = 10; (* how long to wait between looks at the running tests *)
70+ WatchMs = 300; (* how long to wait between looks at the watched files *)
71+ Clear = "\x1b[H\x1b[2J"; (* home the cursor and clear the screen *)
72+ Modulus = 1000000007; (* keeps a fingerprint's sums small *)
5873 Second = 1000000000; (* a second in Sys.clock's nanoseconds *)
5974 Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *)
6075 Red = "\x1b[31m";
@@ -86,6 +101,7 @@
86101 var
87102 raz: []U8; (* the compiler: $RAZ, or raz on PATH *)
88103 includes: Names.Vec; (* the -I directories *)
104+ operands: Names.Vec; (* the files and directories given *)
89105 jobs: INT; (* -j: tests at once *)
90106 timeout: I64; (* -t, in nanoseconds *)
91107 outDir: []U8; (* -o *)
@@ -97,6 +113,8 @@
97113 colour: BOOL; (* the words of the results are coloured *)
98114 tap: BOOL; (* --tap: report in TAP *)
99115 junit: []U8; (* --junit: the file for the XML, or "" *)
116+ watch: BOOL; (* -w: run again when a file changes *)
117+ clearing: BOOL; (* the screen is cleared before each run of -w *)
100118
101119 (* Whether a sorts before b, for sorting a directory's files. *)
102120 proc less(a, b: []U8): BOOL;
@@ -195,7 +213,7 @@
195213 begin
196214 jobs := 1; timeout := 60 * Second; outDir := "build/raztest";
197215 Names.init(includes, Heap.heap);
198- Jobs.init(tests, Heap.heap);
216+ Names.init(operands, Heap.heap);
199217 Opt.init(p, "raztest", Table);
200218 while Opt.next(p) do
201219 if p.letter = 'I' then Names.push(includes, p.value);
@@ -203,6 +221,7 @@
203221 elsif p.letter = 't' then timeout := Opt.number(p, 1, 1000000) * Second;
204222 elsif p.letter = 'o' then outDir := p.value;
205223 elsif p.letter = 'r' then only := p.value;
224+ elsif p.letter = 'w' then watch := true;
206225 elsif p.letter = TapKey then tap := true;
207226 elsif p.letter = JunitKey then junit := p.value;
208227 end;
@@ -210,9 +229,20 @@
210229 arg, ok := Opt.operand(p);
211230 if not ok then Opt.fail(p, "no tests named; raztest --help says how"); end;
212231 repeat
213- addOperand(arg);
232+ Names.push(operands, arg);
214233 arg, ok := Opt.operand(p);
215234 until not ok;
235+end;
236+
237+(* Makes the list of tests, from the operands as they are now. *)
238+proc loadTests;
239+var i: INT;
240+begin
241+ for i := 0 to tests.len - 1 do Buf.done(Jobs.all(tests)[i].why); end;
242+ Jobs.done(tests);
243+ Jobs.init(tests, Heap.heap);
244+ busy := 0;
245+ for i := 0 to operands.len - 1 do addOperand(Names.all(operands)[i]); end;
216246 checkNames;
217247 end;
218248
@@ -245,6 +275,7 @@
245275 signals := Sys.catch([Sys.sigInt, Sys.sigTerm, Sys.sigHup]);
246276 if signals < 0 then die("can't catch signals: {}", [Fmt.str(Sys.errorText(signals))]); end;
247277 colour := Sys.isTerminal(Sys.stdout) and (LEN(Sys.env("NO_COLOR")) = 0) and not tap;
278+ clearing := Sys.isTerminal(Sys.stdout) and not tap;
248279 end;
249280
250281 (* Whether the test j is being built or run. *)
@@ -516,8 +547,8 @@
516547 if not Files.write(junit, Buf.bytes(b)) then die("can't write {}", [Fmt.str(junit)]); end;
517548 end;
518549
519-(* The summary line, the --junit file, and status 1 if any test failed. *)
520-proc summary(took: I64);
550+(* The summary line and the --junit file; whether all the tests passed. *)
551+proc summary(took: I64): BOOL;
521552 var all: []Job; i: INT; count: [3]INT;
522553 begin
523554 all := Jobs.all(tests);
@@ -528,17 +559,130 @@
528559 Fmt.real(F64(took) / F64(Second), 2)]);
529560 Out.flush;
530561 if LEN(junit) > 0 then writeJunit(took); end;
531- if count[INT(passed)] < LEN(all) then Rt.exit(1); end;
562+ return count[INT(passed)] = LEN(all);
532563 end;
533564
534-proc main*;
565+(* Loads, runs and reports the tests once; whether all passed. *)
566+proc cycle(): BOOL;
535567 var began: I64;
536568 begin
537569 began := Sys.clock();
538- readArgs;
539- setUp;
570+ loadTests;
540571 if tap then tapPlan; end;
541572 runAll;
542- summary(Sys.clock() - began);
573+ return summary(Sys.clock() - began);
574+end;
575+
576+(* The directory of the file path. *)
577+proc dirOf(path: []U8): []U8;
578+var i: INT;
579+begin
580+ i := Str.findLast(path, "/");
581+ if i < 0 then return "."; end;
582+ if i = 0 then return "/"; end;
583+ return path[0..i];
584+end;
585+
586+(* Adds dir to the directories to watch, unless it is there. *)
587+proc watchDir(var dirs: Names.Vec; dir: []U8);
588+var i: INT;
589+begin
590+ for i := 0 to dirs.len - 1 do
591+ if Names.all(dirs)[i] = dir then return; end;
592+ end;
593+ Names.push(dirs, dir);
594+end;
595+
596+(* The directories to watch: those given, those of the test files given,
597+ and the -I ones. *)
598+proc watchDirs(var dirs: Names.Vec);
599+var i, err: INT; f: Sys.Info; path: []U8;
600+begin
601+ Names.init(dirs, Heap.heap);
602+ for i := 0 to operands.len - 1 do
603+ path := Names.all(operands)[i];
604+ f, err := Sys.info(path);
605+ if (err = 0) and f.dir then watchDir(dirs, path); else watchDir(dirs, dirOf(path)); end;
606+ end;
607+ for i := 0 to includes.len - 1 do watchDir(dirs, Names.all(includes)[i]); end;
608+end;
609+
610+(* Adds to the fingerprint and the count the *.raz files of dir. It
611+ runs every few hundred milliseconds for as long as -w does, so the
612+ paths are made in one buffer, used again. *)
613+proc lookIn(dir: []U8; var print: I64; var count: INT);
614+var d: Sys.Dir; name: []U8; ok: BOOL; f: Sys.Info; err: INT; path: Buf.Buf;
615+begin
616+ if Sys.openDir(d, dir) < 0 then return; end;
617+ Buf.init(path, Heap.heap);
618+ defer Buf.done(path);
619+ name, ok := Sys.nextEntry(d);
620+ while ok do
621+ Buf.reset(path);
622+ Buf.str(path, dir); Buf.str(path, "/"); Buf.str(path, name);
623+ f, err := Sys.info(Buf.bytes(path));
624+ if Str.hasSuffix(name, ".raz") and (err = 0) then
625+ INC(count);
626+ print := (print + 1 + f.mtime mod Modulus + f.size mod Modulus) mod Modulus;
627+ end;
628+ name, ok := Sys.nextEntry(d);
629+ end;
630+ Sys.closeDir(d);
631+end;
632+
633+(* A number that changes when a *.raz file in the directories is edited,
634+ added or removed, and how many files there are. *)
635+proc fingerprint(): (I64, INT);
636+var dirs: Names.Vec; i, count: INT; print: I64;
637+begin
638+ watchDirs(dirs);
639+ defer Names.done(dirs);
640+ for i := 0 to dirs.len - 1 do lookIn(Names.all(dirs)[i], print, count); end;
641+ return print, count;
642+end;
643+
644+(* Waits one look's time; a signal that stops raztest ends it at once. *)
645+proc nap;
646+begin
647+ if Sys.ready(signals, WatchMs) then interrupted(Sys.caught(signals)); end;
648+end;
649+
650+(* Waits until the fingerprint is not seen, and then does not change
651+ for one look. *)
652+proc waitChange(seen: I64);
653+var print: I64;
654+begin
655+ repeat
656+ nap;
657+ print, _ := fingerprint();
658+ until print # seen;
659+ repeat
660+ seen := print;
661+ nap;
662+ print, _ := fingerprint();
663+ until print = seen;
664+end;
665+
666+(* Runs the tests, and again at each change, for ever. *)
667+proc watchAll;
668+var seen: I64; n: INT;
669+begin
670+ while true do
671+ if clearing then Out.str(Clear); end;
672+ seen, n := fingerprint();
673+ _ := cycle();
674+ if tap then Out.str("# "); end;
675+ Fmt.print("watching {} files; ^C stops\n", [Fmt.int(n)]);
676+ Out.flush;
677+ waitChange(seen);
678+ end;
679+end;
680+
681+proc main*;
682+begin
683+ readArgs;
684+ setUp;
685+ if watch then watchAll; end;
686+ if not cycle() then Rt.exit(1); end;
543687 end;
544688 end Test.