From 1172b23d6f02ed20d4f09bad91c063297f677952 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:10:54 +0100 Subject: [PATCH] raztest --tap and --junit; a test of raztest itself --tap writes TAP version 14 instead of the usual lines, a failure's log as comments under it. --junit file also writes the results as JUnit XML, as CI services read it; a timeout counts as a failure, and the control bytes XML 1.0 forbids become ?. test/raztest holds four fixtures (a pass, a failed check, a build that fails, one that outlasts -t 1); test/check.sh runs raztest over them plain, with --tap and with --junit, each against its .expect, times written T. Step 4 of TESTING-PLAN.md. make check: 1040 passed, 0 failed. --- diff --git a/doc/raztest.1 b/doc/raztest.1 index e254f40..62b6efa 100644 --- a/doc/raztest.1 +++ b/doc/raztest.1 @@ -12,6 +12,8 @@ .Op Fl t Ar secs .Op Fl o Ar dir .Op Fl r Ar text +.Op Fl \-tap +.Op Fl \-junit Ar file .Ar file.raz Ns | Ns Ar dir ... .Sh DESCRIPTION .Nm @@ -73,8 +75,29 @@ .Ed .Pp The three words are in colour when standard output is a terminal. +.Pp +With +.Fl \-tap +standard output is TAP version 14 instead: the lines +.Ql TAP version 14 +and +.Ql 1..N , +then for each test +.Ql ok K - file +or +.Ql not ok K - file # status 1 +(the same reason as the +.Ql FAIL +line, or +.Ql timed out ) , +with a failure's +.Pa .out +under it as comment lines starting with +.Ql # , +and the summary as a last comment line. +There is no colour. .Sh OPTIONS -.Bl -tag -width "-t secs" +.Bl -tag -width "--junit file" .It Fl I Ar dir Look for modules in .Ar dir @@ -101,6 +124,24 @@ .It Fl r Ar text Run only the tests whose name holds .Ar text . +.It Fl \-tap +Write TAP instead of the usual lines. +.It Fl \-junit Ar file +Besides the usual output, write the results to +.Ar file +as JUnit XML, which CI services read: one +.Ql testcase +for each test, its +.Ql classname +the file, with a +.Ql failure +holding the log for one that failed or timed out. +The directory of +.Ar file +must exist; if it can't be written, the status is 2. +Control bytes XML 1.0 forbids in a log are written as +.Ql \&? ; +invalid UTF-8 is passed through. .It Fl h , Fl \-help Say the options, and exit. .El @@ -124,6 +165,11 @@ Run a project's tests, four at a time: .Bd -literal -offset indent raztest -j 4 -I src test +.Ed +.Pp +In CI, with a report for the service to show: +.Bd -literal -offset indent +raztest -j 4 -I src --junit build/junit.xml test .Ed .Pp A Makefile's check that uses diff --git a/test/check.sh b/test/check.sh index 8fdc2ae..89d9b24 100755 --- a/test/check.sh +++ b/test/check.sh @@ -285,6 +285,32 @@ echo "FAIL tools/Test.raz: $(cat $out/raztest.cerr)"; fail=$((fail + 1)) fi +# raztest's own three reports, over the fixtures in test/raztest (a pass, +# a failed check, a build that fails, a test that outlasts -t 1): plain, +# --tap and --junit, each against its .expect, every time in them +# written T +rtreport() { # name, then raztest's arguments after the common ones + name=$1; shift + RAZ=$raz $out/raztest -t 1 -o $out/rt "$@" test/raztest 2>&1 $out/rt.$name + if cmp -s $out/rt.$name test/raztest/$name.expect; then pass=$((pass + 1)) + else + echo "FAIL raztest $name:"; diff test/raztest/$name.expect $out/rt.$name + fail=$((fail + 1)) + fi +} +if [ -x $out/raztest ]; then + rtreport plain + rtreport tap --tap + RAZ=$raz $out/raztest -t 1 -o $out/rt --junit $out/rt.xml test/raztest >/dev/null 2>&1 + sed -e 's/time="[0-9.]*"/time="T"/' $out/rt.xml > $out/rt.junit + if cmp -s $out/rt.junit test/raztest/junit.expect; then pass=$((pass + 1)) + else + echo "FAIL raztest junit:"; diff test/raztest/junit.expect $out/rt.junit + 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/test/raztest/Broken.raz b/test/raztest/Broken.raz new file mode 100644 index 0000000..f3f35cd --- /dev/null +++ b/test/raztest/Broken.raz @@ -0,0 +1,8 @@ +(* A fixture for test/check.sh: a test that doesn't compile. *) +module Broken; + +proc main*; +begin + nothing := 1; +end; +end Broken. diff --git a/test/raztest/Fails.raz b/test/raztest/Fails.raz new file mode 100644 index 0000000..bfa0285 --- /dev/null +++ b/test/raztest/Fails.raz @@ -0,0 +1,9 @@ +(* A fixture for test/check.sh: a test with one failing check. *) +module Fails; +import Check; + +proc main*; +begin + Check.int("two and two", 2 + 2, 5); +end; +end Fails. diff --git a/test/raztest/Pass.raz b/test/raztest/Pass.raz new file mode 100644 index 0000000..a872d9b --- /dev/null +++ b/test/raztest/Pass.raz @@ -0,0 +1,7 @@ +(* A fixture for test/check.sh: a test that passes. *) +module Pass; + +proc main*; +begin +end; +end Pass. diff --git a/test/raztest/Slow.raz b/test/raztest/Slow.raz new file mode 100644 index 0000000..ce574db --- /dev/null +++ b/test/raztest/Slow.raz @@ -0,0 +1,9 @@ +(* A fixture for test/check.sh: a test that outlasts raztest -t 1. *) +module Slow; +import Sys; + +proc main*; +begin + Sys.sleep(5000); +end; +end Slow. diff --git a/test/raztest/junit.expect b/test/raztest/junit.expect new file mode 100644 index 0000000..e2b9eb1 --- /dev/null +++ b/test/raztest/junit.expect @@ -0,0 +1,17 @@ + + + +test/raztest/Broken.raz:6:5: error: nothing isn't declared + 6 | nothing := 1; + | ^^^^^^^ +raz: 1 error + +FAIL two and two + got: 4 + want: 5 +Check: 1 of 1 failed + + + + + diff --git a/test/raztest/plain.expect b/test/raztest/plain.expect new file mode 100644 index 0000000..73ceeed --- /dev/null +++ b/test/raztest/plain.expect @@ -0,0 +1,13 @@ +FAIL test/raztest/Broken.raz doesn't compile Ts + test/raztest/Broken.raz:6:5: error: nothing isn't declared + 6 | nothing := 1; + | ^^^^^^^ + raz: 1 error +FAIL test/raztest/Fails.raz status 1 Ts + FAIL two and two + got: 4 + want: 5 + Check: 1 of 1 failed +ok test/raztest/Pass.raz Ts +TIMEOUT test/raztest/Slow.raz Ts +1 passed, 2 failed, 1 timed out, Ts diff --git a/test/raztest/tap.expect b/test/raztest/tap.expect new file mode 100644 index 0000000..ad933ef --- /dev/null +++ b/test/raztest/tap.expect @@ -0,0 +1,15 @@ +TAP version 14 +1..4 +not ok 1 - test/raztest/Broken.raz # doesn't compile +# test/raztest/Broken.raz:6:5: error: nothing isn't declared +# 6 | nothing := 1; +# | ^^^^^^^ +# raz: 1 error +not ok 2 - test/raztest/Fails.raz # status 1 +# FAIL two and two +# got: 4 +# want: 5 +# Check: 1 of 1 failed +ok 3 - test/raztest/Pass.raz +not ok 4 - test/raztest/Slow.raz # timed out +# 1 passed, 2 failed, 1 timed out, Ts diff --git a/tools/Test.raz b/tools/Test.raz index 8e27ff2..6bf3a77 100644 --- a/tools/Test.raz +++ b/tools/Test.raz @@ -1,6 +1,7 @@ (* 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 ... + raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] + [--tap] [--junit file] 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 @@ -25,22 +26,34 @@ 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. *) + --tap writes TAP version 14 on standard output instead: the plan + 1..N first, then ok or not ok for each test, a failure's log in + comment lines under it, and the summary as a last comment; no + colour. --junit file also writes the results to file as JUnit XML, + as CI services read it; the file's directory must exist. A timeout + counts as a failure there. The log of a failure is written as it + 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. *) module Test; import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files; import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less); const + 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] file.raz|dir ..."), + Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [--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("", 'r', "run", "text", "run only the tests whose name holds text"), + 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 *) Second = 1000000000; (* a second in Sys.clock's nanoseconds *) Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *) @@ -82,6 +95,8 @@ 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 *) + tap: BOOL; (* --tap: report in TAP *) + junit: []U8; (* --junit: the file for the XML, or "" *) (* Whether a sorts before b, for sorting a directory's files. *) proc less(a, b: []U8): BOOL; @@ -188,6 +203,8 @@ 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 = TapKey then tap := true; + elsif p.letter = JunitKey then junit := p.value; end; end; arg, ok := Opt.operand(p); @@ -227,7 +244,7 @@ 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); + colour := Sys.isTerminal(Sys.stdout) and (LEN(Sys.env("NO_COLOR")) = 0) and not tap; end; (* Whether the test j is being built or run. *) @@ -337,24 +354,31 @@ 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); +(* Writes the file at path, each line after the text lead. *) +proc putLog(path, lead: []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; + if not Files.read(path, b) then Fmt.print("{}(can't read {})\n", [Fmt.str(lead), 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)]); + Fmt.print("{}{}\n", [Fmt.str(lead), Fmt.str(line)]); s := rest; if not more then break; end; end; end; +(* Why the test j failed: what its why says, or that it timed out. *) +proc reason(const j: Job): []U8; +begin + if j.result = timedOut then return "timed out"; end; + return Buf.bytes(j.why); +end; + (* Writes the line of the ended test j, and its log if it failed. *) -proc report(const j: Job); +proc reportLine(const j: Job); var secs: Fmt.Arg; begin secs := Fmt.real(F64(j.took) / F64(Second), 2); @@ -364,12 +388,30 @@ elsif j.result = timedOut then putWord("TIMEOUT", Yellow); Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]); - putIndented(j.log); + putLog(j.log, " "); else putWord("FAIL", Red); Fmt.print("{} {} {}s\n", [Fmt.str(j.path), Fmt.str(Buf.bytes(j.why)), secs]); - putIndented(j.log); + putLog(j.log, " "); end; +end; + +(* Writes the TAP lines of the ended test j, the kth: its result, and + its log in comment lines if it failed. *) +proc reportTap(const j: Job; k: INT); +begin + if j.result = passed then + Fmt.print("ok {} - {}\n", [Fmt.int(k), Fmt.str(j.path)]); + return; + end; + Fmt.print("not ok {} - {} # {}\n", [Fmt.int(k), Fmt.str(j.path), Fmt.str(reason(j))]); + putLog(j.log, "# "); +end; + +(* Writes what ended test j, the kth, in the report asked for. *) +proc report(const j: Job; k: INT); +begin + if tap then reportTap(j, k); else reportLine(j); end; Out.flush; end; @@ -402,21 +444,90 @@ 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; + while (shown < LEN(all)) and (all[shown].phase = done) do report(all[shown], shown + 1); 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. *) +(* Writes the plan of a TAP report: how many tests there are. *) +proc tapPlan; +begin + Fmt.print("TAP version 14\n1..{}\n", [Fmt.int(tests.len)]); + Out.flush; +end; + +(* Adds s to b as XML text or an attribute: & < > and " escaped, and + each control byte but tab, newline and carriage return, which XML 1.0 + forbids, written as ?. Bytes from 128 up, valid UTF-8 or not, are + passed through as they are. *) +proc xml(var b: Buf.Buf; s: []U8); +var i: INT; c: U8; +begin + for i := 0 to LEN(s) - 1 do + c := s[i]; + if c = '&' then Buf.str(b, "&"); + elsif c = '<' then Buf.str(b, "<"); + elsif c = '>' then Buf.str(b, ">"); + elsif c = '"' then Buf.str(b, """); + elsif (c < 32) and (c # 9) and (c # 10) and (c # 13) then Buf.add(b, '?'); + else Buf.add(b, c); + end; + end; +end; + +(* Adds the XML element of the test j to b: a testcase, with a failure + inside it, holding the log, if it failed. *) +proc testCase(var b: Buf.Buf; const j: Job); +var log: Buf.Buf; +begin + Buf.str(b, "\n"); return; end; + Buf.str(b, ">"); + Buf.init(log, Heap.heap); + defer Buf.done(log); + if Files.read(j.log, log) then xml(b, Buf.bytes(log)); end; + Buf.str(b, "\n"); +end; + +(* Writes the results to the --junit file, which took the whole run + took nanoseconds; status 2 if it can't be written. *) +proc writeJunit(took: I64); +var b: Buf.Buf; all: []Job; i, failures: INT; counts, secs: Fmt.Arg; +begin + all := Jobs.all(tests); + for i := 0 to LEN(all) - 1 do + if all[i].result # passed then INC(failures); end; + end; + counts := Fmt.int(LEN(all)); + secs := Fmt.real(F64(took) / F64(Second), 2); + Buf.init(b, Heap.heap); + defer Buf.done(b); + Buf.str(b, "\n"); + Fmt.write(b, "\n", [counts, Fmt.int(failures), secs]); + Fmt.write(b, "\n", [counts, Fmt.int(failures), secs]); + for i := 0 to LEN(all) - 1 do testCase(b, all[i]); end; + Buf.str(b, "\n\n"); + 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); 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; + if tap then Out.str("# "); 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 LEN(junit) > 0 then writeJunit(took); end; if count[INT(passed)] < LEN(all) then Rt.exit(1); end; end; @@ -426,6 +537,7 @@ began := Sys.clock(); readArgs; setUp; + if tap then tapPlan; end; runAll; summary(Sys.clock() - began); end;