| | @@ -1,6 +1,7 @@ |
| 1 | 1 | (* Test: raztest, which builds and runs the tests of a raz program. |
| 2 | 2 | |
| 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 ... |
| 4 | 5 | |
| 5 | 6 | A test is a module whose main is its test: it passes if it builds |
| 6 | 7 | with raz -Werror and then exits with status 0, as a program that |
| | @@ -25,22 +26,34 @@ |
| 25 | 26 | command line or the setup. The words ok, FAIL and TIMEOUT are in |
| 26 | 27 | colour when standard output is a terminal and NO_COLOR is unset. |
| 27 | 28 | |
| 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. *) |
| 32 | 41 | module Test; |
| 33 | 42 | import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files; |
| 34 | 43 | import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less); |
| 35 | 44 | |
| 36 | 45 | const |
| 46 | + TapKey = 1; (* the keys of --tap and --junit, which have no letter *) |
| 47 | + JunitKey = 2; |
| 37 | 48 | 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 ..."), |
| 39 | 50 | Opt.option("", 'I', "include", "dir", "look for modules in dir too, as raz -I"), |
| 40 | 51 | Opt.option("", 'j', "jobs", "n", "run n tests at once (1)"), |
| 41 | 52 | Opt.option("", 't', "timeout", "secs", "kill a build or run after secs (60)"), |
| 42 | 53 | 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")]; |
| 44 | 57 | PollMs = 10; (* how long to wait between looks at the running tests *) |
| 45 | 58 | Second = 1000000000; (* a second in Sys.clock's nanoseconds *) |
| 46 | 59 | Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *) |
| | @@ -82,6 +95,8 @@ |
| 82 | 95 | devNull: INT; (* /dev/null, each test's standard input *) |
| 83 | 96 | signals: INT; (* the fd the interrupting signals come on *) |
| 84 | 97 | 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 "" *) |
| 85 | 100 | |
| 86 | 101 | (* Whether a sorts before b, for sorting a directory's files. *) |
| 87 | 102 | proc less(a, b: []U8): BOOL; |
| | @@ -188,6 +203,8 @@ |
| 188 | 203 | elsif p.letter = 't' then timeout := Opt.number(p, 1, 1000000) * Second; |
| 189 | 204 | elsif p.letter = 'o' then outDir := p.value; |
| 190 | 205 | 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; |
| 191 | 208 | end; |
| 192 | 209 | end; |
| 193 | 210 | arg, ok := Opt.operand(p); |
| | @@ -227,7 +244,7 @@ |
| 227 | 244 | if devNull < 0 then die("can't open /dev/null: {}", [Fmt.str(Sys.errorText(devNull))]); end; |
| 228 | 245 | signals := Sys.catch([Sys.sigInt, Sys.sigTerm, Sys.sigHup]); |
| 229 | 246 | 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; |
| 231 | 248 | end; |
| 232 | 249 | |
| 233 | 250 | (* Whether the test j is being built or run. *) |
| | @@ -337,24 +354,31 @@ |
| 337 | 354 | for i := LEN(word) to 7 do Out.char(' '); end; |
| 338 | 355 | end; |
| 339 | 356 | |
| 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); |
| 342 | 359 | var b: Buf.Buf; s, line, rest: []U8; more: BOOL; |
| 343 | 360 | begin |
| 344 | 361 | Buf.init(b, Heap.heap); |
| 345 | 362 | 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; |
| 347 | 364 | s := Buf.bytes(b); |
| 348 | 365 | while LEN(s) > 0 do |
| 349 | 366 | line, rest, more := Str.cut(s, "\n"); |
| 350 | | - Fmt.print(" {}\n", [Fmt.str(line)]); |
| 367 | + Fmt.print("{}{}\n", [Fmt.str(lead), Fmt.str(line)]); |
| 351 | 368 | s := rest; |
| 352 | 369 | if not more then break; end; |
| 353 | 370 | end; |
| 354 | 371 | end; |
| 355 | 372 | |
| 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 | + |
| 356 | 380 | (* 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); |
| 358 | 382 | var secs: Fmt.Arg; |
| 359 | 383 | begin |
| 360 | 384 | secs := Fmt.real(F64(j.took) / F64(Second), 2); |
| | @@ -364,12 +388,30 @@ |
| 364 | 388 | elsif j.result = timedOut then |
| 365 | 389 | putWord("TIMEOUT", Yellow); |
| 366 | 390 | Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]); |
| 367 | | - putIndented(j.log); |
| 391 | + putLog(j.log, " "); |
| 368 | 392 | else |
| 369 | 393 | putWord("FAIL", Red); |
| 370 | 394 | Fmt.print("{} {} {}s\n", [Fmt.str(j.path), Fmt.str(Buf.bytes(j.why)), secs]); |
| 371 | | - putIndented(j.log); |
| 395 | + putLog(j.log, " "); |
| 372 | 396 | 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; |
| 373 | 415 | Out.flush; |
| 374 | 416 | end; |
| 375 | 417 | |
| | @@ -402,21 +444,90 @@ |
| 402 | 444 | for i := 0 to next - 1 do |
| 403 | 445 | if active(all[i]) and poll(all[i]) then changed := true; end; |
| 404 | 446 | 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; |
| 406 | 448 | if not changed and Sys.ready(signals, PollMs) then interrupted(Sys.caught(signals)); end; |
| 407 | 449 | end; |
| 408 | 450 | end; |
| 409 | 451 | |
| 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, "&"); |
| 469 | + elsif c = '<' then Buf.str(b, "<"); |
| 470 | + elsif c = '>' then Buf.str(b, ">"); |
| 471 | + elsif c = '"' then Buf.str(b, """); |
| 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. *) |
| 411 | 520 | proc summary(took: I64); |
| 412 | 521 | var all: []Job; i: INT; count: [3]INT; |
| 413 | 522 | begin |
| 414 | 523 | all := Jobs.all(tests); |
| 415 | 524 | for i := 0 to LEN(all) - 1 do INC(count[INT(all[i].result)]); end; |
| 525 | + if tap then Out.str("# "); end; |
| 416 | 526 | Fmt.print("{} passed, {} failed, {} timed out, {}s\n", |
| 417 | 527 | [Fmt.int(count[INT(passed)]), Fmt.int(count[INT(failed)]), Fmt.int(count[INT(timedOut)]), |
| 418 | 528 | Fmt.real(F64(took) / F64(Second), 2)]); |
| 419 | 529 | Out.flush; |
| 530 | + if LEN(junit) > 0 then writeJunit(took); end; |
| 420 | 531 | if count[INT(passed)] < LEN(all) then Rt.exit(1); end; |
| 421 | 532 | end; |
| 422 | 533 | |
| | @@ -426,6 +537,7 @@ |
| 426 | 537 | began := Sys.clock(); |
| 427 | 538 | readArgs; |
| 428 | 539 | setUp; |
| 540 | + if tap then tapPlan; end; |
| 429 | 541 | runAll; |
| 430 | 542 | summary(Sys.clock() - began); |
| 431 | 543 | end; |