| | @@ -0,0 +1,432 @@ |
| 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] file.raz|dir ... |
| 4 | + |
| 5 | + A test is a module whose main is its test: it passes if it builds |
| 6 | + with raz -Werror and then exits with status 0, as a program that |
| 7 | + checks with Check does when no check fails. The tests are the files |
| 8 | + given; a directory gives each *.raz directly in it that has a line |
| 9 | + starting proc main*. A test's name is its file's, without .raz. |
| 10 | + |
| 11 | + Each is built into the -o directory, as Name, by $RAZ (raz on PATH if |
| 12 | + unset), and run there with no input, in a process group of its own; |
| 13 | + what the compiler, then the test, write goes to Name.out beside it, |
| 14 | + a file, so that nothing need be read while they run and the logs stay |
| 15 | + for CI. -j tests go at once, polled every few milliseconds, with no |
| 16 | + threads; one that takes longer than -t seconds to build, or to run, |
| 17 | + has its group killed. |
| 18 | + |
| 19 | + A line for each test as it ends, in the order given, with the |
| 20 | + .out of one that failed or timed out under it; then a summary, |
| 21 | + |
| 22 | + 12 passed, 1 failed, 0 timed out, 3.41s |
| 23 | + |
| 24 | + and status 0 if all passed, 1 if not, 2 for a mistake in the |
| 25 | + command line or the setup. The words ok, FAIL and TIMEOUT are in |
| 26 | + colour when standard output is a terminal and NO_COLOR is unset. |
| 27 | + |
| 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. *) |
| 32 | +module Test; |
| 33 | +import Opt, Fmt, Out, Buf, Str, Heap, Sys, Rt, Files; |
| 34 | +import Names := Vec([]U8), Jobs := Vec(Job), ByName := Sort([]U8, less); |
| 35 | + |
| 36 | +const |
| 37 | + Table: []Opt.Row = [ |
| 38 | + Opt.usage("", "raztest [-I dir] [-j n] [-t secs] [-o dir] [-r text] file.raz|dir ..."), |
| 39 | + Opt.option("", 'I', "include", "dir", "look for modules in dir too, as raz -I"), |
| 40 | + Opt.option("", 'j', "jobs", "n", "run n tests at once (1)"), |
| 41 | + Opt.option("", 't', "timeout", "secs", "kill a build or run after secs (60)"), |
| 42 | + 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")]; |
| 44 | + PollMs = 10; (* how long to wait between looks at the running tests *) |
| 45 | + Second = 1000000000; (* a second in Sys.clock's nanoseconds *) |
| 46 | + Green = "\x1b[32m"; (* the colours of ok, FAIL and TIMEOUT *) |
| 47 | + Red = "\x1b[31m"; |
| 48 | + Yellow = "\x1b[33m"; |
| 49 | + Plain = "\x1b[0m"; (* back to no colour *) |
| 50 | + |
| 51 | +type |
| 52 | + (* Where a test is: not started, being built, being run, or ended. *) |
| 53 | + Phase = (waiting, building, running, done); |
| 54 | + |
| 55 | + (* How a test ended. *) |
| 56 | + Result = (passed, failed, timedOut); |
| 57 | + |
| 58 | + (* A test and how it is going. *) |
| 59 | + Job = record |
| 60 | + path: []U8; (* its file, as given *) |
| 61 | + name: []U8; (* the file's name without .raz *) |
| 62 | + bin: []U8; (* its program, in the out directory *) |
| 63 | + log: []U8; (* its .out, beside the program *) |
| 64 | + phase: Phase; |
| 65 | + pid: INT; (* the compiler's or the test's, while building or running *) |
| 66 | + began: I64; (* Sys.clock when its build started *) |
| 67 | + deadline: I64; (* Sys.clock when the phase under way is killed *) |
| 68 | + took: I64; (* nanoseconds from its build's start to its end *) |
| 69 | + result: Result; |
| 70 | + why: Buf.Buf; (* for a failure, what failed: "status 1" *) |
| 71 | + end; |
| 72 | + |
| 73 | +var |
| 74 | + raz: []U8; (* the compiler: $RAZ, or raz on PATH *) |
| 75 | + includes: Names.Vec; (* the -I directories *) |
| 76 | + jobs: INT; (* -j: tests at once *) |
| 77 | + timeout: I64; (* -t, in nanoseconds *) |
| 78 | + outDir: []U8; (* -o *) |
| 79 | + only: []U8; (* -r: the text a name must hold, or "" *) |
| 80 | + tests: Jobs.Vec; (* in the order they are reported *) |
| 81 | + busy: INT; (* tests being built or run *) |
| 82 | + devNull: INT; (* /dev/null, each test's standard input *) |
| 83 | + signals: INT; (* the fd the interrupting signals come on *) |
| 84 | + colour: BOOL; (* the words of the results are coloured *) |
| 85 | + |
| 86 | +(* Whether a sorts before b, for sorting a directory's files. *) |
| 87 | +proc less(a, b: []U8): BOOL; |
| 88 | +begin |
| 89 | + return Str.cmp(a, b) < 0; |
| 90 | +end; |
| 91 | + |
| 92 | +(* A mistake in the setup: the message, and status 2. *) |
| 93 | +proc die(f: []U8; args: []Fmt.Arg); |
| 94 | +var b: Buf.Buf; |
| 95 | +begin |
| 96 | + Buf.init(b, Heap.heap); |
| 97 | + Fmt.write(b, "raztest: ", []); |
| 98 | + Fmt.write(b, f, args); |
| 99 | + Fmt.eprint("{}\n", [Fmt.str(Buf.bytes(b))]); |
| 100 | + Rt.exit(2); |
| 101 | +end; |
| 102 | + |
| 103 | +(* a, b and c joined, in memory of its own that is never freed. *) |
| 104 | +proc join(a, b, c: []U8): []U8; |
| 105 | +var s: Buf.Buf; |
| 106 | +begin |
| 107 | + Buf.init(s, Heap.heap); |
| 108 | + Buf.str(s, a); Buf.str(s, b); Buf.str(s, c); |
| 109 | + return Buf.bytes(s); |
| 110 | +end; |
| 111 | + |
| 112 | +(* Whether the source at path has a line that starts proc main*. *) |
| 113 | +proc hasMain(path: []U8): BOOL; |
| 114 | +var b: Buf.Buf; src: []U8; |
| 115 | +begin |
| 116 | + Buf.init(b, Heap.heap); |
| 117 | + defer Buf.done(b); |
| 118 | + if not Files.read(path, b) then return false; end; |
| 119 | + src := Buf.bytes(b); |
| 120 | + return Str.hasPrefix(src, "proc main*") or (Str.find(src, "\nproc main*") >= 0); |
| 121 | +end; |
| 122 | + |
| 123 | +(* Adds the test at path, unless -r leaves it out. *) |
| 124 | +proc add(path: []U8); |
| 125 | +var j: ^Job; name: []U8; |
| 126 | +begin |
| 127 | + name := path[Str.findLast(path, "/") + 1..LEN(path)]; |
| 128 | + if Str.hasSuffix(name, ".raz") then name := name[0..LEN(name) - 4]; end; |
| 129 | + if Str.find(name, only) < 0 then return; end; |
| 130 | + j := Jobs.add(tests); |
| 131 | + j^ := {path: path, name: name, bin: join(outDir, "/", name), phase: waiting}; |
| 132 | + j^.log := join(j^.bin, ".out", ""); |
| 133 | + Buf.init(j^.why, Heap.heap); |
| 134 | +end; |
| 135 | + |
| 136 | +(* Adds the tests of the directory dir: its *.raz with a main, by name. *) |
| 137 | +proc addDir(dir: []U8); |
| 138 | +var d: Sys.Dir; files: Names.Vec; name, path: []U8; ok: BOOL; err, i: INT; |
| 139 | +begin |
| 140 | + err := Sys.openDir(d, dir); |
| 141 | + if err < 0 then die("can't read {}: {}", [Fmt.str(dir), Fmt.str(Sys.errorText(err))]); end; |
| 142 | + Names.init(files, Heap.heap); |
| 143 | + name, ok := Sys.nextEntry(d); |
| 144 | + while ok do |
| 145 | + path := join(dir, "/", name); |
| 146 | + if Str.hasSuffix(name, ".raz") and hasMain(path) then Names.push(files, path); end; |
| 147 | + name, ok := Sys.nextEntry(d); |
| 148 | + end; |
| 149 | + Sys.closeDir(d); |
| 150 | + ByName.sort(Names.all(files)); |
| 151 | + for i := 0 to files.len - 1 do add(Names.all(files)[i]); end; |
| 152 | +end; |
| 153 | + |
| 154 | +(* Adds what an operand names: a directory's tests, or a file. *) |
| 155 | +proc addOperand(path: []U8); |
| 156 | +var f: Sys.Info; err: INT; |
| 157 | +begin |
| 158 | + f, err := Sys.info(path); |
| 159 | + if err < 0 then die("{}: {}", [Fmt.str(path), Fmt.str(Sys.errorText(err))]); end; |
| 160 | + if f.dir then addDir(path); else add(path); end; |
| 161 | +end; |
| 162 | + |
| 163 | +(* Stops if two tests have one name, as their programs would. *) |
| 164 | +proc checkNames; |
| 165 | +var all: []Job; i, k: INT; |
| 166 | +begin |
| 167 | + all := Jobs.all(tests); |
| 168 | + for i := 0 to LEN(all) - 1 do |
| 169 | + for k := 0 to i - 1 do |
| 170 | + if all[i].name = all[k].name then |
| 171 | + die("{} and {} are both named {}", [Fmt.str(all[k].path), Fmt.str(all[i].path), Fmt.str(all[i].name)]); |
| 172 | + end; |
| 173 | + end; |
| 174 | + end; |
| 175 | +end; |
| 176 | + |
| 177 | +(* Reads the command line into the settings and the list of tests. *) |
| 178 | +proc readArgs; |
| 179 | +var p: Opt.Parser; arg: []U8; ok: BOOL; |
| 180 | +begin |
| 181 | + jobs := 1; timeout := 60 * Second; outDir := "build/raztest"; |
| 182 | + Names.init(includes, Heap.heap); |
| 183 | + Jobs.init(tests, Heap.heap); |
| 184 | + Opt.init(p, "raztest", Table); |
| 185 | + while Opt.next(p) do |
| 186 | + if p.letter = 'I' then Names.push(includes, p.value); |
| 187 | + elsif p.letter = 'j' then jobs := INT(Opt.number(p, 1, 1000)); |
| 188 | + elsif p.letter = 't' then timeout := Opt.number(p, 1, 1000000) * Second; |
| 189 | + elsif p.letter = 'o' then outDir := p.value; |
| 190 | + elsif p.letter = 'r' then only := p.value; |
| 191 | + end; |
| 192 | + end; |
| 193 | + arg, ok := Opt.operand(p); |
| 194 | + if not ok then Opt.fail(p, "no tests named; raztest --help says how"); end; |
| 195 | + repeat |
| 196 | + addOperand(arg); |
| 197 | + arg, ok := Opt.operand(p); |
| 198 | + until not ok; |
| 199 | + checkNames; |
| 200 | +end; |
| 201 | + |
| 202 | +(* Makes the directory dir and those it is in, as mkdir -p does. *) |
| 203 | +proc makeDirs(dir: []U8); |
| 204 | +var i, err: INT; f: Sys.Info; |
| 205 | +begin |
| 206 | + for i := 1 to LEN(dir) - 1 do |
| 207 | + if dir[i] = '/' then _ := Sys.makeDir(dir[0..i]); end; |
| 208 | + end; |
| 209 | + err := Sys.makeDir(dir); |
| 210 | + if err = Sys.errExists then |
| 211 | + f, err := Sys.info(dir); |
| 212 | + if (err = 0) and not f.dir then err := Sys.errNotDir; end; |
| 213 | + end; |
| 214 | + if err < 0 then |
| 215 | + die("can't make {}: {}", [Fmt.str(dir), Fmt.str(Sys.errorText(err))]); |
| 216 | + end; |
| 217 | +end; |
| 218 | + |
| 219 | +(* Readies what every test needs: the compiler's name, the out |
| 220 | + directory, /dev/null, the signals that stop raztest, and colour. *) |
| 221 | +proc setUp; |
| 222 | +begin |
| 223 | + raz := Sys.env("RAZ"); |
| 224 | + if LEN(raz) = 0 then raz := "raz"; end; |
| 225 | + makeDirs(outDir); |
| 226 | + devNull := Sys.openRead("/dev/null"); |
| 227 | + if devNull < 0 then die("can't open /dev/null: {}", [Fmt.str(Sys.errorText(devNull))]); end; |
| 228 | + signals := Sys.catch([Sys.sigInt, Sys.sigTerm, Sys.sigHup]); |
| 229 | + 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); |
| 231 | +end; |
| 232 | + |
| 233 | +(* Whether the test j is being built or run. *) |
| 234 | +proc active(const j: Job): BOOL; |
| 235 | +begin |
| 236 | + return (j.phase = building) or (j.phase = running); |
| 237 | +end; |
| 238 | + |
| 239 | +(* Ends the test j's phase as result: what failed is in j.why. *) |
| 240 | +proc finish(var j: Job; result: Result); |
| 241 | +begin |
| 242 | + if active(j) then DEC(busy); end; |
| 243 | + j.phase := done; |
| 244 | + j.result := result; |
| 245 | + j.took := Sys.clock() - j.began; |
| 246 | +end; |
| 247 | + |
| 248 | +(* Starts args[0] with its output and errors into j's log, as j's next |
| 249 | + phase, in a group of its own; false, with j failed, if it can't. *) |
| 250 | +proc start(var j: Job; args: [][]U8; phase: Phase): BOOL; |
| 251 | +var fd: INT; |
| 252 | +begin |
| 253 | + fd := Sys.create(j.log); |
| 254 | + if fd < 0 then |
| 255 | + Fmt.write(j.why, "can't write {}: {}", [Fmt.str(j.log), Fmt.str(Sys.errorText(fd))]); |
| 256 | + finish(j, failed); return false; |
| 257 | + end; |
| 258 | + j.pid := Sys.spawn({path: args[0], args: args, fds: [devNull, fd, fd], group: -1}); |
| 259 | + Sys.close(fd); |
| 260 | + if j.pid < 0 then |
| 261 | + Fmt.write(j.why, "can't start {}: {}", [Fmt.str(args[0]), Fmt.str(Sys.errorText(j.pid))]); |
| 262 | + finish(j, failed); return false; |
| 263 | + end; |
| 264 | + if j.phase = waiting then INC(busy); end; |
| 265 | + j.phase := phase; |
| 266 | + j.deadline := Sys.clock() + timeout; |
| 267 | + return true; |
| 268 | +end; |
| 269 | + |
| 270 | +(* Starts building the test j: raz -Werror, the -I directories, -o. *) |
| 271 | +proc build(var j: Job); |
| 272 | +var args: Names.Vec; i: INT; |
| 273 | +begin |
| 274 | + Names.init(args, Heap.heap); |
| 275 | + defer Names.done(args); |
| 276 | + Names.push(args, raz); |
| 277 | + Names.push(args, "-Werror"); |
| 278 | + for i := 0 to includes.len - 1 do |
| 279 | + Names.push(args, "-I"); |
| 280 | + Names.push(args, Names.all(includes)[i]); |
| 281 | + end; |
| 282 | + Names.push(args, "-o"); |
| 283 | + Names.push(args, j.bin); |
| 284 | + Names.push(args, j.path); |
| 285 | + j.began := Sys.clock(); |
| 286 | + _ := start(j, Names.all(args), building); |
| 287 | +end; |
| 288 | + |
| 289 | +(* The test j's phase has ended, how and with value as Sys.wait says: |
| 290 | + on to running it, or the end. *) |
| 291 | +proc ended(var j: Job; how: Sys.Status; value: INT); |
| 292 | +var args: [1][]U8; |
| 293 | +begin |
| 294 | + if (j.phase = building) and (how = Sys.exited) and (value = 0) then |
| 295 | + args[0] := j.bin; |
| 296 | + _ := start(j, args, running); |
| 297 | + return; |
| 298 | + end; |
| 299 | + if j.phase = building then |
| 300 | + if (how = Sys.exited) and (value = 1) then Buf.str(j.why, "doesn't compile"); |
| 301 | + elsif how = Sys.exited then Fmt.write(j.why, "raz status {}", [Fmt.int(value)]); |
| 302 | + else Fmt.write(j.why, "raz signal {}", [Fmt.int(value)]); |
| 303 | + end; |
| 304 | + finish(j, failed); |
| 305 | + elsif (how = Sys.exited) and (value = 0) then finish(j, passed); |
| 306 | + elsif how = Sys.exited then Fmt.write(j.why, "status {}", [Fmt.int(value)]); finish(j, failed); |
| 307 | + else Fmt.write(j.why, "signal {}", [Fmt.int(value)]); finish(j, failed); |
| 308 | + end; |
| 309 | +end; |
| 310 | + |
| 311 | +(* Looks at the test j, being built or run, without waiting: whether |
| 312 | + it has ended its phase, or run out of time and been killed. *) |
| 313 | +proc poll(var j: Job): BOOL; |
| 314 | +var pid, value: INT; how: Sys.Status; |
| 315 | +begin |
| 316 | + pid, how, value := Sys.wait(j.pid, false); |
| 317 | + if pid < 0 then |
| 318 | + Fmt.write(j.why, "lost: {}", [Fmt.str(Sys.errorText(pid))]); |
| 319 | + finish(j, failed); |
| 320 | + elsif (pid > 0) and (how # Sys.stopped) then |
| 321 | + ended(j, how, value); |
| 322 | + elsif Sys.clock() > j.deadline then |
| 323 | + _ := Sys.kill(-j.pid, Sys.sigKill); |
| 324 | + _, _, _ := Sys.wait(j.pid, true); |
| 325 | + finish(j, timedOut); |
| 326 | + else |
| 327 | + return false; |
| 328 | + end; |
| 329 | + return true; |
| 330 | +end; |
| 331 | + |
| 332 | +(* Writes word, in colour c on a terminal, padded to a column of 8. *) |
| 333 | +proc putWord(word, c: []U8); |
| 334 | +var i: INT; |
| 335 | +begin |
| 336 | + if colour then Out.str(c); Out.str(word); Out.str(Plain); else Out.str(word); end; |
| 337 | + for i := LEN(word) to 7 do Out.char(' '); end; |
| 338 | +end; |
| 339 | + |
| 340 | +(* Writes the file at path, each line indented by 4 spaces. *) |
| 341 | +proc putIndented(path: []U8); |
| 342 | +var b: Buf.Buf; s, line, rest: []U8; more: BOOL; |
| 343 | +begin |
| 344 | + Buf.init(b, Heap.heap); |
| 345 | + defer Buf.done(b); |
| 346 | + if not Files.read(path, b) then Fmt.print(" (can't read {})\n", [Fmt.str(path)]); return; end; |
| 347 | + s := Buf.bytes(b); |
| 348 | + while LEN(s) > 0 do |
| 349 | + line, rest, more := Str.cut(s, "\n"); |
| 350 | + Fmt.print(" {}\n", [Fmt.str(line)]); |
| 351 | + s := rest; |
| 352 | + if not more then break; end; |
| 353 | + end; |
| 354 | +end; |
| 355 | + |
| 356 | +(* Writes the line of the ended test j, and its log if it failed. *) |
| 357 | +proc report(const j: Job); |
| 358 | +var secs: Fmt.Arg; |
| 359 | +begin |
| 360 | + secs := Fmt.real(F64(j.took) / F64(Second), 2); |
| 361 | + if j.result = passed then |
| 362 | + putWord("ok", Green); |
| 363 | + Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]); |
| 364 | + elsif j.result = timedOut then |
| 365 | + putWord("TIMEOUT", Yellow); |
| 366 | + Fmt.print("{} {}s\n", [Fmt.str(j.path), secs]); |
| 367 | + putIndented(j.log); |
| 368 | + else |
| 369 | + putWord("FAIL", Red); |
| 370 | + Fmt.print("{} {} {}s\n", [Fmt.str(j.path), Fmt.str(Buf.bytes(j.why)), secs]); |
| 371 | + putIndented(j.log); |
| 372 | + end; |
| 373 | + Out.flush; |
| 374 | +end; |
| 375 | + |
| 376 | +(* Kills every test being built or run, after a signal to stop, and |
| 377 | + ends with the shell's status for it, 128 and its number. *) |
| 378 | +proc interrupted(sig: INT); |
| 379 | +var all: []Job; i: INT; |
| 380 | +begin |
| 381 | + all := Jobs.all(tests); |
| 382 | + for i := 0 to LEN(all) - 1 do |
| 383 | + if active(all[i]) then |
| 384 | + _ := Sys.kill(-all[i].pid, Sys.sigKill); |
| 385 | + _, _, _ := Sys.wait(all[i].pid, true); |
| 386 | + end; |
| 387 | + end; |
| 388 | + Out.flush; |
| 389 | + Rt.exit(128 + sig); |
| 390 | +end; |
| 391 | + |
| 392 | +(* Builds and runs every test, jobs at a time, and reports each in |
| 393 | + order as soon as it and those before it have ended. *) |
| 394 | +proc runAll; |
| 395 | +var all: []Job; next, shown, i: INT; changed: BOOL; |
| 396 | +begin |
| 397 | + all := Jobs.all(tests); |
| 398 | + next := 0; shown := 0; |
| 399 | + while shown < LEN(all) do |
| 400 | + while (busy < jobs) and (next < LEN(all)) do build(all[next]); INC(next); end; |
| 401 | + changed := false; |
| 402 | + for i := 0 to next - 1 do |
| 403 | + if active(all[i]) and poll(all[i]) then changed := true; end; |
| 404 | + end; |
| 405 | + while (shown < LEN(all)) and (all[shown].phase = done) do report(all[shown]); INC(shown); end; |
| 406 | + if not changed and Sys.ready(signals, PollMs) then interrupted(Sys.caught(signals)); end; |
| 407 | + end; |
| 408 | +end; |
| 409 | + |
| 410 | +(* The summary line, and status 1 if any test failed. *) |
| 411 | +proc summary(took: I64); |
| 412 | +var all: []Job; i: INT; count: [3]INT; |
| 413 | +begin |
| 414 | + all := Jobs.all(tests); |
| 415 | + for i := 0 to LEN(all) - 1 do INC(count[INT(all[i].result)]); end; |
| 416 | + Fmt.print("{} passed, {} failed, {} timed out, {}s\n", |
| 417 | + [Fmt.int(count[INT(passed)]), Fmt.int(count[INT(failed)]), Fmt.int(count[INT(timedOut)]), |
| 418 | + Fmt.real(F64(took) / F64(Second), 2)]); |
| 419 | + Out.flush; |
| 420 | + if count[INT(passed)] < LEN(all) then Rt.exit(1); end; |
| 421 | +end; |
| 422 | + |
| 423 | +proc main*; |
| 424 | +var began: I64; |
| 425 | +begin |
| 426 | + began := Sys.clock(); |
| 427 | + readArgs; |
| 428 | + setUp; |
| 429 | + runAll; |
| 430 | + summary(Sys.clock() - began); |
| 431 | +end; |
| 432 | +end Test. |