raz / commit 1172b23d

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

patch · log

10 files changed, +280 -18

doc/raztest.1 (history) modified +47 -1

@@ -12,6 +12,8 @@
1212 .Op Fl t Ar secs
1313 .Op Fl o Ar dir
1414 .Op Fl r Ar text
15+.Op Fl \-tap
16+.Op Fl \-junit Ar file
1517 .Ar file.raz Ns | Ns Ar dir ...
1618 .Sh DESCRIPTION
1719 .Nm
@@ -73,8 +75,29 @@
7375 .Ed
7476 .Pp
7577 The three words are in colour when standard output is a terminal.
78+.Pp
79+With
80+.Fl \-tap
81+standard output is TAP version 14 instead: the lines
82+.Ql TAP version 14
83+and
84+.Ql 1..N ,
85+then for each test
86+.Ql ok K - file
87+or
88+.Ql not ok K - file # status 1
89+(the same reason as the
90+.Ql FAIL
91+line, or
92+.Ql timed out ) ,
93+with a failure's
94+.Pa .out
95+under it as comment lines starting with
96+.Ql # ,
97+and the summary as a last comment line.
98+There is no colour.
7699 .Sh OPTIONS
77-.Bl -tag -width "-t secs"
100+.Bl -tag -width "--junit file"
78101 .It Fl I Ar dir
79102 Look for modules in
80103 .Ar dir
@@ -101,6 +124,24 @@
101124 .It Fl r Ar text
102125 Run only the tests whose name holds
103126 .Ar text .
127+.It Fl \-tap
128+Write TAP instead of the usual lines.
129+.It Fl \-junit Ar file
130+Besides the usual output, write the results to
131+.Ar file
132+as JUnit XML, which CI services read: one
133+.Ql testcase
134+for each test, its
135+.Ql classname
136+the file, with a
137+.Ql failure
138+holding the log for one that failed or timed out.
139+The directory of
140+.Ar file
141+must exist; if it can't be written, the status is 2.
142+Control bytes XML 1.0 forbids in a log are written as
143+.Ql \&? ;
144+invalid UTF-8 is passed through.
104145 .It Fl h , Fl \-help
105146 Say the options, and exit.
106147 .El
@@ -124,6 +165,11 @@
124165 Run a project's tests, four at a time:
125166 .Bd -literal -offset indent
126167 raztest -j 4 -I src test
168+.Ed
169+.Pp
170+In CI, with a report for the service to show:
171+.Bd -literal -offset indent
172+raztest -j 4 -I src --junit build/junit.xml test
127173 .Ed
128174 .Pp
129175 A Makefile's check that uses

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

@@ -285,6 +285,32 @@
285285 echo "FAIL tools/Test.raz: $(cat $out/raztest.cerr)"; fail=$((fail + 1))
286286 fi
287287
288+# raztest's own three reports, over the fixtures in test/raztest (a pass,
289+# a failed check, a build that fails, a test that outlasts -t 1): plain,
290+# --tap and --junit, each against its .expect, every time in them
291+# written T
292+rtreport() { # name, then raztest's arguments after the common ones
293+ name=$1; shift
294+ RAZ=$raz $out/raztest -t 1 -o $out/rt "$@" test/raztest 2>&1 </dev/null |
295+ sed -e 's/[0-9][0-9]*\.[0-9][0-9]s/Ts/' -e 's/time="[0-9.]*"/time="T"/' > $out/rt.$name
296+ if cmp -s $out/rt.$name test/raztest/$name.expect; then pass=$((pass + 1))
297+ else
298+ echo "FAIL raztest $name:"; diff test/raztest/$name.expect $out/rt.$name
299+ fail=$((fail + 1))
300+ fi
301+}
302+if [ -x $out/raztest ]; then
303+ rtreport plain
304+ rtreport tap --tap
305+ RAZ=$raz $out/raztest -t 1 -o $out/rt --junit $out/rt.xml test/raztest >/dev/null 2>&1
306+ sed -e 's/time="[0-9.]*"/time="T"/' $out/rt.xml > $out/rt.junit
307+ if cmp -s $out/rt.junit test/raztest/junit.expect; then pass=$((pass + 1))
308+ else
309+ echo "FAIL raztest junit:"; diff test/raztest/junit.expect $out/rt.junit
310+ fail=$((fail + 1))
311+ fi
312+fi
313+
288314 # CAbi is tools/cabi.c's output, from the C library's own headers:
289315 # compiled with cc, and with musl-gcc, each where it is installed, it
290316 # writes lib/'s file again

