raz / commit a0997714

commit
a09977148249bd96d084040bad9cf1259b4885dc
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
61fa4786
raztest: builds and runs a program's tests, -j at once

tools/Test.raz, raztest, takes test files, or directories whose *.raz
have a main, builds each with raz -Werror into -o (build/raztest) and
runs it there, in a process group of its own, with -j at once and -t
seconds each. Output goes to Name.out, a file, so nothing is drained
while tests run. A line per test, in order, with the log of a failure
or timeout under it; a summary; status 0, 1 or 2. Colour on a terminal
unless NO_COLOR.

make install puts raztest on PATH with doc/raztest.1; the tarball
carries both. test/check.sh runs the library's tests through it.
Step 3 of TESTING-PLAN.md.

make check: 1027 passed, 1 failed, not this step's: raz --version
says report 2026-10.34 while doc/raz.md is at 10.35.

patch · log

5 files changed, +611 -23

Makefile (history) modified +15 -11

@@ -116,11 +116,12 @@
116116
117117 # raz, its library and its tools go together under $(PREFIX)/lib/raz,
118118 # laid out as in the tarball; the link on PATH works since raz finds
119-# lib/ beside its real path, and a program's make finds tools/ so. The
120-# manual pages go where man looks, beside bin.
119+# lib/ beside its real path, and a program's make finds tools/ so.
120+# raztest, tools/Test.raz, is built onto PATH by this raz. The manual
121+# pages go where man looks, beside bin.
121122 PREFIX = $(HOME)/.local
122123 MAN = $(PREFIX)/share/man
123-TOOLS = Pretty Doc Scan
124+TOOLS = Pretty Doc Scan Test
124125
125126 install: raz build/raz-lib.3
126127 rm -rf $(PREFIX)/lib/raz
@@ -128,21 +129,24 @@
128129 cp -r raz lib $(PREFIX)/lib/raz
129130 cp $(TOOLS:%=tools/%.raz) $(PREFIX)/lib/raz/tools
130131 ln -sf $(PREFIX)/lib/raz/raz $(PREFIX)/bin/raz
131- cp doc/raz.1 $(MAN)/man1/raz.1
132+ ./raz -Werror -o $(PREFIX)/bin/raztest tools/Test.raz
133+ cp doc/raz.1 doc/raztest.1 $(MAN)/man1
132134 cp build/raz-lib.3 $(MAN)/man3/raz-lib.3
133135 cp doc/raz.7 doc/raz-tutorial.7 $(MAN)/man7
134136
135137 uninstall:
136- rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(MAN)/man1/raz.1 $(MAN)/man3/raz-lib.3 \
138+ rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(PREFIX)/bin/raztest \
139+ $(MAN)/man1/raz.1 $(MAN)/man1/raztest.1 $(MAN)/man3/raz-lib.3 \
137140 $(MAN)/man7/raz.7 $(MAN)/man7/raz-tutorial.7
138141
139142 # A release's tarball, build/raz-V-linux-x86_64.tar.gz: raz, the lib/
140143 # it reads beside itself, the tools a program's make may build (the
141-# formatter, Doc and the Scan they share) and the manual pages, V being what raz
142-# --version says. It is made of the commit REV, HEAD unless given: CI
143-# packs the tag it checked out, and site/build.py each tag it lacks. A
144-# release whose tag isn't pushed yet is made with VERSION, its version
145-# without the v: the same tarball as the tag's, once the tag is at REV.
144+# formatter, Doc and the Scan they share, and raztest) and the manual
145+# pages, V being what raz --version says. It is made of the commit
146+# REV, HEAD unless given: CI packs the tag it checked out, and
147+# site/build.py each tag it lacks. A release whose tag isn't pushed yet
148+# is made with VERSION, its version without the v: the same tarball as
149+# the tag's, once the tag is at REV.
146150 #
147151 # The tarball is the same, byte for byte, wherever it is made, so that
148152 # its SHA-256 can be checked against GitHub's or the website's. raz is
@@ -163,7 +167,7 @@
163167 rm -rf build/$$d && mkdir -p build/$$d/man && \
164168 cp -r $(DIST)/raz $(DIST)/lib $(DIST)/LICENSE $(DIST)/README.md build/$$d && \
165169 mkdir build/$$d/tools && cp $(TOOLS:%=$(DIST)/tools/%.raz) build/$$d/tools && \
166- cp $(DIST)/doc/raz.1 $(DIST)/doc/raz.7 $(DIST)/doc/raz-tutorial.7 \
170+ cp $(DIST)/doc/raz.1 $(DIST)/doc/raztest.1 $(DIST)/doc/raz.7 $(DIST)/doc/raz-tutorial.7 \
167171 $(DIST)/build/raz-lib.3 build/$$d/man && \
168172 tar -cf - --format=gnu --sort=name --mtime=@$$(git log -1 --format=%ct $(REV)) \
169173 --owner=0 --group=0 --numeric-owner --mode=a+rX,u+w,go-w \

