| | @@ -1,6 +1,6 @@ |
| 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] |
| 3 | + raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [-w] |
| 4 | 4 | [--tap] [--junit file] file.raz|dir ... |
| 5 | 5 | |
| 6 | 6 | A test is a module whose main is its test: it passes if it builds |
| | @@ -35,9 +35,20 @@ |
| 35 | 35 | is, so invalid UTF-8 in it passes through into the XML, except that |
| 36 | 36 | the control bytes XML 1.0 forbids become ?. |
| 37 | 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. *) |
| 38 | + -w runs the tests, then runs them all again whenever a *.raz file |
| 39 | + changes, until a signal stops it. Watched are the *.raz directly in |
| 40 | + the directory of each test file, of each directory given and of each |
| 41 | + -I directory. Sys has no inotify, so the files' times and sizes are |
| 42 | + looked at every 300 ms; a change is waited out until the files stay |
| 43 | + as they are for one look, since an editor writes in steps. All the |
| 44 | + tests run again, not those a change reaches: raz builds fast, and |
| 45 | + tracking imports isn't worth its lines. The operands are read again |
| 46 | + each time, so a new test file is found. On a terminal the screen is |
| 47 | + cleared before each run. |
| 48 | + |
| 49 | + Needs only the library. Leaves out: arguments to a test, tests not |
| 50 | + in raz, and the number of processors as -j's default, which Sys |
| 51 | + can't tell: it is 1. *) |
| 41 | 52 | module Test; |
| 42 | 53 | import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files; |
| 43 | 54 | import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less); |
| | @@ -46,15 +57,19 @@ |
| 46 | 57 | TapKey = 1; (* the keys of --tap and --junit, which have no letter *) |
| 47 | 58 | JunitKey = 2; |
| 48 | 59 | Table: []Opt.Row = [ |
| 49 | | - Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [--tap] [--junit file] file.raz|dir ..."), |
| 60 | + Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [-w] [--tap] [--junit file] file.raz|dir ..."), |
| 50 | 61 | Opt.option("", 'I', "include", "dir", "look for modules in dir too, as raz -I"), |
| 51 | 62 | Opt.option("", 'j', "jobs", "n", "run n tests at once (1)"), |
| 52 | 63 | Opt.option("", 't', "timeout", "secs", "kill a build or run after secs (60)"), |
| 53 | 64 | Opt.option("", 'o', "out", "dir", "put the programs and their .out in dir (build/raztest)"), |
| 54 | 65 | Opt.option("", 'r', "run", "text", "run only the tests whose name holds text"), |
| 66 | + Opt.option("", 'w', "watch", "", "run again whenever a .raz file changes"), |
| 55 | 67 | Opt.option("", TapKey, "tap", "", "write TAP version 14 instead of the usual lines"), |
| 56 | 68 | Opt.option("", JunitKey, "junit", "file", "also write the results to file as JUnit XML")]; |
| 57 | 69 | PollMs = 10; (* how long to wait between looks at the running tests *) |
| 70 | + WatchMs = 300; (* how long to wait between looks at the watched files *) |
| 71 | + Clear = "\x1b[H\x1b[2J"; (* home the cursor and clear the screen *) |
| 72 | + Modulus = 1000000007; (* keeps a fingerprint's sums small *) |
| 58 | 73 | Second = 1000000000; (* a second in Sys.clock's nanoseconds *) |
| 59 | 74 | Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *) |
| 60 | 75 | Red = "\x1b[31m"; |
| | @@ -86,6 +101,7 @@ |
| 86 | 101 | var |
| 87 | 102 | raz: []U8; (* the compiler: $RAZ, or raz on PATH *) |
| 88 | 103 | includes: Names.Vec; (* the -I directories *) |
| 104 | + operands: Names.Vec; (* the files and directories given *) |
| 89 | 105 | jobs: INT; (* -j: tests at once *) |
| 90 | 106 | timeout: I64; (* -t, in nanoseconds *) |
| 91 | 107 | outDir: []U8; (* -o *) |
| | @@ -97,6 +113,8 @@ |
| 97 | 113 | colour: BOOL; (* the words of the results are coloured *) |
| 98 | 114 | tap: BOOL; (* --tap: report in TAP *) |
| 99 | 115 | junit: []U8; (* --junit: the file for the XML, or "" *) |
| 116 | + watch: BOOL; (* -w: run again when a file changes *) |
| 117 | + clearing: BOOL; (* the screen is cleared before each run of -w *) |
| 100 | 118 | |
| 101 | 119 | (* Whether a sorts before b, for sorting a directory's files. *) |
| 102 | 120 | proc less(a, b: []U8): BOOL; |
| | @@ -195,7 +213,7 @@ |
| 195 | 213 | begin |
| 196 | 214 | jobs := 1; timeout := 60 * Second; outDir := "build/raztest"; |
| 197 | 215 | Names.init(includes, Heap.heap); |
| 198 | | - Jobs.init(tests, Heap.heap); |
| 216 | + Names.init(operands, Heap.heap); |
| 199 | 217 | Opt.init(p, "raztest", Table); |
| 200 | 218 | while Opt.next(p) do |
| 201 | 219 | if p.letter = 'I' then Names.push(includes, p.value); |
| | @@ -203,6 +221,7 @@ |
| 203 | 221 | elsif p.letter = 't' then timeout := Opt.number(p, 1, 1000000) * Second; |
| 204 | 222 | elsif p.letter = 'o' then outDir := p.value; |
| 205 | 223 | elsif p.letter = 'r' then only := p.value; |
| 224 | + elsif p.letter = 'w' then watch := true; |
| 206 | 225 | elsif p.letter = TapKey then tap := true; |
| 207 | 226 | elsif p.letter = JunitKey then junit := p.value; |
| 208 | 227 | end; |
| | @@ -210,9 +229,20 @@ |
| 210 | 229 | arg, ok := Opt.operand(p); |
| 211 | 230 | if not ok then Opt.fail(p, "no tests named; raztest --help says how"); end; |
| 212 | 231 | repeat |
| 213 | | - addOperand(arg); |
| 232 | + Names.push(operands, arg); |
| 214 | 233 | arg, ok := Opt.operand(p); |
| 215 | 234 | until not ok; |
| 235 | +end; |
| 236 | + |
| 237 | +(* Makes the list of tests, from the operands as they are now. *) |
| 238 | +proc loadTests; |
| 239 | +var i: INT; |
| 240 | +begin |
| 241 | + for i := 0 to tests.len - 1 do Buf.done(Jobs.all(tests)[i].why); end; |
| 242 | + Jobs.done(tests); |
| 243 | + Jobs.init(tests, Heap.heap); |
| 244 | + busy := 0; |
| 245 | + for i := 0 to operands.len - 1 do addOperand(Names.all(operands)[i]); end; |
| 216 | 246 | checkNames; |
| 217 | 247 | end; |
| 218 | 248 | |
| | @@ -245,6 +275,7 @@ |
| 245 | 275 | signals := Sys.catch([Sys.sigInt, Sys.sigTerm, Sys.sigHup]); |
| 246 | 276 | if signals < 0 then die("can't catch signals: {}", [Fmt.str(Sys.errorText(signals))]); end; |
| 247 | 277 | colour := Sys.isTerminal(Sys.stdout) and (LEN(Sys.env("NO_COLOR")) = 0) and not tap; |
| 278 | + clearing := Sys.isTerminal(Sys.stdout) and not tap; |
| 248 | 279 | end; |
| 249 | 280 | |
| 250 | 281 | (* Whether the test j is being built or run. *) |
| | @@ -516,8 +547,8 @@ |
| 516 | 547 | if not Files.write(junit, Buf.bytes(b)) then die("can't write {}", [Fmt.str(junit)]); end; |
| 517 | 548 | end; |
| 518 | 549 | |
| 519 | | -(* The summary line, the --junit file, and status 1 if any test failed. *) |
| 520 | | -proc summary(took: I64); |
| 550 | +(* The summary line and the --junit file; whether all the tests passed. *) |
| 551 | +proc summary(took: I64): BOOL; |
| 521 | 552 | var all: []Job; i: INT; count: [3]INT; |
| 522 | 553 | begin |
| 523 | 554 | all := Jobs.all(tests); |
| | @@ -528,17 +559,130 @@ |
| 528 | 559 | Fmt.real(F64(took) / F64(Second), 2)]); |
| 529 | 560 | Out.flush; |
| 530 | 561 | if LEN(junit) > 0 then writeJunit(took); end; |
| 531 | | - if count[INT(passed)] < LEN(all) then Rt.exit(1); end; |
| 562 | + return count[INT(passed)] = LEN(all); |
| 532 | 563 | end; |
| 533 | 564 | |
| 534 | | -proc main*; |
| 565 | +(* Loads, runs and reports the tests once; whether all passed. *) |
| 566 | +proc cycle(): BOOL; |
| 535 | 567 | var began: I64; |
| 536 | 568 | begin |
| 537 | 569 | began := Sys.clock(); |
| 538 | | - readArgs; |
| 539 | | - setUp; |
| 570 | + loadTests; |
| 540 | 571 | if tap then tapPlan; end; |
| 541 | 572 | runAll; |
| 542 | | - summary(Sys.clock() - began); |
| 573 | + return summary(Sys.clock() - began); |
| 574 | +end; |
| 575 | + |
| 576 | +(* The directory of the file path. *) |
| 577 | +proc dirOf(path: []U8): []U8; |
| 578 | +var i: INT; |
| 579 | +begin |
| 580 | + i := Str.findLast(path, "/"); |
| 581 | + if i < 0 then return "."; end; |
| 582 | + if i = 0 then return "/"; end; |
| 583 | + return path[0..i]; |
| 584 | +end; |
| 585 | + |
| 586 | +(* Adds dir to the directories to watch, unless it is there. *) |
| 587 | +proc watchDir(var dirs: Names.Vec; dir: []U8); |
| 588 | +var i: INT; |
| 589 | +begin |
| 590 | + for i := 0 to dirs.len - 1 do |
| 591 | + if Names.all(dirs)[i] = dir then return; end; |
| 592 | + end; |
| 593 | + Names.push(dirs, dir); |
| 594 | +end; |
| 595 | + |
| 596 | +(* The directories to watch: those given, those of the test files given, |
| 597 | + and the -I ones. *) |
| 598 | +proc watchDirs(var dirs: Names.Vec); |
| 599 | +var i, err: INT; f: Sys.Info; path: []U8; |
| 600 | +begin |
| 601 | + Names.init(dirs, Heap.heap); |
| 602 | + for i := 0 to operands.len - 1 do |
| 603 | + path := Names.all(operands)[i]; |
| 604 | + f, err := Sys.info(path); |
| 605 | + if (err = 0) and f.dir then watchDir(dirs, path); else watchDir(dirs, dirOf(path)); end; |
| 606 | + end; |
| 607 | + for i := 0 to includes.len - 1 do watchDir(dirs, Names.all(includes)[i]); end; |
| 608 | +end; |
| 609 | + |
| 610 | +(* Adds to the fingerprint and the count the *.raz files of dir. It |
| 611 | + runs every few hundred milliseconds for as long as -w does, so the |
| 612 | + paths are made in one buffer, used again. *) |
| 613 | +proc lookIn(dir: []U8; var print: I64; var count: INT); |
| 614 | +var d: Sys.Dir; name: []U8; ok: BOOL; f: Sys.Info; err: INT; path: Buf.Buf; |
| 615 | +begin |
| 616 | + if Sys.openDir(d, dir) < 0 then return; end; |
| 617 | + Buf.init(path, Heap.heap); |
| 618 | + defer Buf.done(path); |
| 619 | + name, ok := Sys.nextEntry(d); |
| 620 | + while ok do |
| 621 | + Buf.reset(path); |
| 622 | + Buf.str(path, dir); Buf.str(path, "/"); Buf.str(path, name); |
| 623 | + f, err := Sys.info(Buf.bytes(path)); |
| 624 | + if Str.hasSuffix(name, ".raz") and (err = 0) then |
| 625 | + INC(count); |
| 626 | + print := (print + 1 + f.mtime mod Modulus + f.size mod Modulus) mod Modulus; |
| 627 | + end; |
| 628 | + name, ok := Sys.nextEntry(d); |
| 629 | + end; |
| 630 | + Sys.closeDir(d); |
| 631 | +end; |
| 632 | + |
| 633 | +(* A number that changes when a *.raz file in the directories is edited, |
| 634 | + added or removed, and how many files there are. *) |
| 635 | +proc fingerprint(): (I64, INT); |
| 636 | +var dirs: Names.Vec; i, count: INT; print: I64; |
| 637 | +begin |
| 638 | + watchDirs(dirs); |
| 639 | + defer Names.done(dirs); |
| 640 | + for i := 0 to dirs.len - 1 do lookIn(Names.all(dirs)[i], print, count); end; |
| 641 | + return print, count; |
| 642 | +end; |
| 643 | + |
| 644 | +(* Waits one look's time; a signal that stops raztest ends it at once. *) |
| 645 | +proc nap; |
| 646 | +begin |
| 647 | + if Sys.ready(signals, WatchMs) then interrupted(Sys.caught(signals)); end; |
| 648 | +end; |
| 649 | + |
| 650 | +(* Waits until the fingerprint is not seen, and then does not change |
| 651 | + for one look. *) |
| 652 | +proc waitChange(seen: I64); |
| 653 | +var print: I64; |
| 654 | +begin |
| 655 | + repeat |
| 656 | + nap; |
| 657 | + print, _ := fingerprint(); |
| 658 | + until print # seen; |
| 659 | + repeat |
| 660 | + seen := print; |
| 661 | + nap; |
| 662 | + print, _ := fingerprint(); |
| 663 | + until print = seen; |
| 664 | +end; |
| 665 | + |
| 666 | +(* Runs the tests, and again at each change, for ever. *) |
| 667 | +proc watchAll; |
| 668 | +var seen: I64; n: INT; |
| 669 | +begin |
| 670 | + while true do |
| 671 | + if clearing then Out.str(Clear); end; |
| 672 | + seen, n := fingerprint(); |
| 673 | + _ := cycle(); |
| 674 | + if tap then Out.str("# "); end; |
| 675 | + Fmt.print("watching {} files; ^C stops\n", [Fmt.int(n)]); |
| 676 | + Out.flush; |
| 677 | + waitChange(seen); |
| 678 | + end; |
| 679 | +end; |
| 680 | + |
| 681 | +proc main*; |
| 682 | +begin |
| 683 | + readArgs; |
| 684 | + setUp; |
| 685 | + if watch then watchAll; end; |
| 686 | + if not cycle() then Rt.exit(1); end; |
| 543 | 687 | end; |
| 544 | 688 | end Test. |