test/raztest/Broken.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+(* A fixture for test/check.sh: a test that doesn't compile. *)
2+module Broken;
3+
4+proc main*;
5+begin
6+ nothing := 1;
7+end;
8+end Broken.

test/raztest/Fails.raz (history) added +9 -0

@@ -0,0 +1,9 @@
1+(* A fixture for test/check.sh: a test with one failing check. *)
2+module Fails;
3+import Check;
4+
5+proc main*;
6+begin
7+ Check.int("two and two", 2 + 2, 5);
8+end;
9+end Fails.

test/raztest/Pass.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+(* A fixture for test/check.sh: a test that passes. *)
2+module Pass;
3+
4+proc main*;
5+begin
6+end;
7+end Pass.

test/raztest/Slow.raz (history) added +9 -0

@@ -0,0 +1,9 @@
1+(* A fixture for test/check.sh: a test that outlasts raztest -t 1. *)
2+module Slow;
3+import Sys;
4+
5+proc main*;
6+begin
7+ Sys.sleep(5000);
8+end;
9+end Slow.

test/raztest/junit.expect (history) added +17 -0

@@ -0,0 +1,17 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<testsuites tests="4" failures="3" time="T">
3+<testsuite name="raztest" tests="4" failures="3" time="T">
4+<testcase name="Broken" classname="test/raztest/Broken.raz" time="T"><failure message="doesn't compile">test/raztest/Broken.raz:6:5: error: nothing isn't declared
5+ 6 | nothing := 1;
6+ | ^^^^^^^
7+raz: 1 error
8+</failure></testcase>
9+<testcase name="Fails" classname="test/raztest/Fails.raz" time="T"><failure message="status 1">FAIL two and two
10+ got: 4
11+ want: 5
12+Check: 1 of 1 failed
13+</failure></testcase>
14+<testcase name="Pass" classname="test/raztest/Pass.raz" time="T"/>
15+<testcase name="Slow" classname="test/raztest/Slow.raz" time="T"><failure message="timed out"></failure></testcase>
16+</testsuite>
17+</testsuites>

test/raztest/plain.expect (history) added +13 -0

@@ -0,0 +1,13 @@
1+FAIL test/raztest/Broken.raz doesn't compile Ts
2+ test/raztest/Broken.raz:6:5: error: nothing isn't declared
3+ 6 | nothing := 1;
4+ | ^^^^^^^
5+ raz: 1 error
6+FAIL test/raztest/Fails.raz status 1 Ts
7+ FAIL two and two
8+ got: 4
9+ want: 5
10+ Check: 1 of 1 failed
11+ok test/raztest/Pass.raz Ts
12+TIMEOUT test/raztest/Slow.raz Ts
13+1 passed, 2 failed, 1 timed out, Ts

test/raztest/tap.expect (history) added +15 -0

@@ -0,0 +1,15 @@
1+TAP version 14
2+1..4
3+not ok 1 - test/raztest/Broken.raz # doesn't compile
4+# test/raztest/Broken.raz:6:5: error: nothing isn't declared
5+# 6 | nothing := 1;
6+# | ^^^^^^^
7+# raz: 1 error
8+not ok 2 - test/raztest/Fails.raz # status 1
9+# FAIL two and two
10+# got: 4
11+# want: 5
12+# Check: 1 of 1 failed
13+ok 3 - test/raztest/Pass.raz
14+not ok 4 - test/raztest/Slow.raz # timed out
15+# 1 passed, 2 failed, 1 timed out, Ts

tools/Test.raz (history) modified +129 -17

@@ -1,6 +1,7 @@
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] file.raz|dir ...
3+ raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text]
4+ [--tap] [--junit file] file.raz|dir ...
45
56 A test is a module whose main is its test: it passes if it builds
67 with raz -Werror and then exits with status 0, as a program that
@@ -25,22 +26,34 @@
2526 command line or the setup. The words ok, FAIL and TIMEOUT are in
2627 colour when standard output is a terminal and NO_COLOR is unset.
2728
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. *)
29+ --tap writes TAP version 14 on standard output instead: the plan
30+ 1..N first, then ok or not ok for each test, a failure's log in
31+ comment lines under it, and the summary as a last comment; no
32+ colour. --junit file also writes the results to file as JUnit XML,
33+ as CI services read it; the file's directory must exist. A timeout
34+ counts as a failure there. The log of a failure is written as it
35+ is, so invalid UTF-8 in it passes through into the XML, except that
36+ the control bytes XML 1.0 forbids become ?.
37+
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. *)
3241 module Test;
3342 import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files;
3443 import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less);
3544
3645 const
46+ TapKey = 1; (* the keys of --tap and --junit, which have no letter *)
47+ JunitKey = 2;
3748 Table: []Opt.Row = [
38- Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] file.raz|dir ..."),
49+ Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [--tap] [--junit file] file.raz|dir ..."),
3950 Opt.option("", 'I', "include", "dir", "look for modules in dir too, as raz -I"),
4051 Opt.option("", 'j', "jobs", "n", "run n tests at once (1)"),
4152 Opt.option("", 't', "timeout", "secs", "kill a build or run after secs (60)"),
4253 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")];
54+ Opt.option("", 'r', "run", "text", "run only the tests whose name holds text"),
55+ Opt.option("", TapKey, "tap", "", "write TAP version 14 instead of the usual lines"),
56+ Opt.option("", JunitKey, "junit", "file", "also write the results to file as JUnit XML")];
4457 PollMs = 10; (* how long to wait between looks at the running tests *)
4558 Second = 1000000000; (* a second in Sys.clock's nanoseconds *)
4659 Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *)
@@ -82,6 +95,8 @@
8295 devNull: INT; (* /dev/null, each test's standard input *)
8396 signals: INT; (* the fd the interrupting signals come on *)
8497 colour: BOOL; (* the words of the results are coloured *)
98+ tap: BOOL; (* --tap: report in TAP *)
99+ junit: []U8; (* --junit: the file for the XML, or "" *)
85100
86101 (* Whether a sorts before b, for sorting a directory's files. *)
87102 proc less(a, b: []U8): BOOL;
@@ -188,6 +203,8 @@
188203 elsif p.letter = 't' then timeout := Opt.number(p, 1, 1000000) * Second;
189204 elsif p.letter = 'o' then outDir := p.value;
190205 elsif p.letter = 'r' then only := p.value;
206+ elsif p.letter = TapKey then tap := true;
207+ elsif p.letter = JunitKey then junit := p.value;
191208 end;
192209 end;
193210 arg, ok := Opt.operand(p);
@@ -227,7 +244,7 @@
227244 if devNull < 0 then die("can't open /dev/null: {}", [Fmt.str(Sys.errorText(devNull))]); end;
228245 signals := Sys.catch([Sys.sigInt, Sys.sigTerm, Sys.sigHup]);
229246 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);
247+ colour := Sys.isTerminal(Sys.stdout) and (LEN(Sys.env("NO_COLOR")) = 0) and not tap;
231248 end;
232249
233250 (* Whether the test j is being built or run. *)
@@ -337,24 +354,31 @@
337354 for i := LEN(word) to 7 do Out.char(' '); end;
338355 end;
339356
340-(* Writes the file at path, each line indented by 4 spaces. *)
341-proc putIndented(path: []U8);
357+(* Writes the file at path, each line after the text lead. *)
358+proc putLog(path, lead: []U8);
342359 var b: Buf.Buf; s, line, rest: []U8; more: BOOL;
343360 begin
344361 Buf.init(b, Heap.heap);
345362 defer Buf.done(b);
346- if not Files.read(path, b) then Fmt.print(" (can't read {})\n", [Fmt.str(path)]); return; end;
363+ if not Files.read(path, b) then Fmt.print("{}(can't read {})\n", [Fmt.str(lead), Fmt.str(path)]); return; end;
347364 s := Buf.bytes(b);
348365 while LEN(s) > 0 do
349366 line, rest, more := Str.cut(s, "\n");
350- Fmt.print(" {}\n", [Fmt.str(line)]);
367+ Fmt.print("{}{}\n", [Fmt.str(lead), Fmt.str(line)]);
351368 s := rest;
352369 if not more then break; end;
353370 end;
354371 end;
355372
373+(* Why the test j failed: what its why says, or that it timed out. *)
374+proc reason(const j: Job): []U8;
375+begin
376+ if j.result = timedOut then return "timed out"; end;
377+ return Buf.bytes(j.why);
378+end;
379+
356380 (* Writes the line of the ended test j, and its log if it failed. *)
357-proc report(const j: Job);
381+proc reportLine(const j: Job);
358382 var secs: Fmt.Arg;
359383 begin
360384 secs := Fmt.real(F64(j.took) / F64(Second), 2);
@@ -364,12 +388,30 @@
364388 elsif j.result = timedOut then
365389 putWord("TIMEOUT", Yellow);
366390 Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]);
367- putIndented(j.log);
391+ putLog(j.log, " ");
368392 else
369393 putWord("FAIL", Red);
370394 Fmt.print("{} {} {}s\n", [Fmt.str(j.path), Fmt.str(Buf.bytes(j.why)), secs]);
371- putIndented(j.log);
395+ putLog(j.log, " ");
372396 end;
397+end;
398+
399+(* Writes the TAP lines of the ended test j, the kth: its result, and
400+ its log in comment lines if it failed. *)
401+proc reportTap(const j: Job; k: INT);
402+begin
403+ if j.result = passed then
404+ Fmt.print("ok {} - {}\n", [Fmt.int(k), Fmt.str(j.path)]);
405+ return;
406+ end;
407+ Fmt.print("not ok {} - {} # {}\n", [Fmt.int(k), Fmt.str(j.path), Fmt.str(reason(j))]);
408+ putLog(j.log, "# ");
409+end;
410+
411+(* Writes what ended test j, the kth, in the report asked for. *)
412+proc report(const j: Job; k: INT);
413+begin
414+ if tap then reportTap(j, k); else reportLine(j); end;
373415 Out.flush;
374416 end;
375417
@@ -402,21 +444,90 @@
402444 for i := 0 to next - 1 do
403445 if active(all[i]) and poll(all[i]) then changed := true; end;
404446 end;
405- while (shown < LEN(all)) and (all[shown].phase = done) do report(all[shown]); INC(shown); end;
447+ while (shown < LEN(all)) and (all[shown].phase = done) do report(all[shown], shown + 1); INC(shown); end;
406448 if not changed and Sys.ready(signals, PollMs) then interrupted(Sys.caught(signals)); end;
407449 end;
408450 end;
409451
410-(* The summary line, and status 1 if any test failed. *)
452+(* Writes the plan of a TAP report: how many tests there are. *)
453+proc tapPlan;
454+begin
455+ Fmt.print("TAP version 14\n1..{}\n", [Fmt.int(tests.len)]);
456+ Out.flush;
457+end;
458+
459+(* Adds s to b as XML text or an attribute: & < > and " escaped, and
460+ each control byte but tab, newline and carriage return, which XML 1.0
461+ forbids, written as ?. Bytes from 128 up, valid UTF-8 or not, are
462+ passed through as they are. *)
463+proc xml(var b: Buf.Buf; s: []U8);
464+var i: INT; c: U8;
465+begin
466+ for i := 0 to LEN(s) - 1 do
467+ c := s[i];
468+ if c = '&' then Buf.str(b, "&amp;");
469+ elsif c = '<' then Buf.str(b, "&lt;");
470+ elsif c = '>' then Buf.str(b, "&gt;");
471+ elsif c = '"' then Buf.str(b, "&quot;");
472+ elsif (c < 32) and (c # 9) and (c # 10) and (c # 13) then Buf.add(b, '?');
473+ else Buf.add(b, c);
474+ end;
475+ end;
476+end;
477+
478+(* Adds the XML element of the test j to b: a testcase, with a failure
479+ inside it, holding the log, if it failed. *)
480+proc testCase(var b: Buf.Buf; const j: Job);
481+var log: Buf.Buf;
482+begin
483+ Buf.str(b, "<testcase name=\"");
484+ xml(b, j.name);
485+ Buf.str(b, "\" classname=\"");
486+ xml(b, j.path);
487+ Fmt.write(b, "\" time=\"{}\"", [Fmt.real(F64(j.took) / F64(Second), 2)]);
488+ if j.result = passed then Buf.str(b, "/>\n"); return; end;
489+ Buf.str(b, "><failure message=\"");
490+ xml(b, reason(j));
491+ Buf.str(b, "\">");
492+ Buf.init(log, Heap.heap);
493+ defer Buf.done(log);
494+ if Files.read(j.log, log) then xml(b, Buf.bytes(log)); end;
495+ Buf.str(b, "</failure></testcase>\n");
496+end;
497+
498+(* Writes the results to the --junit file, which took the whole run
499+ took nanoseconds; status 2 if it can't be written. *)
500+proc writeJunit(took: I64);
501+var b: Buf.Buf; all: []Job; i, failures: INT; counts, secs: Fmt.Arg;
502+begin
503+ all := Jobs.all(tests);
504+ for i := 0 to LEN(all) - 1 do
505+ if all[i].result # passed then INC(failures); end;
506+ end;
507+ counts := Fmt.int(LEN(all));
508+ secs := Fmt.real(F64(took) / F64(Second), 2);
509+ Buf.init(b, Heap.heap);
510+ defer Buf.done(b);
511+ Buf.str(b, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
512+ Fmt.write(b, "<testsuites tests=\"{}\" failures=\"{}\" time=\"{}\">\n", [counts, Fmt.int(failures), secs]);
513+ Fmt.write(b, "<testsuite name=\"raztest\" tests=\"{}\" failures=\"{}\" time=\"{}\">\n", [counts, Fmt.int(failures), secs]);
514+ for i := 0 to LEN(all) - 1 do testCase(b, all[i]); end;
515+ Buf.str(b, "</testsuite>\n</testsuites>\n");
516+ if not Files.write(junit, Buf.bytes(b)) then die("can't write {}", [Fmt.str(junit)]); end;
517+end;
518+
519+(* The summary line, the --junit file, and status 1 if any test failed. *)
411520 proc summary(took: I64);
412521 var all: []Job; i: INT; count: [3]INT;
413522 begin
414523 all := Jobs.all(tests);
415524 for i := 0 to LEN(all) - 1 do INC(count[INT(all[i].result)]); end;
525+ if tap then Out.str("# "); end;
416526 Fmt.print("{} passed, {} failed, {} timed out, {}s\n",
417527 [Fmt.int(count[INT(passed)]), Fmt.int(count[INT(failed)]), Fmt.int(count[INT(timedOut)]),
418528 Fmt.real(F64(took) / F64(Second), 2)]);
419529 Out.flush;
530+ if LEN(junit) > 0 then writeJunit(took); end;
420531 if count[INT(passed)] < LEN(all) then Rt.exit(1); end;
421532 end;
422533
@@ -426,6 +537,7 @@
426537 began := Sys.clock();
427538 readArgs;
428539 setUp;
540+ if tap then tapPlan; end;
429541 runAll;
430542 summary(Sys.clock() - began);
431543 end;