README.md (history) modified +1 -0

@@ -42,6 +42,7 @@
4242 | `examples/` | Small programs, each with the output it should give |
4343 | `tools/Doc.raz` | Prints what a module exports, with its comments |
4444 | `tools/Pretty.raz` | The formatter: fixes indentation and spacing |
45+| `tools/Test.raz` | `raztest`: builds and runs a program's tests, `-j` at once; `make install` puts it on PATH, `doc/raztest.1` says how |
4546 | `site/` | The website: home page, the tours "Raz, Set, Go" and "More Raz", the report and the latest release's tarball, as plain files in `site/public/`; `make site` rebuilds them, with the report's two PDFs (`make pdf`) |
4647 | `editor/` | Support for vim and emacs |
4748 | `conform/` | The conformance suite: what every implementation must do |

doc/raztest.1 (history) added +145 -0

@@ -0,0 +1,145 @@
1+.\" raztest(1) - build and run the tests of a raz program
2+.Dd October 22, 2026
3+.Dt RAZTEST 1
4+.Os
5+.Sh NAME
6+.Nm raztest
7+.Nd build and run the tests of a raz program
8+.Sh SYNOPSIS
9+.Nm
10+.Op Fl I Ar dir
11+.Op Fl j Ar n
12+.Op Fl t Ar secs
13+.Op Fl o Ar dir
14+.Op Fl r Ar text
15+.Ar file.raz Ns | Ns Ar dir ...
16+.Sh DESCRIPTION
17+.Nm
18+builds each test with
19+.Ic raz -Werror ,
20+runs it, and says which passed.
21+A test is a module whose
22+.Ic main
23+is its test: it passes if it builds and then exits with status 0, as a
24+program that checks with the library's
25+.Ic Check
26+does when no check fails.
27+.Pp
28+The tests are the files given.
29+A directory gives each
30+.Pa *.raz
31+directly in it that has a line starting
32+.Ql proc main* ,
33+in the order of their names.
34+A test's name is its file's, without
35+.Pa .raz ;
36+two tests may not share one.
37+.Pp
38+Each test is built into the
39+.Fl o
40+directory as
41+.Ar name ,
42+and run there with standard input from
43+.Pa /dev/null ,
44+in a process group of its own.
45+What the compiler writes, and then what the test writes on standard
46+output and standard error, goes to
47+.Ar name Ns .out
48+beside it, which stays for a look afterwards.
49+A build or a run that takes longer than the
50+.Fl t
51+seconds has its process group killed.
52+.Pp
53+As each test ends,
54+.Nm
55+writes a line for it, in the order the tests were given: the word
56+.Ql ok ,
57+.Ql FAIL
58+or
59+.Ql TIMEOUT ,
60+the file, for a failure what failed,
61+such as
62+.Ql doesn't compile ,
63+.Ql status 1
64+or
65+.Ql signal 11 ,
66+and the seconds it took to build and run.
67+Under a failure comes its
68+.Pa .out ,
69+indented by four spaces.
70+Last comes a summary:
71+.Bd -literal -offset indent
72+12 passed, 1 failed, 0 timed out, 3.41s
73+.Ed
74+.Pp
75+The three words are in colour when standard output is a terminal.
76+.Sh OPTIONS
77+.Bl -tag -width "-t secs"
78+.It Fl I Ar dir
79+Look for modules in
80+.Ar dir
81+too, as
82+.Ic raz -I
83+does.
84+It may be given more than once.
85+.It Fl j Ar n
86+Build and run
87+.Ar n
88+tests at once; 1 if not given.
89+.It Fl t Ar secs
90+Kill a build, or a run, that takes longer than
91+.Ar secs
92+seconds; 60 if not given.
93+.It Fl o Ar dir
94+Put the programs and their
95+.Pa .out
96+files in
97+.Ar dir ,
98+made if it isn't there;
99+.Pa build/raztest
100+if not given.
101+.It Fl r Ar text
102+Run only the tests whose name holds
103+.Ar text .
104+.It Fl h , Fl \-help
105+Say the options, and exit.
106+.El
107+.Sh ENVIRONMENT
108+.Bl -tag -width "NO_COLOR"
109+.It Ev RAZ
110+The compiler; if unset,
111+.Ic raz
112+on the
113+.Ev PATH .
114+.It Ev NO_COLOR
115+If set and not empty, no colour.
116+.El
117+.Sh EXIT STATUS
118+0 if every test passed; 1 if one failed or timed out; 2 for a mistake
119+in the command line, a file that isn't there, or an output directory
120+that can't be made.
121+Stopped by an interrupt or a hangup, it kills the tests under way and
122+exits with 128 and the signal's number.
123+.Sh EXAMPLES
124+Run a project's tests, four at a time:
125+.Bd -literal -offset indent
126+raztest -j 4 -I src test
127+.Ed
128+.Pp
129+A Makefile's check that uses
130+.Nm
131+where it is installed, and its own loop where it isn't:
132+.Bd -literal -offset indent
133+check:
134+ @if command -v raztest >/dev/null; then raztest -I src test; \e
135+ else fail=0; for f in test/*.raz; do \e
136+ raz -Werror -I src -o build/t $$f && build/t || fail=1; \e
137+ done; test $$fail = 0; fi
138+.Ed
139+.Sh SEE ALSO
140+.Xr raz 1 ,
141+.Xr raz-lib 3
142+.Sh BUGS
143+.Fl j
144+doesn't default to the number of processors, which the library can't
145+tell yet.

test/check.sh (history) modified +18 -12

@@ -267,17 +267,23 @@
267267 echo "FAIL tools/Mutate.raz: $(cat $out/Mutate.cerr)"; fail=$((fail + 1))
268268 fi
269269
270-# each library module's main is its test; Sys over C is tested in test/libc
271-for f in lib/*.raz; do
272- [ $f = lib/Sys.raz ] && continue
273- grep -q '^proc main\*' $f || continue
274- name=lib_$(basename $f .raz)
275- if ! $raz -Werror -o $out/$name $f 2>$out/$name.cerr; then
276- echo "FAIL $f: doesn't compile: $(cat $out/$name.cerr)"; fail=$((fail + 1))
277- elif ! $out/$name > $out/$name.out 2>&1; then
278- echo "FAIL $f: its test says $(cat $out/$name.out)"; fail=$((fail + 1))
279- else pass=$((pass + 1)); fi
280-done
270+# each library module's main is its test, run by tools/Test.raz,
271+# raztest, which says what it counted on its last line; Sys over C is
272+# tested in test/libc
273+if $raz -Werror -o $out/raztest tools/Test.raz 2>$out/raztest.cerr; then
274+ RAZ=$raz $out/raztest -j $(nproc 2>/dev/null || echo 1) -o $out/lib \
275+ $(for f in lib/*.raz; do [ $f != lib/Sys.raz ] && grep -q '^proc main\*' $f && echo $f; done) \
276+ > $out/raztest.out 2>&1
277+ set -- $(tail -1 $out/raztest.out) # N passed, F failed, T timed out, S
278+ if [ "$2" = passed, ] && [ "$6" = timed ]; then
279+ pass=$((pass + $1)); fail=$((fail + $3 + $5))
280+ [ $(($3 + $5)) = 0 ] || grep -v '^ok ' $out/raztest.out
281+ else
282+ echo "FAIL tools/Test.raz:"; cat $out/raztest.out; fail=$((fail + 1))
283+ fi
284+else
285+ echo "FAIL tools/Test.raz: $(cat $out/raztest.cerr)"; fail=$((fail + 1))
286+fi
281287
282288 # CAbi is tools/cabi.c's output, from the C library's own headers:
283289 # compiled with cc, and with musl-gcc, each where it is installed, it
@@ -293,7 +299,7 @@
293299
294300 # Thread's test again, with too little memory for all its threads:
295301 # spawn must fail, with Thread.Failed
296-if (ulimit -v 60000; $out/lib_Thread.linux-x86_64 fail) > /dev/null 2>&1; then pass=$((pass + 1))
302+if (ulimit -v 60000; $out/lib/Thread.linux-x86_64 fail) > /dev/null 2>&1; then pass=$((pass + 1))
297303 else echo "FAIL Thread under a memory limit"; fail=$((fail + 1)); fi
298304
299305 # the compiler's way of going on after an error, in small: a parser

tools/Test.raz (history) added +432 -0

@@ -0,0 +1,432 @@
1+(* Test: raztest, which builds and runs the tests of a raz program.
2+
3+ raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] file.raz|dir ...
4+
5+ A test is a module whose main is its test: it passes if it builds
6+ with raz -Werror and then exits with status 0, as a program that
7+ checks with Check does when no check fails. The tests are the files
8+ given; a directory gives each *.raz directly in it that has a line
9+ starting proc main*. A test's name is its file's, without .raz.
10+
11+ Each is built into the -o directory, as Name, by $RAZ (raz on PATH if
12+ unset), and run there with no input, in a process group of its own;
13+ what the compiler, then the test, write goes to Name.out beside it,
14+ a file, so that nothing need be read while they run and the logs stay
15+ for CI. -j tests go at once, polled every few milliseconds, with no
16+ threads; one that takes longer than -t seconds to build, or to run,
17+ has its group killed.
18+
19+ A line for each test as it ends, in the order given, with the
20+ .out of one that failed or timed out under it; then a summary,
21+
22+ 12 passed, 1 failed, 0 timed out, 3.41s
23+
24+ and status 0 if all passed, 1 if not, 2 for a mistake in the
25+ command line or the setup. The words ok, FAIL and TIMEOUT are in
26+ colour when standard output is a terminal and NO_COLOR is unset.
27+
28+ Needs only the library. Leaves out: TAP and JUnit reports and
29+ watching files (to come), arguments to a test, tests not in raz,
30+ and the number of processors as -j's default, which Sys can't tell:
31+ it is 1. *)
32+module Test;
33+import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files;
34+import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less);
35+
36+const
37+ Table: []Opt.Row = [
38+ Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] file.raz|dir ..."),
39+ Opt.option("", 'I', "include", "dir", "look for modules in dir too, as raz -I"),
40+ Opt.option("", 'j', "jobs", "n", "run n tests at once (1)"),
41+ Opt.option("", 't', "timeout", "secs", "kill a build or run after secs (60)"),
42+ Opt.option("", 'o', "out", "dir", "put the programs and their .out in dir (build/raztest)"),
43+ Opt.option("", 'r', "run", "text", "run only the tests whose name holds text")];
44+ PollMs = 10; (* how long to wait between looks at the running tests *)
45+ Second = 1000000000; (* a second in Sys.clock's nanoseconds *)
46+ Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *)
47+ Red = "\x1b[31m";
48+ Yellow = "\x1b[33m";
49+ Plain = "\x1b[0m"; (* back to no colour *)
50+
51+type
52+ (* Where a test is: not started, being built, being run, or ended. *)
53+ Phase = (waiting, building, running, done);
54+
55+ (* How a test ended. *)
56+ Result = (passed, failed, timedOut);
57+
58+ (* A test and how it is going. *)
59+ Job = record
60+ path: []U8; (* its file, as given *)
61+ name: []U8; (* the file's name without .raz *)
62+ bin: []U8; (* its program, in the out directory *)
63+ log: []U8; (* its .out, beside the program *)
64+ phase: Phase;
65+ pid: INT; (* the compiler's or the test's, while building or running *)
66+ began: I64; (* Sys.clock when its build started *)
67+ deadline: I64; (* Sys.clock when the phase under way is killed *)
68+ took: I64; (* nanoseconds from its build's start to its end *)
69+ result: Result;
70+ why: Buf.Buf; (* for a failure, what failed: "status 1" *)
71+ end;
72+
73+var
74+ raz: []U8; (* the compiler: $RAZ, or raz on PATH *)
75+ includes: Names.Vec; (* the -I directories *)
76+ jobs: INT; (* -j: tests at once *)
77+ timeout: I64; (* -t, in nanoseconds *)
78+ outDir: []U8; (* -o *)
79+ only: []U8; (* -r: the text a name must hold, or "" *)
80+ tests: Jobs.Vec; (* in the order they are reported *)
81+ busy: INT; (* tests being built or run *)
82+ devNull: INT; (* /dev/null, each test's standard input *)
83+ signals: INT; (* the fd the interrupting signals come on *)
84+ colour: BOOL; (* the words of the results are coloured *)
85+
86+(* Whether a sorts before b, for sorting a directory's files. *)
87+proc less(a, b: []U8): BOOL;
88+begin
89+ return Str.cmp(a, b) < 0;
90+end;
91+
92+(* A mistake in the setup: the message, and status 2. *)
93+proc die(f: []U8; args: []Fmt.Arg);
94+var b: Buf.Buf;
95+begin
96+ Buf.init(b, Heap.heap);
97+ Fmt.write(b, "raztest: ", []);
98+ Fmt.write(b, f, args);
99+ Fmt.eprint("{}\n", [Fmt.str(Buf.bytes(b))]);
100+ Rt.exit(2);
101+end;
102+
103+(* a, b and c joined, in memory of its own that is never freed. *)
104+proc join(a, b, c: []U8): []U8;
105+var s: Buf.Buf;
106+begin
107+ Buf.init(s, Heap.heap);
108+ Buf.str(s, a); Buf.str(s, b); Buf.str(s, c);
109+ return Buf.bytes(s);
110+end;
111+
112+(* Whether the source at path has a line that starts proc main*. *)
113+proc hasMain(path: []U8): BOOL;
114+var b: Buf.Buf; src: []U8;
115+begin
116+ Buf.init(b, Heap.heap);
117+ defer Buf.done(b);
118+ if not Files.read(path, b) then return false; end;
119+ src := Buf.bytes(b);
120+ return Str.hasPrefix(src, "proc main*") or (Str.find(src, "\nproc main*") >= 0);
121+end;
122+
123+(* Adds the test at path, unless -r leaves it out. *)
124+proc add(path: []U8);
125+var j: ^Job; name: []U8;
126+begin
127+ name := path[Str.findLast(path, "/") + 1..LEN(path)];
128+ if Str.hasSuffix(name, ".raz") then name := name[0..LEN(name) - 4]; end;
129+ if Str.find(name, only) < 0 then return; end;
130+ j := Jobs.add(tests);
131+ j^ := {path: path, name: name, bin: join(outDir, "/", name), phase: waiting};
132+ j^.log := join(j^.bin, ".out", "");
133+ Buf.init(j^.why, Heap.heap);
134+end;
135+
136+(* Adds the tests of the directory dir: its *.raz with a main, by name. *)
137+proc addDir(dir: []U8);
138+var d: Sys.Dir; files: Names.Vec; name, path: []U8; ok: BOOL; err, i: INT;
139+begin
140+ err := Sys.openDir(d, dir);
141+ if err < 0 then die("can't read {}: {}", [Fmt.str(dir), Fmt.str(Sys.errorText(err))]); end;
142+ Names.init(files, Heap.heap);
143+ name, ok := Sys.nextEntry(d);
144+ while ok do
145+ path := join(dir, "/", name);
146+ if Str.hasSuffix(name, ".raz") and hasMain(path) then Names.push(files, path); end;
147+ name, ok := Sys.nextEntry(d);
148+ end;
149+ Sys.closeDir(d);
150+ ByName.sort(Names.all(files));
151+ for i := 0 to files.len - 1 do add(Names.all(files)[i]); end;
152+end;
153+
154+(* Adds what an operand names: a directory's tests, or a file. *)
155+proc addOperand(path: []U8);
156+var f: Sys.Info; err: INT;
157+begin
158+ f, err := Sys.info(path);
159+ if err < 0 then die("{}: {}", [Fmt.str(path), Fmt.str(Sys.errorText(err))]); end;
160+ if f.dir then addDir(path); else add(path); end;
161+end;
162+
163+(* Stops if two tests have one name, as their programs would. *)
164+proc checkNames;
165+var all: []Job; i, k: INT;
166+begin
167+ all := Jobs.all(tests);
168+ for i := 0 to LEN(all) - 1 do
169+ for k := 0 to i - 1 do
170+ if all[i].name = all[k].name then
171+ die("{} and {} are both named {}", [Fmt.str(all[k].path), Fmt.str(all[i].path), Fmt.str(all[i].name)]);
172+ end;
173+ end;
174+ end;
175+end;
176+
177+(* Reads the command line into the settings and the list of tests. *)
178+proc readArgs;
179+var p: Opt.Parser; arg: []U8; ok: BOOL;
180+begin
181+ jobs := 1; timeout := 60 * Second; outDir := "build/raztest";
182+ Names.init(includes, Heap.heap);
183+ Jobs.init(tests, Heap.heap);
184+ Opt.init(p, "raztest", Table);
185+ while Opt.next(p) do
186+ if p.letter = 'I' then Names.push(includes, p.value);
187+ elsif p.letter = 'j' then jobs := INT(Opt.number(p, 1, 1000));
188+ elsif p.letter = 't' then timeout := Opt.number(p, 1, 1000000) * Second;
189+ elsif p.letter = 'o' then outDir := p.value;
190+ elsif p.letter = 'r' then only := p.value;
191+ end;
192+ end;
193+ arg, ok := Opt.operand(p);
194+ if not ok then Opt.fail(p, "no tests named; raztest --help says how"); end;
195+ repeat
196+ addOperand(arg);
197+ arg, ok := Opt.operand(p);
198+ until not ok;
199+ checkNames;
200+end;
201+
202+(* Makes the directory dir and those it is in, as mkdir -p does. *)
203+proc makeDirs(dir: []U8);
204+var i, err: INT; f: Sys.Info;
205+begin
206+ for i := 1 to LEN(dir) - 1 do
207+ if dir[i] = '/' then _ := Sys.makeDir(dir[0..i]); end;
208+ end;
209+ err := Sys.makeDir(dir);
210+ if err = Sys.errExists then
211+ f, err := Sys.info(dir);
212+ if (err = 0) and not f.dir then err := Sys.errNotDir; end;
213+ end;
214+ if err < 0 then
215+ die("can't make {}: {}", [Fmt.str(dir), Fmt.str(Sys.errorText(err))]);
216+ end;
217+end;
218+
219+(* Readies what every test needs: the compiler's name, the out
220+ directory, /dev/null, the signals that stop raztest, and colour. *)
221+proc setUp;
222+begin
223+ raz := Sys.env("RAZ");
224+ if LEN(raz) = 0 then raz := "raz"; end;
225+ makeDirs(outDir);
226+ devNull := Sys.openRead("/dev/null");
227+ if devNull < 0 then die("can't open /dev/null: {}", [Fmt.str(Sys.errorText(devNull))]); end;
228+ signals := Sys.catch([Sys.sigInt, Sys.sigTerm, Sys.sigHup]);
229+ if signals < 0 then die("can't catch signals: {}", [Fmt.str(Sys.errorText(signals))]); end;
230+ colour := Sys.isTerminal(Sys.stdout) and (LEN(Sys.env("NO_COLOR")) = 0);
231+end;
232+
233+(* Whether the test j is being built or run. *)
234+proc active(const j: Job): BOOL;
235+begin
236+ return (j.phase = building) or (j.phase = running);
237+end;
238+
239+(* Ends the test j's phase as result: what failed is in j.why. *)
240+proc finish(var j: Job; result: Result);
241+begin
242+ if active(j) then DEC(busy); end;
243+ j.phase := done;
244+ j.result := result;
245+ j.took := Sys.clock() - j.began;
246+end;
247+
248+(* Starts args[0] with its output and errors into j's log, as j's next
249+ phase, in a group of its own; false, with j failed, if it can't. *)
250+proc start(var j: Job; args: [][]U8; phase: Phase): BOOL;
251+var fd: INT;
252+begin
253+ fd := Sys.create(j.log);
254+ if fd < 0 then
255+ Fmt.write(j.why, "can't write {}: {}", [Fmt.str(j.log), Fmt.str(Sys.errorText(fd))]);
256+ finish(j, failed); return false;
257+ end;
258+ j.pid := Sys.spawn({path: args[0], args: args, fds: [devNull, fd, fd], group: -1});
259+ Sys.close(fd);
260+ if j.pid < 0 then
261+ Fmt.write(j.why, "can't start {}: {}", [Fmt.str(args[0]), Fmt.str(Sys.errorText(j.pid))]);
262+ finish(j, failed); return false;
263+ end;
264+ if j.phase = waiting then INC(busy); end;
265+ j.phase := phase;
266+ j.deadline := Sys.clock() + timeout;
267+ return true;
268+end;
269+
270+(* Starts building the test j: raz -Werror, the -I directories, -o. *)
271+proc build(var j: Job);
272+var args: Names.Vec; i: INT;
273+begin
274+ Names.init(args, Heap.heap);
275+ defer Names.done(args);
276+ Names.push(args, raz);
277+ Names.push(args, "-Werror");
278+ for i := 0 to includes.len - 1 do
279+ Names.push(args, "-I");
280+ Names.push(args, Names.all(includes)[i]);
281+ end;
282+ Names.push(args, "-o");
283+ Names.push(args, j.bin);
284+ Names.push(args, j.path);
285+ j.began := Sys.clock();
286+ _ := start(j, Names.all(args), building);
287+end;
288+
289+(* The test j's phase has ended, how and with value as Sys.wait says:
290+ on to running it, or the end. *)
291+proc ended(var j: Job; how: Sys.Status; value: INT);
292+var args: [1][]U8;
293+begin
294+ if (j.phase = building) and (how = Sys.exited) and (value = 0) then
295+ args[0] := j.bin;
296+ _ := start(j, args, running);
297+ return;
298+ end;
299+ if j.phase = building then
300+ if (how = Sys.exited) and (value = 1) then Buf.str(j.why, "doesn't compile");
301+ elsif how = Sys.exited then Fmt.write(j.why, "raz status {}", [Fmt.int(value)]);
302+ else Fmt.write(j.why, "raz signal {}", [Fmt.int(value)]);
303+ end;
304+ finish(j, failed);
305+ elsif (how = Sys.exited) and (value = 0) then finish(j, passed);
306+ elsif how = Sys.exited then Fmt.write(j.why, "status {}", [Fmt.int(value)]); finish(j, failed);
307+ else Fmt.write(j.why, "signal {}", [Fmt.int(value)]); finish(j, failed);
308+ end;
309+end;
310+
311+(* Looks at the test j, being built or run, without waiting: whether
312+ it has ended its phase, or run out of time and been killed. *)
313+proc poll(var j: Job): BOOL;
314+var pid, value: INT; how: Sys.Status;
315+begin
316+ pid, how, value := Sys.wait(j.pid, false);
317+ if pid < 0 then
318+ Fmt.write(j.why, "lost: {}", [Fmt.str(Sys.errorText(pid))]);
319+ finish(j, failed);
320+ elsif (pid > 0) and (how # Sys.stopped) then
321+ ended(j, how, value);
322+ elsif Sys.clock() > j.deadline then
323+ _ := Sys.kill(-j.pid, Sys.sigKill);
324+ _, _, _ := Sys.wait(j.pid, true);
325+ finish(j, timedOut);
326+ else
327+ return false;
328+ end;
329+ return true;
330+end;
331+
332+(* Writes word, in colour c on a terminal, padded to a column of 8. *)
333+proc putWord(word, c: []U8);
334+var i: INT;
335+begin
336+ if colour then Out.str(c); Out.str(word); Out.str(Plain); else Out.str(word); end;
337+ for i := LEN(word) to 7 do Out.char(' '); end;
338+end;
339+
340+(* Writes the file at path, each line indented by 4 spaces. *)
341+proc putIndented(path: []U8);
342+var b: Buf.Buf; s, line, rest: []U8; more: BOOL;
343+begin
344+ Buf.init(b, Heap.heap);
345+ defer Buf.done(b);
346+ if not Files.read(path, b) then Fmt.print(" (can't read {})\n", [Fmt.str(path)]); return; end;
347+ s := Buf.bytes(b);
348+ while LEN(s) > 0 do
349+ line, rest, more := Str.cut(s, "\n");
350+ Fmt.print(" {}\n", [Fmt.str(line)]);
351+ s := rest;
352+ if not more then break; end;
353+ end;
354+end;
355+
356+(* Writes the line of the ended test j, and its log if it failed. *)
357+proc report(const j: Job);
358+var secs: Fmt.Arg;
359+begin
360+ secs := Fmt.real(F64(j.took) / F64(Second), 2);
361+ if j.result = passed then
362+ putWord("ok", Green);
363+ Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]);
364+ elsif j.result = timedOut then
365+ putWord("TIMEOUT", Yellow);
366+ Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]);
367+ putIndented(j.log);
368+ else
369+ putWord("FAIL", Red);
370+ Fmt.print("{} {} {}s\n", [Fmt.str(j.path), Fmt.str(Buf.bytes(j.why)), secs]);
371+ putIndented(j.log);
372+ end;
373+ Out.flush;
374+end;
375+
376+(* Kills every test being built or run, after a signal to stop, and
377+ ends with the shell's status for it, 128 and its number. *)
378+proc interrupted(sig: INT);
379+var all: []Job; i: INT;
380+begin
381+ all := Jobs.all(tests);
382+ for i := 0 to LEN(all) - 1 do
383+ if active(all[i]) then
384+ _ := Sys.kill(-all[i].pid, Sys.sigKill);
385+ _, _, _ := Sys.wait(all[i].pid, true);
386+ end;
387+ end;
388+ Out.flush;
389+ Rt.exit(128 + sig);
390+end;
391+
392+(* Builds and runs every test, jobs at a time, and reports each in
393+ order as soon as it and those before it have ended. *)
394+proc runAll;
395+var all: []Job; next, shown, i: INT; changed: BOOL;
396+begin
397+ all := Jobs.all(tests);
398+ next := 0; shown := 0;
399+ while shown < LEN(all) do
400+ while (busy < jobs) and (next < LEN(all)) do build(all[next]); INC(next); end;
401+ changed := false;
402+ for i := 0 to next - 1 do
403+ if active(all[i]) and poll(all[i]) then changed := true; end;
404+ end;
405+ while (shown < LEN(all)) and (all[shown].phase = done) do report(all[shown]); INC(shown); end;
406+ if not changed and Sys.ready(signals, PollMs) then interrupted(Sys.caught(signals)); end;
407+ end;
408+end;
409+
410+(* The summary line, and status 1 if any test failed. *)
411+proc summary(took: I64);
412+var all: []Job; i: INT; count: [3]INT;
413+begin
414+ all := Jobs.all(tests);
415+ for i := 0 to LEN(all) - 1 do INC(count[INT(all[i].result)]); end;
416+ Fmt.print("{} passed, {} failed, {} timed out, {}s\n",
417+ [Fmt.int(count[INT(passed)]), Fmt.int(count[INT(failed)]), Fmt.int(count[INT(timedOut)]),
418+ Fmt.real(F64(took) / F64(Second), 2)]);
419+ Out.flush;
420+ if count[INT(passed)] < LEN(all) then Rt.exit(1); end;
421+end;
422+
423+proc main*;
424+var began: I64;
425+begin
426+ began := Sys.clock();
427+ readArgs;
428+ setUp;
429+ runAll;
430+ summary(Sys.clock() - began);
431+end;
432+end Test.