| 1 | (* Test: raztest, which builds and runs the tests of a raz program. |
| 2 | |
| 3 | raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [-w] |
| 4 | [--tap] [--junit file] [--seed n] [--fuzz secs] |
| 5 | file.raz|dir ... |
| 6 | |
| 7 | A test is a module whose main is its test: it passes if it builds |
| 8 | with raz -Werror and then exits with status 0, as a program that |
| 9 | checks with Check does when no check fails. The tests are the files |
| 10 | given; a directory gives each *.raz directly in it that has a line |
| 11 | starting proc main*. A test's name is its file's, without .raz. |
| 12 | |
| 13 | Each is built into the -o directory, as Name, by $RAZ (raz on PATH if |
| 14 | unset), and run there with no input, in a process group of its own; |
| 15 | what the compiler, then the test, write goes to Name.out beside it, |
| 16 | a file, so that nothing need be read while they run and the logs stay |
| 17 | for CI. -j tests go at once, polled every few milliseconds, with no |
| 18 | threads; one that takes longer than -t seconds to build, or to run, |
| 19 | has its group killed. |
| 20 | |
| 21 | A line for each test as it ends, in the order given, with the |
| 22 | .out of one that failed or timed out under it; then a summary, |
| 23 | |
| 24 | 12 passed, 1 failed, 0 timed out, 3.41s |
| 25 | |
| 26 | and status 0 if all passed, 1 if not, 2 for a mistake in the |
| 27 | command line or the setup. The words ok, FAIL and TIMEOUT are in |
| 28 | colour when standard output is a terminal and NO_COLOR is unset. |
| 29 | |
| 30 | --tap writes TAP version 14 on standard output instead: the plan |
| 31 | 1..N first, then ok or not ok for each test, a failure's log in |
| 32 | comment lines under it, and the summary as a last comment; no |
| 33 | colour. --junit file also writes the results to file as JUnit XML, |
| 34 | as CI services read it; the file's directory must exist. A timeout |
| 35 | counts as a failure there. The log of a failure is written as it |
| 36 | is, so invalid UTF-8 in it passes through into the XML, except that |
| 37 | the control bytes XML 1.0 forbids become ?. |
| 38 | |
| 39 | -w runs the tests, then runs them all again whenever a *.raz file |
| 40 | changes, until a signal stops it. Watched are the *.raz directly in |
| 41 | the directory of each test file, of each directory given and of each |
| 42 | -I directory. Sys has no inotify, so the files' times and sizes are |
| 43 | looked at every 300 ms; a change is waited out until the files stay |
| 44 | as they are for one look, since an editor writes in steps. All the |
| 45 | tests run again, not those a change reaches: raz builds fast, and |
| 46 | tracking imports isn't worth its lines. The operands are read again |
| 47 | each time, so a new test file is found. On a terminal the screen is |
| 48 | cleared before each run. |
| 49 | |
| 50 | --seed n and --fuzz secs are for tests that use Prop: they set |
| 51 | PROP_SEED, the seed of the fresh cases, and PROP_TIME, how long each |
| 52 | property is fuzzed, in the tests' environment. --fuzz raises -t to |
| 53 | secs + 60, unless -t is given. |
| 54 | |
| 55 | Needs only the library. Leaves out: arguments to a test, tests not |
| 56 | in raz, and the number of processors as -j's default, which Sys |
| 57 | can't tell: it is 1. *) |
| 58 | module Test; |
| 59 | import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files; |
| 60 | import Names := Vec([]const U8), Jobs := Vec(Job), ByName := Sort([]const U8, less); |
| 61 | |
| 62 | const |
| 63 | TapKey = 1; (* the keys of --tap and --junit, which have no letter *) |
| 64 | JunitKey = 2; |
| 65 | SeedKey = 3; (* and of --seed and --fuzz *) |
| 66 | FuzzKey = 4; |
| 67 | Table: []Opt.Row = [ |
| 68 | Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] [-w] [--tap] [--junit file] [--seed n] [--fuzz secs] file.raz|dir ..."), |
| 69 | Opt.option("", 'I', "include", "dir", "look for modules in dir too, as raz -I"), |
| 70 | Opt.option("", 'j', "jobs", "n", "run n tests at once (1)"), |
| 71 | Opt.option("", 't', "timeout", "secs", "kill a build or run after secs (60)"), |
| 72 | Opt.option("", 'o', "out", "dir", "put the programs and their .out in dir (build/raztest)"), |
| 73 | Opt.option("", 'r', "run", "text", "run only the tests whose name holds text"), |
| 74 | Opt.option("", 'w', "watch", "", "run again whenever a .raz file changes"), |
| 75 | Opt.option("", TapKey, "tap", "", "write TAP version 14 instead of the usual lines"), |
| 76 | Opt.option("", JunitKey, "junit", "file", "also write the results to file as JUnit XML"), |
| 77 | Opt.option("", SeedKey, "seed", "n", "give Prop's fresh cases the seed n"), |
| 78 | Opt.option("", FuzzKey, "fuzz", "secs", "fuzz each Prop property for secs")]; |
| 79 | PollMs = 10; (* how long to wait between looks at the running tests *) |
| 80 | WatchMs = 300; (* how long to wait between looks at the watched files *) |
| 81 | Clear = "\x1b[H\x1b[2J"; (* home the cursor and clear the screen *) |
| 82 | Modulus = 1000000007; (* keeps a fingerprint's sums small *) |
| 83 | Second = 1000000000; (* a second in Sys.clock's nanoseconds *) |
| 84 | Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *) |
| 85 | Red = "\x1b[31m"; |
| 86 | Yellow = "\x1b[33m"; |
| 87 | Plain = "\x1b[0m"; (* back to no colour *) |
| 88 | |
| 89 | type |
| 90 | (* Where a test is: not started, being built, being run, or ended. *) |
| 91 | Phase = (waiting, building, running, done); |
| 92 | |
| 93 | (* How a test ended. *) |
| 94 | Result = (passed, failed, timedOut); |
| 95 | |
| 96 | (* A test and how it is going. *) |
| 97 | Job = record |
| 98 | path: []const U8; (* its file, as given *) |
| 99 | name: []const U8; (* the file's name without .raz *) |
| 100 | bin: []const U8; (* its program, in the out directory *) |
| 101 | log: []const U8; (* its .out, beside the program *) |
| 102 | phase: Phase; |
| 103 | pid: INT; (* the compiler's or the test's, while building or running *) |
| 104 | began: I64; (* Sys.clock when its build started *) |
| 105 | deadline: I64; (* Sys.clock when the phase under way is killed *) |
| 106 | took: I64; (* nanoseconds from its build's start to its end *) |
| 107 | result: Result; |
| 108 | why: Buf.Buf; (* for a failure, what failed: "status 1" *) |
| 109 | end; |
| 110 | |
| 111 | var |
| 112 | raz: []const U8; (* the compiler: $RAZ, or raz on PATH *) |
| 113 | includes: Names.Vec; (* the -I directories *) |
| 114 | operands: Names.Vec; (* the files and directories given *) |
| 115 | jobs: INT; (* -j: tests at once *) |
| 116 | timeout: I64; (* -t, in nanoseconds *) |
| 117 | outDir: []const U8; (* -o *) |
| 118 | only: []const U8; (* -r: the text a name must hold, or "" *) |
| 119 | tests: Jobs.Vec; (* in the order they are reported *) |
| 120 | busy: INT; (* tests being built or run *) |
| 121 | devNull: INT; (* /dev/null, each test's standard input *) |
| 122 | signals: INT; (* the fd the interrupting signals come on *) |
| 123 | colour: BOOL; (* the words of the results are coloured *) |
| 124 | tap: BOOL; (* --tap: report in TAP *) |
| 125 | junit: []const U8; (* --junit: the file for the XML, or "" *) |
| 126 | watch: BOOL; (* -w: run again when a file changes *) |
| 127 | clearing: BOOL; (* the screen is cleared before each run of -w *) |
| 128 | |
| 129 | (* Whether a sorts before b, for sorting a directory's files. *) |
| 130 | proc less(a, b: []const U8): BOOL; |
| 131 | begin |
| 132 | return Str.cmp(a, b) < 0; |
| 133 | end; |
| 134 | |
| 135 | (* A mistake in the setup: the message, and status 2. *) |
| 136 | proc die(f: []const U8; args: []Fmt.Arg); |
| 137 | var b: Buf.Buf; |
| 138 | begin |
| 139 | Buf.init(b, Heap.heap); |
| 140 | Fmt.write(b, "raztest: ", []); |
| 141 | Fmt.write(b, f, args); |
| 142 | Fmt.eprint("{}\n", [Fmt.str(Buf.bytes(b))]); |
| 143 | Rt.exit(2); |
| 144 | end; |
| 145 | |
| 146 | (* a, b and c joined, in memory of its own that is never freed. *) |
| 147 | proc join(a, b, c: []const U8): []const U8; |
| 148 | var s: Buf.Buf; |
| 149 | begin |
| 150 | Buf.init(s, Heap.heap); |
| 151 | Buf.str(s, a); Buf.str(s, b); Buf.str(s, c); |
| 152 | return Buf.bytes(s); |
| 153 | end; |
| 154 | |
| 155 | (* Whether the source at path has a line that starts proc main*. *) |
| 156 | proc hasMain(path: []const U8): BOOL; |
| 157 | var b: Buf.Buf; src: []const U8; |
| 158 | begin |
| 159 | Buf.init(b, Heap.heap); |
| 160 | defer Buf.done(b); |
| 161 | if not Files.read(path, b) then return false; end; |
| 162 | src := Buf.bytes(b); |
| 163 | return Str.hasPrefix(src, "proc main*") or (Str.find(src, "\nproc main*") >= 0); |
| 164 | end; |
| 165 | |
| 166 | (* Adds the test at path, unless -r leaves it out. *) |
| 167 | proc add(path: []const U8); |
| 168 | var j: ^Job; name: []const U8; |
| 169 | begin |
| 170 | name := path[Str.findLast(path, "/") + 1..LEN(path)]; |
| 171 | if Str.hasSuffix(name, ".raz") then name := name[0..LEN(name) - 4]; end; |
| 172 | if Str.find(name, only) < 0 then return; end; |
| 173 | j := Jobs.add(tests); |
| 174 | j^ := {path: path, name: name, bin: join(outDir, "/", name), phase: waiting}; |
| 175 | j^.log := join(j^.bin, ".out", ""); |
| 176 | Buf.init(j^.why, Heap.heap); |
| 177 | end; |
| 178 | |
| 179 | (* Adds the tests of the directory dir: its *.raz with a main, by name. *) |
| 180 | proc addDir(dir: []const U8); |
| 181 | var d: Sys.Dir; files: Names.Vec; name, path: []const U8; ok: BOOL; err, i: INT; |
| 182 | begin |
| 183 | err := Sys.openDir(d, dir); |
| 184 | if err < 0 then die("can't read {}: {}", [Fmt.str(dir), Fmt.str(Sys.errorText(err))]); end; |
| 185 | Names.init(files, Heap.heap); |
| 186 | name, ok := Sys.nextEntry(d); |
| 187 | while ok do |
| 188 | path := join(dir, "/", name); |
| 189 | if Str.hasSuffix(name, ".raz") and hasMain(path) then Names.push(files, path); end; |
| 190 | name, ok := Sys.nextEntry(d); |
| 191 | end; |
| 192 | Sys.closeDir(d); |
| 193 | ByName.sort(Names.all(files)); |
| 194 | for i := 0 to files.len - 1 do add(Names.all(files)[i]); end; |
| 195 | end; |
| 196 | |
| 197 | (* Adds what an operand names: a directory's tests, or a file. *) |
| 198 | proc addOperand(path: []const U8); |
| 199 | var f: Sys.Info; err: INT; |
| 200 | begin |
| 201 | f, err := Sys.info(path); |
| 202 | if err < 0 then die("{}: {}", [Fmt.str(path), Fmt.str(Sys.errorText(err))]); end; |
| 203 | if f.dir then addDir(path); else add(path); end; |
| 204 | end; |
| 205 | |
| 206 | (* Stops if two tests have one name, as their programs would. *) |
| 207 | proc checkNames; |
| 208 | var all: []Job; i, k: INT; |
| 209 | begin |
| 210 | all := Jobs.all(tests); |
| 211 | for i := 0 to LEN(all) - 1 do |
| 212 | for k := 0 to i - 1 do |
| 213 | if all[i].name = all[k].name then |
| 214 | die("{} and {} are both named {}", [Fmt.str(all[k].path), Fmt.str(all[i].path), Fmt.str(all[i].name)]); |
| 215 | end; |
| 216 | end; |
| 217 | end; |
| 218 | end; |
| 219 | |
| 220 | (* Sets the environment variable name to value, for the tests. *) |
| 221 | proc setProp(name, value: []const U8); |
| 222 | begin |
| 223 | if Sys.setEnv(name, value) # 0 then die("can't set {}", [Fmt.str(name)]); end; |
| 224 | end; |
| 225 | |
| 226 | (* --seed: PROP_SEED, a whole number from 0, as Prop reads it. *) |
| 227 | proc setSeed(const p: Opt.Parser); |
| 228 | var i: INT; |
| 229 | begin |
| 230 | if LEN(p.value) = 0 then Opt.refuse(p, "a whole number"); end; |
| 231 | for i := 0 to LEN(p.value) - 1 do |
| 232 | if not Str.isDigit(p.value[i]) then Opt.refuse(p, "a whole number"); end; |
| 233 | end; |
| 234 | setProp("PROP_SEED", p.value); |
| 235 | end; |
| 236 | |
| 237 | (* Reads the command line into the settings and the list of tests. *) |
| 238 | proc readArgs; |
| 239 | var p: Opt.Parser; arg: []const U8; ok, timed: BOOL; fuzz: I64; |
| 240 | begin |
| 241 | jobs := 1; timeout := 60 * Second; outDir := "build/raztest"; |
| 242 | Names.init(includes, Heap.heap); |
| 243 | Names.init(operands, Heap.heap); |
| 244 | Opt.init(p, "raztest", Table); |
| 245 | while Opt.next(p) do |
| 246 | if p.letter = 'I' then Names.push(includes, p.value); |
| 247 | elsif p.letter = 'j' then jobs := INT(Opt.number(p, 1, 1000)); |
| 248 | elsif p.letter = 't' then timeout := Opt.number(p, 1, 1000000) * Second; |
| 249 | elsif p.letter = 'o' then outDir := p.value; |
| 250 | elsif p.letter = 'r' then only := p.value; |
| 251 | elsif p.letter = 'w' then watch := true; |
| 252 | elsif p.letter = TapKey then tap := true; |
| 253 | elsif p.letter = JunitKey then junit := p.value; |
| 254 | elsif p.letter = SeedKey then setSeed(p); |
| 255 | elsif p.letter = FuzzKey then fuzz := Opt.number(p, 1, 1000000); setProp("PROP_TIME", p.value); |
| 256 | end; |
| 257 | timed := timed or (p.letter = 't'); |
| 258 | end; |
| 259 | if (fuzz > 0) and not timed then timeout := (fuzz + 60) * Second; end; |
| 260 | arg, ok := Opt.operand(p); |
| 261 | if not ok then Opt.fail(p, "no tests named; raztest --help says how"); end; |
| 262 | repeat |
| 263 | Names.push(operands, arg); |
| 264 | arg, ok := Opt.operand(p); |
| 265 | until not ok; |
| 266 | end; |
| 267 | |
| 268 | (* Makes the list of tests, from the operands as they are now. *) |
| 269 | proc loadTests; |
| 270 | var i: INT; |
| 271 | begin |
| 272 | for i := 0 to tests.len - 1 do Buf.done(Jobs.all(tests)[i].why); end; |
| 273 | Jobs.done(tests); |
| 274 | Jobs.init(tests, Heap.heap); |
| 275 | busy := 0; |
| 276 | for i := 0 to operands.len - 1 do addOperand(Names.all(operands)[i]); end; |
| 277 | checkNames; |
| 278 | end; |
| 279 | |
| 280 | (* Makes the directory dir and those it is in, as mkdir -p does. *) |
| 281 | proc makeDirs(dir: []const U8); |
| 282 | var i, err: INT; f: Sys.Info; |
| 283 | begin |
| 284 | for i := 1 to LEN(dir) - 1 do |
| 285 | if dir[i] = '/' then _ := Sys.makeDir(dir[0..i]); end; |
| 286 | end; |
| 287 | err := Sys.makeDir(dir); |
| 288 | if err = Sys.errExists then |
| 289 | f, err := Sys.info(dir); |
| 290 | if (err = 0) and not f.dir then err := Sys.errNotDir; end; |
| 291 | end; |
| 292 | if err < 0 then |
| 293 | die("can't make {}: {}", [Fmt.str(dir), Fmt.str(Sys.errorText(err))]); |
| 294 | end; |
| 295 | end; |
| 296 | |
| 297 | (* Readies what every test needs: the compiler's name, the out |
| 298 | directory, /dev/null, the signals that stop raztest, and colour. *) |
| 299 | proc setUp; |
| 300 | begin |
| 301 | raz := Sys.env("RAZ"); |
| 302 | if LEN(raz) = 0 then raz := "raz"; end; |
| 303 | makeDirs(outDir); |
| 304 | devNull := Sys.openRead("/dev/null"); |
| 305 | if devNull < 0 then die("can't open /dev/null: {}", [Fmt.str(Sys.errorText(devNull))]); end; |
| 306 | signals := Sys.catch([Sys.sigInt, Sys.sigTerm, Sys.sigHup]); |
| 307 | if signals < 0 then die("can't catch signals: {}", [Fmt.str(Sys.errorText(signals))]); end; |
| 308 | colour := Sys.isTerminal(Sys.stdout) and (LEN(Sys.env("NO_COLOR")) = 0) and not tap; |
| 309 | clearing := Sys.isTerminal(Sys.stdout) and not tap; |
| 310 | end; |
| 311 | |
| 312 | (* Whether the test j is being built or run. *) |
| 313 | proc active(const j: Job): BOOL; |
| 314 | begin |
| 315 | return (j.phase = building) or (j.phase = running); |
| 316 | end; |
| 317 | |
| 318 | (* Ends the test j's phase as result: what failed is in j.why. *) |
| 319 | proc finish(var j: Job; result: Result); |
| 320 | begin |
| 321 | if active(j) then DEC(busy); end; |
| 322 | j.phase := done; |
| 323 | j.result := result; |
| 324 | j.took := Sys.clock() - j.began; |
| 325 | end; |
| 326 | |
| 327 | (* Starts args[0] with its output and errors into j's log, as j's next |
| 328 | phase, in a group of its own; false, with j failed, if it can't. *) |
| 329 | proc start(var j: Job; args: [][]const U8; phase: Phase): BOOL; |
| 330 | var fd: INT; |
| 331 | begin |
| 332 | fd := Sys.create(j.log); |
| 333 | if fd < 0 then |
| 334 | Fmt.write(j.why, "can't write {}: {}", [Fmt.str(j.log), Fmt.str(Sys.errorText(fd))]); |
| 335 | finish(j, failed); return false; |
| 336 | end; |
| 337 | j.pid := Sys.spawn({path: args[0], args: args, fds: [devNull, fd, fd], group: -1}); |
| 338 | Sys.close(fd); |
| 339 | if j.pid < 0 then |
| 340 | Fmt.write(j.why, "can't start {}: {}", [Fmt.str(args[0]), Fmt.str(Sys.errorText(j.pid))]); |
| 341 | finish(j, failed); return false; |
| 342 | end; |
| 343 | if j.phase = waiting then INC(busy); end; |
| 344 | j.phase := phase; |
| 345 | j.deadline := Sys.clock() + timeout; |
| 346 | return true; |
| 347 | end; |
| 348 | |
| 349 | (* Starts building the test j: raz -Werror, the -I directories, -o. *) |
| 350 | proc build(var j: Job); |
| 351 | var args: Names.Vec; i: INT; |
| 352 | begin |
| 353 | Names.init(args, Heap.heap); |
| 354 | defer Names.done(args); |
| 355 | Names.push(args, raz); |
| 356 | Names.push(args, "-Werror"); |
| 357 | for i := 0 to includes.len - 1 do |
| 358 | Names.push(args, "-I"); |
| 359 | Names.push(args, Names.all(includes)[i]); |
| 360 | end; |
| 361 | Names.push(args, "-o"); |
| 362 | Names.push(args, j.bin); |
| 363 | Names.push(args, j.path); |
| 364 | j.began := Sys.clock(); |
| 365 | _ := start(j, Names.all(args), building); |
| 366 | end; |
| 367 | |
| 368 | (* The test j's phase has ended, how and with value as Sys.wait says: |
| 369 | on to running it, or the end. *) |
| 370 | proc ended(var j: Job; how: Sys.Status; value: INT); |
| 371 | var args: [1][]const U8; |
| 372 | begin |
| 373 | if (j.phase = building) and (how = Sys.exited) and (value = 0) then |
| 374 | args[0] := j.bin; |
| 375 | _ := start(j, args, running); |
| 376 | return; |
| 377 | end; |
| 378 | if j.phase = building then |
| 379 | if (how = Sys.exited) and (value = 1) then Buf.str(j.why, "doesn't compile"); |
| 380 | elsif how = Sys.exited then Fmt.write(j.why, "raz status {}", [Fmt.int(value)]); |
| 381 | else Fmt.write(j.why, "raz signal {}", [Fmt.int(value)]); |
| 382 | end; |
| 383 | finish(j, failed); |
| 384 | elsif (how = Sys.exited) and (value = 0) then finish(j, passed); |
| 385 | elsif how = Sys.exited then Fmt.write(j.why, "status {}", [Fmt.int(value)]); finish(j, failed); |
| 386 | else Fmt.write(j.why, "signal {}", [Fmt.int(value)]); finish(j, failed); |
| 387 | end; |
| 388 | end; |
| 389 | |
| 390 | (* Looks at the test j, being built or run, without waiting: whether |
| 391 | it has ended its phase, or run out of time and been killed. *) |
| 392 | proc poll(var j: Job): BOOL; |
| 393 | var pid, value: INT; how: Sys.Status; |
| 394 | begin |
| 395 | pid, how, value := Sys.wait(j.pid, false); |
| 396 | if pid < 0 then |
| 397 | Fmt.write(j.why, "lost: {}", [Fmt.str(Sys.errorText(pid))]); |
| 398 | finish(j, failed); |
| 399 | elsif (pid > 0) and (how # Sys.stopped) then |
| 400 | ended(j, how, value); |
| 401 | elsif Sys.clock() > j.deadline then |
| 402 | _ := Sys.kill(-j.pid, Sys.sigKill); |
| 403 | _, _, _ := Sys.wait(j.pid, true); |
| 404 | finish(j, timedOut); |
| 405 | else |
| 406 | return false; |
| 407 | end; |
| 408 | return true; |
| 409 | end; |
| 410 | |
| 411 | (* Writes word, in colour c on a terminal, padded to a column of 8. *) |
| 412 | proc putWord(word, c: []const U8); |
| 413 | var i: INT; |
| 414 | begin |
| 415 | if colour then Out.str(c); Out.str(word); Out.str(Plain); else Out.str(word); end; |
| 416 | for i := LEN(word) to 7 do Out.char(' '); end; |
| 417 | end; |
| 418 | |
| 419 | (* Writes the file at path, each line after the text lead. *) |
| 420 | proc putLog(path, lead: []const U8); |
| 421 | var b: Buf.Buf; s, line, rest: []const U8; more: BOOL; |
| 422 | begin |
| 423 | Buf.init(b, Heap.heap); |
| 424 | defer Buf.done(b); |
| 425 | if not Files.read(path, b) then Fmt.print("{}(can't read {})\n", [Fmt.str(lead), Fmt.str(path)]); return; end; |
| 426 | s := Buf.bytes(b); |
| 427 | while LEN(s) > 0 do |
| 428 | line, rest, more := Str.cut(s, "\n"); |
| 429 | Fmt.print("{}{}\n", [Fmt.str(lead), Fmt.str(line)]); |
| 430 | s := rest; |
| 431 | if not more then break; end; |
| 432 | end; |
| 433 | end; |
| 434 | |
| 435 | (* Why the test j failed: what its why says, or that it timed out. *) |
| 436 | proc reason(const j: Job): []const U8; |
| 437 | begin |
| 438 | if j.result = timedOut then return "timed out"; end; |
| 439 | return Buf.bytes(j.why); |
| 440 | end; |
| 441 | |
| 442 | (* Writes the line of the ended test j, and its log if it failed. *) |
| 443 | proc reportLine(const j: Job); |
| 444 | var secs: Fmt.Arg; |
| 445 | begin |
| 446 | secs := Fmt.real(F64(j.took) / F64(Second), 2); |
| 447 | if j.result = passed then |
| 448 | putWord("ok", Green); |
| 449 | Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]); |
| 450 | elsif j.result = timedOut then |
| 451 | putWord("TIMEOUT", Yellow); |
| 452 | Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]); |
| 453 | putLog(j.log, " "); |
| 454 | else |
| 455 | putWord("FAIL", Red); |
| 456 | Fmt.print("{} {} {}s\n", [Fmt.str(j.path), Fmt.str(Buf.bytes(j.why)), secs]); |
| 457 | putLog(j.log, " "); |
| 458 | end; |
| 459 | end; |
| 460 | |
| 461 | (* Writes the TAP lines of the ended test j, the kth: its result, and |
| 462 | its log in comment lines if it failed. *) |
| 463 | proc reportTap(const j: Job; k: INT); |
| 464 | begin |
| 465 | if j.result = passed then |
| 466 | Fmt.print("ok {} - {}\n", [Fmt.int(k), Fmt.str(j.path)]); |
| 467 | return; |
| 468 | end; |
| 469 | Fmt.print("not ok {} - {} # {}\n", [Fmt.int(k), Fmt.str(j.path), Fmt.str(reason(j))]); |
| 470 | putLog(j.log, "# "); |
| 471 | end; |
| 472 | |
| 473 | (* Writes what ended test j, the kth, in the report asked for. *) |
| 474 | proc report(const j: Job; k: INT); |
| 475 | begin |
| 476 | if tap then reportTap(j, k); else reportLine(j); end; |
| 477 | Out.flush; |
| 478 | end; |
| 479 | |
| 480 | (* Kills every test being built or run, after a signal to stop, and |
| 481 | ends with the shell's status for it, 128 and its number. *) |
| 482 | proc interrupted(sig: INT); |
| 483 | var all: []Job; i: INT; |
| 484 | begin |
| 485 | all := Jobs.all(tests); |
| 486 | for i := 0 to LEN(all) - 1 do |
| 487 | if active(all[i]) then |
| 488 | _ := Sys.kill(-all[i].pid, Sys.sigKill); |
| 489 | _, _, _ := Sys.wait(all[i].pid, true); |
| 490 | end; |
| 491 | end; |
| 492 | Out.flush; |
| 493 | Rt.exit(128 + sig); |
| 494 | end; |
| 495 | |
| 496 | (* Builds and runs every test, jobs at a time, and reports each in |
| 497 | order as soon as it and those before it have ended. *) |
| 498 | proc runAll; |
| 499 | var all: []Job; next, shown, i: INT; changed: BOOL; |
| 500 | begin |
| 501 | all := Jobs.all(tests); |
| 502 | next := 0; shown := 0; |
| 503 | while shown < LEN(all) do |
| 504 | while (busy < jobs) and (next < LEN(all)) do build(all[next]); INC(next); end; |
| 505 | changed := false; |
| 506 | for i := 0 to next - 1 do |
| 507 | if active(all[i]) and poll(all[i]) then changed := true; end; |
| 508 | end; |
| 509 | while (shown < LEN(all)) and (all[shown].phase = done) do report(all[shown], shown + 1); INC(shown); end; |
| 510 | if not changed and Sys.ready(signals, PollMs) then interrupted(Sys.caught(signals)); end; |
| 511 | end; |
| 512 | end; |
| 513 | |
| 514 | (* Writes the plan of a TAP report: how many tests there are. *) |
| 515 | proc tapPlan; |
| 516 | begin |
| 517 | Fmt.print("TAP version 14\n1..{}\n", [Fmt.int(tests.len)]); |
| 518 | Out.flush; |
| 519 | end; |
| 520 | |
| 521 | (* Adds s to b as XML text or an attribute: & < > and " escaped, and |
| 522 | each control byte but tab, newline and carriage return, which XML 1.0 |
| 523 | forbids, written as ?. Bytes from 128 up, valid UTF-8 or not, are |
| 524 | passed through as they are. *) |
| 525 | proc xml(var b: Buf.Buf; s: []const U8); |
| 526 | var i: INT; c: U8; |
| 527 | begin |
| 528 | for i := 0 to LEN(s) - 1 do |
| 529 | c := s[i]; |
| 530 | if c = '&' then Buf.str(b, "&"); |
| 531 | elsif c = '<' then Buf.str(b, "<"); |
| 532 | elsif c = '>' then Buf.str(b, ">"); |
| 533 | elsif c = '"' then Buf.str(b, """); |
| 534 | elsif (c < 32) and (c # 9) and (c # 10) and (c # 13) then Buf.add(b, '?'); |
| 535 | else Buf.add(b, c); |
| 536 | end; |
| 537 | end; |
| 538 | end; |
| 539 | |
| 540 | (* Adds the XML element of the test j to b: a testcase, with a failure |
| 541 | inside it, holding the log, if it failed. *) |
| 542 | proc testCase(var b: Buf.Buf; const j: Job); |
| 543 | var log: Buf.Buf; |
| 544 | begin |
| 545 | Buf.str(b, "<testcase name=\""); |
| 546 | xml(b, j.name); |
| 547 | Buf.str(b, "\" classname=\""); |
| 548 | xml(b, j.path); |
| 549 | Fmt.write(b, "\" time=\"{}\"", [Fmt.real(F64(j.took) / F64(Second), 2)]); |
| 550 | if j.result = passed then Buf.str(b, "/>\n"); return; end; |
| 551 | Buf.str(b, "><failure message=\""); |
| 552 | xml(b, reason(j)); |
| 553 | Buf.str(b, "\">"); |
| 554 | Buf.init(log, Heap.heap); |
| 555 | defer Buf.done(log); |
| 556 | if Files.read(j.log, log) then xml(b, Buf.bytes(log)); end; |
| 557 | Buf.str(b, "</failure></testcase>\n"); |
| 558 | end; |
| 559 | |
| 560 | (* Writes the results to the --junit file, which took the whole run |
| 561 | took nanoseconds; status 2 if it can't be written. *) |
| 562 | proc writeJunit(took: I64); |
| 563 | var b: Buf.Buf; all: []Job; i, failures: INT; counts, secs: Fmt.Arg; |
| 564 | begin |
| 565 | all := Jobs.all(tests); |
| 566 | for i := 0 to LEN(all) - 1 do |
| 567 | if all[i].result # passed then INC(failures); end; |
| 568 | end; |
| 569 | counts := Fmt.int(LEN(all)); |
| 570 | secs := Fmt.real(F64(took) / F64(Second), 2); |
| 571 | Buf.init(b, Heap.heap); |
| 572 | defer Buf.done(b); |
| 573 | Buf.str(b, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
| 574 | Fmt.write(b, "<testsuites tests=\"{}\" failures=\"{}\" time=\"{}\">\n", [counts, Fmt.int(failures), secs]); |
| 575 | Fmt.write(b, "<testsuite name=\"raztest\" tests=\"{}\" failures=\"{}\" time=\"{}\">\n", [counts, Fmt.int(failures), secs]); |
| 576 | for i := 0 to LEN(all) - 1 do testCase(b, all[i]); end; |
| 577 | Buf.str(b, "</testsuite>\n</testsuites>\n"); |
| 578 | if not Files.write(junit, Buf.bytes(b)) then die("can't write {}", [Fmt.str(junit)]); end; |
| 579 | end; |
| 580 | |
| 581 | (* The summary line and the --junit file; whether all the tests passed. *) |
| 582 | proc summary(took: I64): BOOL; |
| 583 | var all: []Job; i: INT; count: [3]INT; |
| 584 | begin |
| 585 | all := Jobs.all(tests); |
| 586 | for i := 0 to LEN(all) - 1 do INC(count[INT(all[i].result)]); end; |
| 587 | if tap then Out.str("# "); end; |
| 588 | Fmt.print("{} passed, {} failed, {} timed out, {}s\n", |
| 589 | [Fmt.int(count[INT(passed)]), Fmt.int(count[INT(failed)]), Fmt.int(count[INT(timedOut)]), |
| 590 | Fmt.real(F64(took) / F64(Second), 2)]); |
| 591 | Out.flush; |
| 592 | if LEN(junit) > 0 then writeJunit(took); end; |
| 593 | return count[INT(passed)] = LEN(all); |
| 594 | end; |
| 595 | |
| 596 | (* Loads, runs and reports the tests once; whether all passed. *) |
| 597 | proc cycle(): BOOL; |
| 598 | var began: I64; |
| 599 | begin |
| 600 | began := Sys.clock(); |
| 601 | loadTests; |
| 602 | if tap then tapPlan; end; |
| 603 | runAll; |
| 604 | return summary(Sys.clock() - began); |
| 605 | end; |
| 606 | |
| 607 | (* The directory of the file path. *) |
| 608 | proc dirOf(path: []const U8): []const U8; |
| 609 | var i: INT; |
| 610 | begin |
| 611 | i := Str.findLast(path, "/"); |
| 612 | if i < 0 then return "."; end; |
| 613 | if i = 0 then return "/"; end; |
| 614 | return path[0..i]; |
| 615 | end; |
| 616 | |
| 617 | (* Adds dir to the directories to watch, unless it is there. *) |
| 618 | proc watchDir(var dirs: Names.Vec; dir: []const U8); |
| 619 | var i: INT; |
| 620 | begin |
| 621 | for i := 0 to dirs.len - 1 do |
| 622 | if Names.all(dirs)[i] = dir then return; end; |
| 623 | end; |
| 624 | Names.push(dirs, dir); |
| 625 | end; |
| 626 | |
| 627 | (* The directories to watch: those given, those of the test files given, |
| 628 | and the -I ones. *) |
| 629 | proc watchDirs(var dirs: Names.Vec); |
| 630 | var i, err: INT; f: Sys.Info; path: []const U8; |
| 631 | begin |
| 632 | Names.init(dirs, Heap.heap); |
| 633 | for i := 0 to operands.len - 1 do |
| 634 | path := Names.all(operands)[i]; |
| 635 | f, err := Sys.info(path); |
| 636 | if (err = 0) and f.dir then watchDir(dirs, path); else watchDir(dirs, dirOf(path)); end; |
| 637 | end; |
| 638 | for i := 0 to includes.len - 1 do watchDir(dirs, Names.all(includes)[i]); end; |
| 639 | end; |
| 640 | |
| 641 | (* Adds to the fingerprint and the count the *.raz files of dir. It |
| 642 | runs every few hundred milliseconds for as long as -w does, so the |
| 643 | paths are made in one buffer, used again. *) |
| 644 | proc lookIn(dir: []const U8; var print: I64; var count: INT); |
| 645 | var d: Sys.Dir; name: []const U8; ok: BOOL; f: Sys.Info; err: INT; path: Buf.Buf; |
| 646 | begin |
| 647 | if Sys.openDir(d, dir) < 0 then return; end; |
| 648 | Buf.init(path, Heap.heap); |
| 649 | defer Buf.done(path); |
| 650 | name, ok := Sys.nextEntry(d); |
| 651 | while ok do |
| 652 | Buf.reset(path); |
| 653 | Buf.str(path, dir); Buf.str(path, "/"); Buf.str(path, name); |
| 654 | f, err := Sys.info(Buf.bytes(path)); |
| 655 | if Str.hasSuffix(name, ".raz") and (err = 0) then |
| 656 | INC(count); |
| 657 | print := (print + 1 + f.mtime mod Modulus + f.size mod Modulus) mod Modulus; |
| 658 | end; |
| 659 | name, ok := Sys.nextEntry(d); |
| 660 | end; |
| 661 | Sys.closeDir(d); |
| 662 | end; |
| 663 | |
| 664 | (* A number that changes when a *.raz file in the directories is edited, |
| 665 | added or removed, and how many files there are. *) |
| 666 | proc fingerprint(): (I64, INT); |
| 667 | var dirs: Names.Vec; i, count: INT; print: I64; |
| 668 | begin |
| 669 | watchDirs(dirs); |
| 670 | defer Names.done(dirs); |
| 671 | for i := 0 to dirs.len - 1 do lookIn(Names.all(dirs)[i], print, count); end; |
| 672 | return print, count; |
| 673 | end; |
| 674 | |
| 675 | (* Waits one look's time; a signal that stops raztest ends it at once. *) |
| 676 | proc nap; |
| 677 | begin |
| 678 | if Sys.ready(signals, WatchMs) then interrupted(Sys.caught(signals)); end; |
| 679 | end; |
| 680 | |
| 681 | (* Waits until the fingerprint is not seen, and then does not change |
| 682 | for one look. *) |
| 683 | proc waitChange(seen: I64); |
| 684 | var print: I64; |
| 685 | begin |
| 686 | repeat |
| 687 | nap; |
| 688 | print, _ := fingerprint(); |
| 689 | until print # seen; |
| 690 | repeat |
| 691 | seen := print; |
| 692 | nap; |
| 693 | print, _ := fingerprint(); |
| 694 | until print = seen; |
| 695 | end; |
| 696 | |
| 697 | (* Runs the tests, and again at each change, for ever. *) |
| 698 | proc watchAll; |
| 699 | var seen: I64; n: INT; |
| 700 | begin |
| 701 | while true do |
| 702 | if clearing then Out.str(Clear); end; |
| 703 | seen, n := fingerprint(); |
| 704 | _ := cycle(); |
| 705 | if tap then Out.str("# "); end; |
| 706 | Fmt.print("watching {} files; ^C stops\n", [Fmt.int(n)]); |
| 707 | Out.flush; |
| 708 | waitChange(seen); |
| 709 | end; |
| 710 | end; |
| 711 | |
| 712 | proc main*; |
| 713 | begin |
| 714 | readArgs; |
| 715 | setUp; |
| 716 | if watch then watchAll; end; |
| 717 | if not cycle() then Rt.exit(1); end; |
| 718 | end; |
| 719 | end Test. |