-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.zig
More file actions
423 lines (361 loc) · 11.8 KB
/
test_runner.zig
File metadata and controls
423 lines (361 loc) · 11.8 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
const std = @import("std");
const builtin = @import("builtin");
pub const panic = std.debug.FullPanic(panicHandler);
var is_child_mode: bool = false;
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 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, "--zhttp-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, "--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.
}
}
if (child_test_name) |name| {
is_child_mode = true;
defer init.gpa.free(name);
runSingleTest(name, seed);
return;
}
if (filter) |f| {
defer init.gpa.free(f);
}
try runAllTests(init.gpa, init.io, argv0, filter, jobs, seed);
}
fn panicHandler(msg: []const u8, first_trace_addr: ?usize) noreturn {
if (is_child_mode) {
std.debug.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 {
std.debug.print(
"Usage: test-runner [--test-filter <str>] [--jobs <n>] [--seed <n>]\n",
.{},
);
}
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,
};
fn runAllTests(
gpa: std.mem.Allocator,
io: std.Io,
argv0: []const u8,
filter: ?[]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;
}
try tests.append(gpa, .{ .name = t.name });
}
if (tests.items.len == 0) {
std.debug.print("0 tests selected\n", .{});
return;
}
const cpu_count = std.Thread.getCpuCount() catch 1;
var job_count = jobs orelse cpu_count;
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;
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,
};
if (builtin.single_threaded or job_count == 1) {
worker(&ctx);
} else {
const threads = try gpa.alloc(std.Thread, job_count);
defer gpa.free(threads);
for (threads, 0..) |*t, i| {
_ = i;
t.* = try std.Thread.spawn(.{}, worker, .{&ctx});
}
for (threads) |t| t.join();
}
std.debug.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);
}
}
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,
};
fn worker(ctx: *WorkerCtx) 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;
const result = runChildTest(ctx, test_name) catch |err| {
ctx.print_mutex.lockUncancelable(ctx.io);
defer ctx.print_mutex.unlock(ctx.io);
std.debug.print("\n== TEST {s} ==\nrunner error: {s}\n", .{ test_name, @errorName(err) });
ctx.count_mutex.lockUncancelable(ctx.io);
ctx.summary.fail += 1;
ctx.count_mutex.unlock(ctx.io);
continue;
};
defer ctx.gpa.free(result.stdout);
defer ctx.gpa.free(result.stderr);
ctx.print_mutex.lockUncancelable(ctx.io);
defer ctx.print_mutex.unlock(ctx.io);
printTestOutput(test_name, result);
ctx.count_mutex.lockUncancelable(ctx.io);
switch (result.status) {
.pass => ctx.summary.pass += 1,
.fail => ctx.summary.fail += 1,
.skip => ctx.summary.skip += 1,
.leak => ctx.summary.leak += 1,
.crash => ctx.summary.crash += 1,
}
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, "--zhttp-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 result = try std.process.run(ctx.gpa, ctx.io, .{
.argv = argv.items,
.stdout_limit = .limited(4 * 1024 * 1024),
.stderr_limit = .limited(4 * 1024 * 1024),
});
const status = classifyStatus(result.term);
return .{
.status = status,
.term = result.term,
.stdout = result.stdout,
.stderr = 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",
};
std.debug.print("{s}{s}\x1b[0m {s}", .{ color, label, name });
if (res.stdout.len > 0) {
std.debug.print(" | out: ", .{});
printSingleLine(res.stdout, 200);
}
if (res.stderr.len > 0) {
std.debug.print(" | err: ", .{});
printSingleLine(res.stderr, 200);
}
switch (res.term) {
.exited => |code| if (code != 0) std.debug.print(" | exit {d}", .{code}),
.signal => |sig| std.debug.print(" | signal {d}", .{@intFromEnum(sig)}),
.stopped => |code| std.debug.print(" | stopped {d}", .{code}),
.unknown => |code| std.debug.print(" | unknown {d}", .{code}),
}
std.debug.print("\n", .{});
}
fn printSingleLine(bytes: []const u8, max_len: usize) void {
var written: usize = 0;
for (bytes) |c| {
if (written >= max_len) break;
switch (c) {
'\n', '\r', '\t' => {
std.debug.print(" ", .{});
written += 1;
},
else => {
std.debug.print("{c}", .{c});
written += 1;
},
}
}
if (bytes.len > max_len) std.debug.print("...", .{});
}
fn runSingleTest(name: []const u8, seed: ?u32) void {
if (seed) |s| std.testing.random_seed = s;
const test_fn = findTest(name) orelse {
std.debug.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) {
std.debug.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 => {
std.debug.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;
}