-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.zig
More file actions
565 lines (484 loc) · 16.6 KB
/
test_runner.zig
File metadata and controls
565 lines (484 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
const std = @import("std");
const builtin = @import("builtin");
pub const panic = std.debug.FullPanic(panicHandler);
var is_child_mode: bool = false;
const default_job_cap: usize = 16;
fn print(comptime fmt: []const u8, args: anytype) void {
std.debug.print(fmt, args);
}
pub fn fuzz(
context: anytype,
comptime testOne: fn (context: @TypeOf(context), smith: *std.testing.Smith) anyerror!void,
fuzz_opts: std.testing.FuzzInputOptions,
) anyerror!void {
if (comptime builtin.fuzz) {
return fuzzBuiltin(context, testOne, fuzz_opts);
}
if (fuzz_opts.corpus.len == 0) {
var smith: std.testing.Smith = .{ .in = "" };
return testOne(context, &smith);
}
for (fuzz_opts.corpus) |input| {
var smith: std.testing.Smith = .{ .in = input };
try testOne(context, &smith);
}
}
fn fuzzBuiltin(
context: anytype,
comptime testOne: fn (context: @TypeOf(context), smith: *std.testing.Smith) anyerror!void,
fuzz_opts: std.testing.FuzzInputOptions,
) anyerror!void {
const fuzz_abi = std.Build.abi.fuzz;
const Smith = std.testing.Smith;
const Ctx = @TypeOf(context);
const Wrapper = struct {
var ctx: Ctx = undefined;
pub fn testOneC() callconv(.c) void {
var smith: Smith = .{ .in = null };
testOne(ctx, &smith) catch {};
}
};
Wrapper.ctx = context;
var cache_dir: []const u8 = ".";
var map_opt: ?std.process.Environ.Map = null;
if (std.testing.environ.createMap(std.testing.allocator)) |map| {
map_opt = map;
if (map.get("ZIG_CACHE_DIR")) |v| {
cache_dir = v;
} else if (map.get("ZIG_GLOBAL_CACHE_DIR")) |v| {
cache_dir = v;
}
} else |_| {}
fuzz_abi.fuzzer_init(.fromSlice(cache_dir));
const test_name = @typeName(@TypeOf(testOne));
fuzz_abi.fuzzer_set_test(Wrapper.testOneC, .fromSlice(test_name));
for (fuzz_opts.corpus) |input| {
fuzz_abi.fuzzer_new_input(.fromSlice(input));
}
fuzz_abi.fuzzer_main(.forever, 0);
if (map_opt) |*m| m.deinit();
}
pub fn main(init: std.process.Init) !void {
const threaded = std.Io.Threaded.init(init.gpa, .{
.argv0 = .init(init.minimal.args),
.environ = init.minimal.environ,
});
std.testing.io_instance = threaded;
defer std.testing.io_instance.deinit();
std.testing.environ = init.minimal.environ;
var arg_it = try std.process.Args.Iterator.initAllocator(init.minimal.args, init.gpa);
defer arg_it.deinit();
const argv0_z = arg_it.next() orelse "test-runner";
const argv0 = try init.gpa.dupe(u8, argv0_z[0..argv0_z.len]);
defer init.gpa.free(argv0);
var child_test_name: ?[]const u8 = null;
var filter: ?[]const u8 = null;
var exclude_filters: std.ArrayList([]const u8) = .empty;
defer exclude_filters.deinit(init.gpa);
var jobs: ?usize = null;
var seed: ?u32 = null;
while (arg_it.next()) |arg_z| {
const arg = arg_z[0..arg_z.len];
if (std.mem.eql(u8, arg, "--run-test")) {
const name_z = arg_it.next() orelse return error.MissingTestName;
child_test_name = try init.gpa.dupe(u8, name_z[0..name_z.len]);
} else if (std.mem.eql(u8, arg, "--test-filter")) {
const f_z = arg_it.next() orelse return error.MissingFilter;
filter = try init.gpa.dupe(u8, f_z[0..f_z.len]);
} else if (std.mem.eql(u8, arg, "--test-skip")) {
const f_z = arg_it.next() orelse return error.MissingFilter;
try exclude_filters.append(init.gpa, try init.gpa.dupe(u8, f_z[0..f_z.len]));
} else if (std.mem.eql(u8, arg, "--jobs")) {
const j_z = arg_it.next() orelse return error.MissingJobs;
jobs = try parseUsize(j_z[0..j_z.len]);
} else if (std.mem.eql(u8, arg, "--seed")) {
const s_z = arg_it.next() orelse return error.MissingSeed;
seed = try parseU32(s_z[0..s_z.len]);
} else if (std.mem.eql(u8, arg, "--help")) {
printHelp();
return;
} else {
// Ignore unknown args to stay compatible with Zig's test flags.
}
}
defer if (child_test_name) |name| init.gpa.free(name);
defer if (filter) |f| init.gpa.free(f);
defer for (exclude_filters.items) |f| init.gpa.free(f);
if (child_test_name) |name| {
is_child_mode = true;
runSingleTest(name, seed);
return;
}
try runAllTests(init.gpa, init.io, argv0, filter, exclude_filters.items, jobs, seed);
}
fn panicHandler(msg: []const u8, first_trace_addr: ?usize) noreturn {
if (is_child_mode) {
print("{s}\n", .{msg});
std.process.exit(1);
}
std.debug.defaultPanic(msg, first_trace_addr);
}
fn parseUsize(s: []const u8) !usize {
return std.fmt.parseUnsigned(usize, s, 10);
}
fn parseU32(s: []const u8) !u32 {
return std.fmt.parseUnsigned(u32, s, 10);
}
fn printHelp() void {
print(
"Usage: test-runner [--test-filter <str>] [--test-skip <str>] [--jobs <n>] [--seed <n>]\n" ++
" --seed also controls deterministic test ordering in parent mode.\n",
.{},
);
}
fn testGroupKey(name: []const u8) []const u8 {
// Keep tests from the same module adjacent so parallel scheduling does not
// make the live dashboard jump between unrelated groups every line.
const marker = ".test.";
if (std.mem.indexOf(u8, name, marker)) |idx| {
return name[0 .. idx + marker.len];
}
return name;
}
const TestInfo = struct {
name: []const u8,
};
const Status = enum {
pass,
fail,
skip,
leak,
crash,
};
const Summary = struct {
pass: usize = 0,
fail: usize = 0,
skip: usize = 0,
leak: usize = 0,
crash: usize = 0,
};
const Dashboard = struct {
/// One slot per worker thread so the parent process can redraw the live
/// "currently running" view without interleaving worker output.
running: []?[]const u8,
/// Number of lines emitted by the last dashboard render so they can be
/// cleared before printing result lines or the next dashboard frame.
rendered_lines: usize = 0,
};
fn noteStatus(summary: *Summary, status: Status) void {
switch (status) {
.pass => summary.pass += 1,
.fail => summary.fail += 1,
.skip => summary.skip += 1,
.leak => summary.leak += 1,
.crash => summary.crash += 1,
}
}
fn printRunnerError(name: []const u8, err: anyerror) void {
print("\n== TEST {s} ==\nrunner error: {s}\n", .{ name, @errorName(err) });
}
fn runAllTests(
gpa: std.mem.Allocator,
io: std.Io,
argv0: []const u8,
filter: ?[]const u8,
exclude_filters: []const []const u8,
jobs: ?usize,
seed: ?u32,
) !void {
var tests: std.ArrayList(TestInfo) = .empty;
defer tests.deinit(gpa);
for (builtin.test_functions) |t| {
if (filter) |f| {
if (std.mem.indexOf(u8, t.name, f) == null) continue;
}
var excluded = false;
for (exclude_filters) |f| {
if (std.mem.indexOf(u8, t.name, f) != null) {
excluded = true;
break;
}
}
if (excluded) continue;
try tests.append(gpa, .{ .name = t.name });
}
if (tests.items.len == 0) {
print("0 tests selected\n", .{});
return;
}
const GroupBucket = struct {
key: []const u8,
items: std.ArrayList(TestInfo) = .empty,
next: usize = 0,
};
var buckets: std.ArrayList(GroupBucket) = .empty;
defer {
for (buckets.items) |*b| b.items.deinit(gpa);
buckets.deinit(gpa);
}
for (tests.items) |t| {
const key = testGroupKey(t.name);
var found: ?usize = null;
for (buckets.items, 0..) |b, bi| {
if (std.mem.eql(u8, b.key, key)) {
found = bi;
break;
}
}
if (found == null) {
try buckets.append(gpa, .{ .key = key });
found = buckets.items.len - 1;
}
try buckets.items[found.?].items.append(gpa, t);
}
if (seed) |s| {
var prng = std.Random.DefaultPrng.init(@as(u64, s));
var random = prng.random();
shuffleSlice(GroupBucket, &random, buckets.items);
for (buckets.items) |*bucket| {
shuffleSlice(TestInfo, &random, bucket.items.items);
}
}
var reordered: std.ArrayList(TestInfo) = .empty;
defer reordered.deinit(gpa);
try reordered.ensureTotalCapacity(gpa, tests.items.len);
var remaining = tests.items.len;
while (remaining != 0) {
for (buckets.items) |*bucket| {
if (bucket.next >= bucket.items.items.len) continue;
try reordered.append(gpa, bucket.items.items[bucket.next]);
bucket.next += 1;
remaining -= 1;
}
}
@memcpy(tests.items, reordered.items);
const cpu_count = std.Thread.getCpuCount() catch 1;
var job_count = jobs orelse @min(cpu_count, default_job_cap);
if (job_count == 0) job_count = 1;
if (job_count > tests.items.len) job_count = tests.items.len;
var next_index: std.atomic.Value(usize) = .init(0);
var summary: Summary = .{};
var print_mutex: std.Io.Mutex = .init;
var count_mutex: std.Io.Mutex = .init;
const running = try gpa.alloc(?[]const u8, job_count);
defer gpa.free(running);
@memset(running, null);
var dashboard: Dashboard = .{ .running = running };
var ctx = WorkerCtx{
.gpa = gpa,
.io = io,
.argv0 = argv0,
.tests = tests.items,
.seed = seed,
.next_index = &next_index,
.summary = &summary,
.print_mutex = &print_mutex,
.count_mutex = &count_mutex,
.dashboard = &dashboard,
};
if (builtin.single_threaded or job_count == 1) {
worker(&ctx, 0);
} else {
const threads = try gpa.alloc(std.Thread, job_count);
defer gpa.free(threads);
for (threads, 0..) |*t, i| {
t.* = try std.Thread.spawn(.{}, worker, .{ &ctx, i });
}
for (threads) |t| t.join();
}
print_mutex.lockUncancelable(io);
clearDashboardLocked(&ctx);
print_mutex.unlock(io);
print(
"\npass: {d} fail: {d} skip: {d} leak: {d} crash: {d}\n",
.{ summary.pass, summary.fail, summary.skip, summary.leak, summary.crash },
);
if (summary.fail != 0 or summary.crash != 0 or summary.leak != 0) {
std.process.exit(1);
}
}
fn shuffleSlice(comptime T: type, random: *std.Random, items: []T) void {
if (items.len <= 1) return;
var i: usize = items.len - 1;
while (i != 0) {
const j = random.uintLessThan(usize, i + 1);
std.mem.swap(T, &items[i], &items[j]);
i -= 1;
}
}
const WorkerCtx = struct {
gpa: std.mem.Allocator,
io: std.Io,
argv0: []const u8,
tests: []const TestInfo,
seed: ?u32,
next_index: *std.atomic.Value(usize),
summary: *Summary,
print_mutex: *std.Io.Mutex,
count_mutex: *std.Io.Mutex,
/// Shared dashboard state updated by each worker while its child process
/// is running.
dashboard: *Dashboard,
};
fn clearDashboardLocked(ctx: *WorkerCtx) void {
if (ctx.dashboard.rendered_lines == 0) return;
print("\x1b[{d}F", .{ctx.dashboard.rendered_lines});
var i: usize = 0;
while (i < ctx.dashboard.rendered_lines) : (i += 1) {
print("\x1b[2K\n", .{});
}
print("\x1b[{d}F", .{ctx.dashboard.rendered_lines});
ctx.dashboard.rendered_lines = 0;
}
fn renderDashboardLocked(ctx: *WorkerCtx) void {
clearDashboardLocked(ctx);
var rendered: usize = 0;
for (ctx.dashboard.running) |name_opt| {
if (name_opt) |name| {
print("\x1b[33mrunning\x1b[0m {s}\n", .{name});
rendered += 1;
}
}
ctx.dashboard.rendered_lines = rendered;
}
fn worker(ctx: *WorkerCtx, slot: usize) void {
while (true) {
const idx = ctx.next_index.fetchAdd(1, .seq_cst);
if (idx >= ctx.tests.len) break;
const test_name = ctx.tests[idx].name;
ctx.print_mutex.lockUncancelable(ctx.io);
ctx.dashboard.running[slot] = test_name;
renderDashboardLocked(ctx);
ctx.print_mutex.unlock(ctx.io);
const result = runChildTest(ctx, test_name) catch |err| {
ctx.print_mutex.lockUncancelable(ctx.io);
defer ctx.print_mutex.unlock(ctx.io);
clearDashboardLocked(ctx);
ctx.dashboard.running[slot] = null;
printRunnerError(test_name, err);
renderDashboardLocked(ctx);
ctx.count_mutex.lockUncancelable(ctx.io);
noteStatus(ctx.summary, .fail);
ctx.count_mutex.unlock(ctx.io);
continue;
};
ctx.print_mutex.lockUncancelable(ctx.io);
defer ctx.print_mutex.unlock(ctx.io);
defer deinitChildResult(ctx.gpa, result);
clearDashboardLocked(ctx);
ctx.dashboard.running[slot] = null;
printTestOutput(test_name, result);
renderDashboardLocked(ctx);
ctx.count_mutex.lockUncancelable(ctx.io);
noteStatus(ctx.summary, result.status);
ctx.count_mutex.unlock(ctx.io);
}
}
const ChildResult = struct {
status: Status,
term: std.process.Child.Term,
stdout: []u8,
stderr: []u8,
};
fn runChildTest(ctx: *WorkerCtx, test_name: []const u8) !ChildResult {
var argv: std.ArrayList([]const u8) = .empty;
defer argv.deinit(ctx.gpa);
try argv.append(ctx.gpa, ctx.argv0);
try argv.append(ctx.gpa, "--run-test");
try argv.append(ctx.gpa, test_name);
var seed_buf: ?[]u8 = null;
if (ctx.seed) |s| {
const seed_str = try std.fmt.allocPrint(ctx.gpa, "{d}", .{s});
seed_buf = seed_str;
try argv.append(ctx.gpa, "--seed");
try argv.append(ctx.gpa, seed_str);
}
defer if (seed_buf) |b| ctx.gpa.free(b);
const run_result = try std.process.run(ctx.gpa, ctx.io, .{
.argv = argv.items,
.stdout_limit = .limited(256 * 1024),
.stderr_limit = .limited(256 * 1024),
.reserve_amount = 16 * 1024,
});
return .{
.status = classifyStatus(run_result.term),
.term = run_result.term,
.stdout = run_result.stdout,
.stderr = run_result.stderr,
};
}
fn classifyStatus(term: std.process.Child.Term) Status {
switch (term) {
.exited => |code| return switch (code) {
0 => .pass,
2 => .skip,
3 => .leak,
else => .fail,
},
.signal, .stopped, .unknown => return .crash,
}
}
fn printTestOutput(name: []const u8, res: ChildResult) void {
const color = switch (res.status) {
.pass => "\x1b[32m",
.skip => "\x1b[94m",
else => "\x1b[31m",
};
const label = switch (res.status) {
.pass => "ok",
.skip => "skip",
.leak => "leak",
.crash => "crash",
.fail => "error",
};
print("{s}{s}\x1b[0m {s}", .{ color, label, name });
switch (res.term) {
.exited => |code| if (code != 0) print(" | exit {d}", .{code}),
.signal => |sig| print(" | signal {d} ({s})", .{ @intFromEnum(sig), @tagName(sig) }),
.stopped => |code| print(" | stopped {d}", .{code}),
.unknown => |code| print(" | unknown {d}", .{code}),
}
print("\n", .{});
if (res.stderr.len != 0 and res.status != .pass and res.status != .skip) {
print("stderr:\n{s}", .{res.stderr});
if (res.stderr[res.stderr.len - 1] != '\n') print("\n", .{});
}
if (res.stdout.len != 0 and res.status != .pass and res.status != .skip) {
print("stdout:\n{s}", .{res.stdout});
if (res.stdout[res.stdout.len - 1] != '\n') print("\n", .{});
}
}
fn deinitChildResult(gpa: std.mem.Allocator, res: ChildResult) void {
gpa.free(res.stdout);
gpa.free(res.stderr);
}
fn runSingleTest(name: []const u8, seed: ?u32) void {
if (seed) |s| std.testing.random_seed = s;
const test_fn = findTest(name) orelse {
print("unknown test: {s}\n", .{name});
std.process.exit(1);
};
std.testing.allocator_instance = .{};
const result = test_fn.func();
const leak_status = std.testing.allocator_instance.deinit();
if (leak_status == .leak) {
print("memory leak\n", .{});
std.process.exit(3);
}
if (result) |_| {
std.process.exit(0);
} else |err| switch (err) {
error.SkipZigTest => std.process.exit(2),
else => {
print("{s}\n", .{@errorName(err)});
std.process.exit(1);
},
}
}
const TestFn = std.meta.Elem(@TypeOf(builtin.test_functions));
fn findTest(name: []const u8) ?TestFn {
for (builtin.test_functions) |t| {
if (std.mem.eql(u8, t.name, name)) return t;
}
return null;
}