From a09977148249bd96d084040bad9cf1259b4885dc Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:05:02 +0100 Subject: [PATCH] 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. --- diff --git a/Makefile b/Makefile index b380516..fa2cbdc 100644 --- a/Makefile +++ b/Makefile @@ -116,11 +116,12 @@ # raz, its library and its tools go together under $(PREFIX)/lib/raz, # laid out as in the tarball; the link on PATH works since raz finds -# lib/ beside its real path, and a program's make finds tools/ so. The -# manual pages go where man looks, beside bin. +# lib/ beside its real path, and a program's make finds tools/ so. +# raztest, tools/Test.raz, is built onto PATH by this raz. The manual +# pages go where man looks, beside bin. PREFIX = $(HOME)/.local MAN = $(PREFIX)/share/man -TOOLS = Pretty Doc Scan +TOOLS = Pretty Doc Scan Test install: raz build/raz-lib.3 rm -rf $(PREFIX)/lib/raz @@ -128,21 +129,24 @@ cp -r raz lib $(PREFIX)/lib/raz cp $(TOOLS:%=tools/%.raz) $(PREFIX)/lib/raz/tools ln -sf $(PREFIX)/lib/raz/raz $(PREFIX)/bin/raz - cp doc/raz.1 $(MAN)/man1/raz.1 + ./raz -Werror -o $(PREFIX)/bin/raztest tools/Test.raz + cp doc/raz.1 doc/raztest.1 $(MAN)/man1 cp build/raz-lib.3 $(MAN)/man3/raz-lib.3 cp doc/raz.7 doc/raz-tutorial.7 $(MAN)/man7 uninstall: - rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(MAN)/man1/raz.1 $(MAN)/man3/raz-lib.3 \ + rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(PREFIX)/bin/raztest \ + $(MAN)/man1/raz.1 $(MAN)/man1/raztest.1 $(MAN)/man3/raz-lib.3 \ $(MAN)/man7/raz.7 $(MAN)/man7/raz-tutorial.7 # A release's tarball, build/raz-V-linux-x86_64.tar.gz: raz, the lib/ # it reads beside itself, the tools a program's make may build (the -# formatter, Doc and the Scan they share) and the manual pages, V being what raz -# --version says. It is made of the commit REV, HEAD unless given: CI -# packs the tag it checked out, and site/build.py each tag it lacks. A -# release whose tag isn't pushed yet is made with VERSION, its version -# without the v: the same tarball as the tag's, once the tag is at REV. +# formatter, Doc and the Scan they share, and raztest) and the manual +# pages, V being what raz --version says. It is made of the commit +# REV, HEAD unless given: CI packs the tag it checked out, and +# site/build.py each tag it lacks. A release whose tag isn't pushed yet +# is made with VERSION, its version without the v: the same tarball as +# the tag's, once the tag is at REV. # # The tarball is the same, byte for byte, wherever it is made, so that # its SHA-256 can be checked against GitHub's or the website's. raz is @@ -163,7 +167,7 @@ rm -rf build/$$d && mkdir -p build/$$d/man && \ cp -r $(DIST)/raz $(DIST)/lib $(DIST)/LICENSE $(DIST)/README.md build/$$d && \ mkdir build/$$d/tools && cp $(TOOLS:%=$(DIST)/tools/%.raz) build/$$d/tools && \ - cp $(DIST)/doc/raz.1 $(DIST)/doc/raz.7 $(DIST)/doc/raz-tutorial.7 \ + cp $(DIST)/doc/raz.1 $(DIST)/doc/raztest.1 $(DIST)/doc/raz.7 $(DIST)/doc/raz-tutorial.7 \ $(DIST)/build/raz-lib.3 build/$$d/man && \ tar -cf - --format=gnu --sort=name --mtime=@$$(git log -1 --format=%ct $(REV)) \ --owner=0 --group=0 --numeric-owner --mode=a+rX,u+w,go-w \ diff --git a/README.md b/README.md index f2022e4..1408496 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ | `examples/` | Small programs, each with the output it should give | | `tools/Doc.raz` | Prints what a module exports, with its comments | | `tools/Pretty.raz` | The formatter: fixes indentation and spacing | +| `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 | | `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`) | | `editor/` | Support for vim and emacs | | `conform/` | The conformance suite: what every implementation must do | diff --git a/doc/raztest.1 b/doc/raztest.1 new file mode 100644 index 0000000..e254f40 --- /dev/null +++ b/doc/raztest.1 @@ -0,0 +1,145 @@ +.\" raztest(1) - build and run the tests of a raz program +.Dd October 22, 2026 +.Dt RAZTEST 1 +.Os +.Sh NAME +.Nm raztest +.Nd build and run the tests of a raz program +.Sh SYNOPSIS +.Nm +.Op Fl I Ar dir +.Op Fl j Ar n +.Op Fl t Ar secs +.Op Fl o Ar dir +.Op Fl r Ar text +.Ar file.raz Ns | Ns Ar dir ... +.Sh DESCRIPTION +.Nm +builds each test with +.Ic raz -Werror , +runs it, and says which passed. +A test is a module whose +.Ic main +is its test: it passes if it builds and then exits with status 0, as a +program that checks with the library's +.Ic Check +does when no check fails. +.Pp +The tests are the files given. +A directory gives each +.Pa *.raz +directly in it that has a line starting +.Ql proc main* , +in the order of their names. +A test's name is its file's, without +.Pa .raz ; +two tests may not share one. +.Pp +Each test is built into the +.Fl o +directory as +.Ar name , +and run there with standard input from +.Pa /dev/null , +in a process group of its own. +What the compiler writes, and then what the test writes on standard +output and standard error, goes to +.Ar name Ns .out +beside it, which stays for a look afterwards. +A build or a run that takes longer than the +.Fl t +seconds has its process group killed. +.Pp +As each test ends, +.Nm +writes a line for it, in the order the tests were given: the word +.Ql ok , +.Ql FAIL +or +.Ql TIMEOUT , +the file, for a failure what failed, +such as +.Ql doesn't compile , +.Ql status 1 +or +.Ql signal 11 , +and the seconds it took to build and run. +Under a failure comes its +.Pa .out , +indented by four spaces. +Last comes a summary: +.Bd -literal -offset indent +12 passed, 1 failed, 0 timed out, 3.41s +.Ed +.Pp +The three words are in colour when standard output is a terminal. +.Sh OPTIONS +.Bl -tag -width "-t secs" +.It Fl I Ar dir +Look for modules in +.Ar dir +too, as +.Ic raz -I +does. +It may be given more than once. +.It Fl j Ar n +Build and run +.Ar n +tests at once; 1 if not given. +.It Fl t Ar secs +Kill a build, or a run, that takes longer than +.Ar secs +seconds; 60 if not given. +.It Fl o Ar dir +Put the programs and their +.Pa .out +files in +.Ar dir , +made if it isn't there; +.Pa build/raztest +if not given. +.It Fl r Ar text +Run only the tests whose name holds +.Ar text . +.It Fl h , Fl \-help +Say the options, and exit. +.El +.Sh ENVIRONMENT +.Bl -tag -width "NO_COLOR" +.It Ev RAZ +The compiler; if unset, +.Ic raz +on the +.Ev PATH . +.It Ev NO_COLOR +If set and not empty, no colour. +.El +.Sh EXIT STATUS +0 if every test passed; 1 if one failed or timed out; 2 for a mistake +in the command line, a file that isn't there, or an output directory +that can't be made. +Stopped by an interrupt or a hangup, it kills the tests under way and +exits with 128 and the signal's number. +.Sh EXAMPLES +Run a project's tests, four at a time: +.Bd -literal -offset indent +raztest -j 4 -I src test +.Ed +.Pp +A Makefile's check that uses +.Nm +where it is installed, and its own loop where it isn't: +.Bd -literal -offset indent +check: + @if command -v raztest >/dev/null; then raztest -I src test; \e + else fail=0; for f in test/*.raz; do \e + raz -Werror -I src -o build/t $$f && build/t || fail=1; \e + done; test $$fail = 0; fi +.Ed +.Sh SEE ALSO +.Xr raz 1 , +.Xr raz-lib 3 +.Sh BUGS +.Fl j +doesn't default to the number of processors, which the library can't +tell yet. diff --git a/test/check.sh b/test/check.sh index bbd60be..8fdc2ae 100755 --- a/test/check.sh +++ b/test/check.sh @@ -267,17 +267,23 @@ echo "FAIL tools/Mutate.raz: $(cat $out/Mutate.cerr)"; fail=$((fail + 1)) fi -# each library module's main is its test; Sys over C is tested in test/libc -for f in lib/*.raz; do - [ $f = lib/Sys.raz ] && continue - grep -q '^proc main\*' $f || continue - name=lib_$(basename $f .raz) - if ! $raz -Werror -o $out/$name $f 2>$out/$name.cerr; then - echo "FAIL $f: doesn't compile: $(cat $out/$name.cerr)"; fail=$((fail + 1)) - elif ! $out/$name > $out/$name.out 2>&1; then - echo "FAIL $f: its test says $(cat $out/$name.out)"; fail=$((fail + 1)) - else pass=$((pass + 1)); fi -done +# each library module's main is its test, run by tools/Test.raz, +# raztest, which says what it counted on its last line; Sys over C is +# tested in test/libc +if $raz -Werror -o $out/raztest tools/Test.raz 2>$out/raztest.cerr; then + RAZ=$raz $out/raztest -j $(nproc 2>/dev/null || echo 1) -o $out/lib \ + $(for f in lib/*.raz; do [ $f != lib/Sys.raz ] && grep -q '^proc main\*' $f && echo $f; done) \ + > $out/raztest.out 2>&1 + set -- $(tail -1 $out/raztest.out) # N passed, F failed, T timed out, S + if [ "$2" = passed, ] && [ "$6" = timed ]; then + pass=$((pass + $1)); fail=$((fail + $3 + $5)) + [ $(($3 + $5)) = 0 ] || grep -v '^ok ' $out/raztest.out + else + echo "FAIL tools/Test.raz:"; cat $out/raztest.out; fail=$((fail + 1)) + fi +else + echo "FAIL tools/Test.raz: $(cat $out/raztest.cerr)"; fail=$((fail + 1)) +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 @@ -293,7 +299,7 @@ # Thread's test again, with too little memory for all its threads: # spawn must fail, with Thread.Failed -if (ulimit -v 60000; $out/lib_Thread.linux-x86_64 fail) > /dev/null 2>&1; then pass=$((pass + 1)) +if (ulimit -v 60000; $out/lib/Thread.linux-x86_64 fail) > /dev/null 2>&1; then pass=$((pass + 1)) else echo "FAIL Thread under a memory limit"; fail=$((fail + 1)); fi # the compiler's way of going on after an error, in small: a parser diff --git a/tools/Test.raz b/tools/Test.raz new file mode 100644 index 0000000..8e27ff2 --- /dev/null +++ b/tools/Test.raz @@ -0,0 +1,432 @@ +(* Test: raztest, which builds and runs the tests of a raz program. + + raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] file.raz|dir ... + + A test is a module whose main is its test: it passes if it builds + with raz -Werror and then exits with status 0, as a program that + checks with Check does when no check fails. The tests are the files + given; a directory gives each *.raz directly in it that has a line + starting proc main*. A test's name is its file's, without .raz. + + Each is built into the -o directory, as Name, by $RAZ (raz on PATH if + unset), and run there with no input, in a process group of its own; + what the compiler, then the test, write goes to Name.out beside it, + a file, so that nothing need be read while they run and the logs stay + for CI. -j tests go at once, polled every few milliseconds, with no + threads; one that takes longer than -t seconds to build, or to run, + has its group killed. + + A line for each test as it ends, in the order given, with the + .out of one that failed or timed out under it; then a summary, + + 12 passed, 1 failed, 0 timed out, 3.41s + + and status 0 if all passed, 1 if not, 2 for a mistake in the + command line or the setup. The words ok, FAIL and TIMEOUT are in + colour when standard output is a terminal and NO_COLOR is unset. + + Needs only the library. Leaves out: TAP and JUnit reports and + 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. *) +module Test; +import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files; +import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less); + +const + Table: []Opt.Row = [ + Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] 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")]; + PollMs = 10; (* how long to wait between looks at the running tests *) + Second = 1000000000; (* a second in Sys.clock's nanoseconds *) + Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *) + Red = "\x1b[31m"; + Yellow = "\x1b[33m"; + Plain = "\x1b[0m"; (* back to no colour *) + +type + (* Where a test is: not started, being built, being run, or ended. *) + Phase = (waiting, building, running, done); + + (* How a test ended. *) + Result = (passed, failed, timedOut); + + (* A test and how it is going. *) + Job = record + path: []U8; (* its file, as given *) + name: []U8; (* the file's name without .raz *) + bin: []U8; (* its program, in the out directory *) + log: []U8; (* its .out, beside the program *) + phase: Phase; + pid: INT; (* the compiler's or the test's, while building or running *) + began: I64; (* Sys.clock when its build started *) + deadline: I64; (* Sys.clock when the phase under way is killed *) + took: I64; (* nanoseconds from its build's start to its end *) + result: Result; + why: Buf.Buf; (* for a failure, what failed: "status 1" *) + end; + +var + raz: []U8; (* the compiler: $RAZ, or raz on PATH *) + includes: Names.Vec; (* the -I directories *) + jobs: INT; (* -j: tests at once *) + timeout: I64; (* -t, in nanoseconds *) + outDir: []U8; (* -o *) + only: []U8; (* -r: the text a name must hold, or "" *) + tests: Jobs.Vec; (* in the order they are reported *) + busy: INT; (* tests being built or run *) + devNull: INT; (* /dev/null, each test's standard input *) + signals: INT; (* the fd the interrupting signals come on *) + colour: BOOL; (* the words of the results are coloured *) + +(* Whether a sorts before b, for sorting a directory's files. *) +proc less(a, b: []U8): BOOL; +begin + return Str.cmp(a, b) < 0; +end; + +(* A mistake in the setup: the message, and status 2. *) +proc die(f: []U8; args: []Fmt.Arg); +var b: Buf.Buf; +begin + Buf.init(b, Heap.heap); + Fmt.write(b, "raztest: ", []); + Fmt.write(b, f, args); + Fmt.eprint("{}\n", [Fmt.str(Buf.bytes(b))]); + Rt.exit(2); +end; + +(* a, b and c joined, in memory of its own that is never freed. *) +proc join(a, b, c: []U8): []U8; +var s: Buf.Buf; +begin + Buf.init(s, Heap.heap); + Buf.str(s, a); Buf.str(s, b); Buf.str(s, c); + return Buf.bytes(s); +end; + +(* Whether the source at path has a line that starts proc main*. *) +proc hasMain(path: []U8): BOOL; +var b: Buf.Buf; src: []U8; +begin + Buf.init(b, Heap.heap); + defer Buf.done(b); + if not Files.read(path, b) then return false; end; + src := Buf.bytes(b); + return Str.hasPrefix(src, "proc main*") or (Str.find(src, "\nproc main*") >= 0); +end; + +(* Adds the test at path, unless -r leaves it out. *) +proc add(path: []U8); +var j: ^Job; name: []U8; +begin + name := path[Str.findLast(path, "/") + 1..LEN(path)]; + if Str.hasSuffix(name, ".raz") then name := name[0..LEN(name) - 4]; end; + if Str.find(name, only) < 0 then return; end; + j := Jobs.add(tests); + j^ := {path: path, name: name, bin: join(outDir, "/", name), phase: waiting}; + j^.log := join(j^.bin, ".out", ""); + Buf.init(j^.why, Heap.heap); +end; + +(* Adds the tests of the directory dir: its *.raz with a main, by name. *) +proc addDir(dir: []U8); +var d: Sys.Dir; files: Names.Vec; name, path: []U8; ok: BOOL; err, i: INT; +begin + err := Sys.openDir(d, dir); + if err < 0 then die("can't read {}: {}", [Fmt.str(dir), Fmt.str(Sys.errorText(err))]); end; + Names.init(files, Heap.heap); + name, ok := Sys.nextEntry(d); + while ok do + path := join(dir, "/", name); + if Str.hasSuffix(name, ".raz") and hasMain(path) then Names.push(files, path); end; + name, ok := Sys.nextEntry(d); + end; + Sys.closeDir(d); + ByName.sort(Names.all(files)); + for i := 0 to files.len - 1 do add(Names.all(files)[i]); end; +end; + +(* Adds what an operand names: a directory's tests, or a file. *) +proc addOperand(path: []U8); +var f: Sys.Info; err: INT; +begin + f, err := Sys.info(path); + if err < 0 then die("{}: {}", [Fmt.str(path), Fmt.str(Sys.errorText(err))]); end; + if f.dir then addDir(path); else add(path); end; +end; + +(* Stops if two tests have one name, as their programs would. *) +proc checkNames; +var all: []Job; i, k: INT; +begin + all := Jobs.all(tests); + for i := 0 to LEN(all) - 1 do + for k := 0 to i - 1 do + if all[i].name = all[k].name then + die("{} and {} are both named {}", [Fmt.str(all[k].path), Fmt.str(all[i].path), Fmt.str(all[i].name)]); + end; + end; + end; +end; + +(* Reads the command line into the settings and the list of tests. *) +proc readArgs; +var p: Opt.Parser; arg: []U8; ok: BOOL; +begin + jobs := 1; timeout := 60 * Second; outDir := "build/raztest"; + Names.init(includes, Heap.heap); + Jobs.init(tests, Heap.heap); + Opt.init(p, "raztest", Table); + while Opt.next(p) do + if p.letter = 'I' then Names.push(includes, p.value); + elsif p.letter = 'j' then jobs := INT(Opt.number(p, 1, 1000)); + 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; + end; + end; + arg, ok := Opt.operand(p); + if not ok then Opt.fail(p, "no tests named; raztest --help says how"); end; + repeat + addOperand(arg); + arg, ok := Opt.operand(p); + until not ok; + checkNames; +end; + +(* Makes the directory dir and those it is in, as mkdir -p does. *) +proc makeDirs(dir: []U8); +var i, err: INT; f: Sys.Info; +begin + for i := 1 to LEN(dir) - 1 do + if dir[i] = '/' then _ := Sys.makeDir(dir[0..i]); end; + end; + err := Sys.makeDir(dir); + if err = Sys.errExists then + f, err := Sys.info(dir); + if (err = 0) and not f.dir then err := Sys.errNotDir; end; + end; + if err < 0 then + die("can't make {}: {}", [Fmt.str(dir), Fmt.str(Sys.errorText(err))]); + end; +end; + +(* Readies what every test needs: the compiler's name, the out + directory, /dev/null, the signals that stop raztest, and colour. *) +proc setUp; +begin + raz := Sys.env("RAZ"); + if LEN(raz) = 0 then raz := "raz"; end; + makeDirs(outDir); + devNull := Sys.openRead("/dev/null"); + if devNull < 0 then die("can't open /dev/null: {}", [Fmt.str(Sys.errorText(devNull))]); end; + 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); +end; + +(* Whether the test j is being built or run. *) +proc active(const j: Job): BOOL; +begin + return (j.phase = building) or (j.phase = running); +end; + +(* Ends the test j's phase as result: what failed is in j.why. *) +proc finish(var j: Job; result: Result); +begin + if active(j) then DEC(busy); end; + j.phase := done; + j.result := result; + j.took := Sys.clock() - j.began; +end; + +(* Starts args[0] with its output and errors into j's log, as j's next + phase, in a group of its own; false, with j failed, if it can't. *) +proc start(var j: Job; args: [][]U8; phase: Phase): BOOL; +var fd: INT; +begin + fd := Sys.create(j.log); + if fd < 0 then + Fmt.write(j.why, "can't write {}: {}", [Fmt.str(j.log), Fmt.str(Sys.errorText(fd))]); + finish(j, failed); return false; + end; + j.pid := Sys.spawn({path: args[0], args: args, fds: [devNull, fd, fd], group: -1}); + Sys.close(fd); + if j.pid < 0 then + Fmt.write(j.why, "can't start {}: {}", [Fmt.str(args[0]), Fmt.str(Sys.errorText(j.pid))]); + finish(j, failed); return false; + end; + if j.phase = waiting then INC(busy); end; + j.phase := phase; + j.deadline := Sys.clock() + timeout; + return true; +end; + +(* Starts building the test j: raz -Werror, the -I directories, -o. *) +proc build(var j: Job); +var args: Names.Vec; i: INT; +begin + Names.init(args, Heap.heap); + defer Names.done(args); + Names.push(args, raz); + Names.push(args, "-Werror"); + for i := 0 to includes.len - 1 do + Names.push(args, "-I"); + Names.push(args, Names.all(includes)[i]); + end; + Names.push(args, "-o"); + Names.push(args, j.bin); + Names.push(args, j.path); + j.began := Sys.clock(); + _ := start(j, Names.all(args), building); +end; + +(* The test j's phase has ended, how and with value as Sys.wait says: + on to running it, or the end. *) +proc ended(var j: Job; how: Sys.Status; value: INT); +var args: [1][]U8; +begin + if (j.phase = building) and (how = Sys.exited) and (value = 0) then + args[0] := j.bin; + _ := start(j, args, running); + return; + end; + if j.phase = building then + if (how = Sys.exited) and (value = 1) then Buf.str(j.why, "doesn't compile"); + elsif how = Sys.exited then Fmt.write(j.why, "raz status {}", [Fmt.int(value)]); + else Fmt.write(j.why, "raz signal {}", [Fmt.int(value)]); + end; + finish(j, failed); + elsif (how = Sys.exited) and (value = 0) then finish(j, passed); + elsif how = Sys.exited then Fmt.write(j.why, "status {}", [Fmt.int(value)]); finish(j, failed); + else Fmt.write(j.why, "signal {}", [Fmt.int(value)]); finish(j, failed); + end; +end; + +(* Looks at the test j, being built or run, without waiting: whether + it has ended its phase, or run out of time and been killed. *) +proc poll(var j: Job): BOOL; +var pid, value: INT; how: Sys.Status; +begin + pid, how, value := Sys.wait(j.pid, false); + if pid < 0 then + Fmt.write(j.why, "lost: {}", [Fmt.str(Sys.errorText(pid))]); + finish(j, failed); + elsif (pid > 0) and (how # Sys.stopped) then + ended(j, how, value); + elsif Sys.clock() > j.deadline then + _ := Sys.kill(-j.pid, Sys.sigKill); + _, _, _ := Sys.wait(j.pid, true); + finish(j, timedOut); + else + return false; + end; + return true; +end; + +(* Writes word, in colour c on a terminal, padded to a column of 8. *) +proc putWord(word, c: []U8); +var i: INT; +begin + if colour then Out.str(c); Out.str(word); Out.str(Plain); else Out.str(word); end; + for i := LEN(word) to 7 do Out.char(' '); end; +end; + +(* Writes the file at path, each line indented by 4 spaces. *) +proc putIndented(path: []U8); +var b: Buf.Buf; s, line, rest: []U8; more: BOOL; +begin + Buf.init(b, Heap.heap); + defer Buf.done(b); + if not Files.read(path, b) then Fmt.print(" (can't read {})\n", [Fmt.str(path)]); return; end; + s := Buf.bytes(b); + while LEN(s) > 0 do + line, rest, more := Str.cut(s, "\n"); + Fmt.print(" {}\n", [Fmt.str(line)]); + s := rest; + if not more then break; end; + end; +end; + +(* Writes the line of the ended test j, and its log if it failed. *) +proc report(const j: Job); +var secs: Fmt.Arg; +begin + secs := Fmt.real(F64(j.took) / F64(Second), 2); + if j.result = passed then + putWord("ok", Green); + Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]); + elsif j.result = timedOut then + putWord("TIMEOUT", Yellow); + Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]); + putIndented(j.log); + else + putWord("FAIL", Red); + Fmt.print("{} {} {}s\n", [Fmt.str(j.path), Fmt.str(Buf.bytes(j.why)), secs]); + putIndented(j.log); + end; + Out.flush; +end; + +(* Kills every test being built or run, after a signal to stop, and + ends with the shell's status for it, 128 and its number. *) +proc interrupted(sig: INT); +var all: []Job; i: INT; +begin + all := Jobs.all(tests); + for i := 0 to LEN(all) - 1 do + if active(all[i]) then + _ := Sys.kill(-all[i].pid, Sys.sigKill); + _, _, _ := Sys.wait(all[i].pid, true); + end; + end; + Out.flush; + Rt.exit(128 + sig); +end; + +(* Builds and runs every test, jobs at a time, and reports each in + order as soon as it and those before it have ended. *) +proc runAll; +var all: []Job; next, shown, i: INT; changed: BOOL; +begin + all := Jobs.all(tests); + next := 0; shown := 0; + while shown < LEN(all) do + while (busy < jobs) and (next < LEN(all)) do build(all[next]); INC(next); end; + changed := false; + for i := 0 to next - 1 do + if active(all[i]) and poll(all[i]) then changed := true; end; + end; + while (shown < LEN(all)) and (all[shown].phase = done) do report(all[shown]); INC(shown); end; + if not changed and Sys.ready(signals, PollMs) then interrupted(Sys.caught(signals)); end; + end; +end; + +(* The summary line, and status 1 if any test failed. *) +proc summary(took: I64); +var all: []Job; i: INT; count: [3]INT; +begin + all := Jobs.all(tests); + for i := 0 to LEN(all) - 1 do INC(count[INT(all[i].result)]); end; + Fmt.print("{} passed, {} failed, {} timed out, {}s\n", + [Fmt.int(count[INT(passed)]), Fmt.int(count[INT(failed)]), Fmt.int(count[INT(timedOut)]), + Fmt.real(F64(took) / F64(Second), 2)]); + Out.flush; + if count[INT(passed)] < LEN(all) then Rt.exit(1); end; +end; + +proc main*; +var began: I64; +begin + began := Sys.clock(); + readArgs; + setUp; + runAll; + summary(Sys.clock() - began); +end; +end Test.