Skip to content

Commit e4796b1

Browse files
committed
test_runner: enable testing panics in mainTerminal
The user can use std.testing.spawnExpectPanic() in a test to spawn a child process, which must panic or the test fails. Internally, - 1. is_panic_parentproc is set from the cli arguments for simple reproduction of both test spawn and panic behavior, - 2. panic_msg is set as threadlocal, if comptime-detectable capabilities exist, to enable multithreaded processing and user-customized messages, - 3. error.SpawnZigTest is returned to the test_runner.zig - 4. the test_runner spawns a child_process on correct usage - 5. the child_process expected to panic executes only one test block This means, that only one @Panic is possible within a test block and that no follow-up code after the @Panic in the test block can be run. This commit does not add the panic test capability to the server yet, since there are open design questions how many processes should be spawned at the same time and how to manage time quotas to prevent unnecessary slowdowns. Supersedes ziglang#14351. Work on ziglang#1356.
1 parent 629f0d2 commit e4796b1

File tree

5 files changed

+254
-15
lines changed

5 files changed

+254
-15
lines changed

lib/std/child_process.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ pub const ChildProcess = struct {
240240
}
241241

242242
/// Blocks until child process terminates and then cleans up all resources.
243+
/// In case of error, the caller is responsible to clean up the ressources
244+
/// via calling `self.cleanupStreams()`.
245+
/// TODO: This describes the current state. Is this intended?
243246
pub fn wait(self: *ChildProcess) !Term {
244247
const term = if (builtin.os.tag == .windows)
245248
try self.waitWindows()
@@ -312,7 +315,7 @@ pub const ChildProcess = struct {
312315
};
313316

314317
/// Spawns a child process, waits for it, collecting stdout and stderr, and then returns.
315-
/// If it succeeds, the caller owns result.stdout and result.stderr memory.
318+
/// If spawning succeeds, then the caller owns result.stdout and result.stderr memory.
316319
pub fn exec(args: struct {
317320
allocator: mem.Allocator,
318321
argv: []const []const u8,
@@ -415,7 +418,7 @@ pub const ChildProcess = struct {
415418
self.term = self.cleanupAfterWait(status);
416419
}
417420

418-
fn cleanupStreams(self: *ChildProcess) void {
421+
pub fn cleanupStreams(self: *ChildProcess) void {
419422
if (self.stdin) |*stdin| {
420423
stdin.close();
421424
self.stdin = null;

lib/std/std.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ pub const options = struct {
196196
options_override.side_channels_mitigations
197197
else
198198
crypto.default_side_channels_mitigations;
199+
200+
/// Default thread-local storage panic message size used for panic tests.
201+
pub const testing_max_panic_msg_size = 100;
199202
};
200203

201204
// This forces the start.zig file to be imported, and the comptime logic inside that

lib/std/testing.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,37 @@ test "expectEqualDeep composite type" {
916916
}
917917
}
918918

919+
pub const can_panic_test = builtin.is_test and !builtin.single_threaded and std.process.can_spawn;
920+
921+
/// Static storage to support user-generated panic messages
922+
/// Parent process writes these, returns to the test execution loop and spawns,
923+
/// child process ignores these.
924+
const TestFn_iT = if (can_panic_test) ?[std.options.testing_max_panic_msg_size:0]u8 else void;
925+
pub threadlocal var panic_msg: TestFn_iT = if (can_panic_test) null else {};
926+
927+
/// Distinguishes between parent and child, if panics are tested for.
928+
/// TODO: is_panic_parentproc and panic_msg feels like it belongs into test api to
929+
/// allow implementations providing their own way to prevent the necessity to use tls.
930+
pub var is_panic_parentproc: if (can_panic_test) bool else void = if (can_panic_test) true else {};
931+
932+
/// To be used for panic tests after test block declaration.
933+
pub fn spawnExpectPanic(msg: []const u8) error{ SpawnZigTest, SkipZigTest }!void {
934+
std.debug.assert(can_panic_test); // Caller is responsible to check.
935+
if (is_panic_parentproc) {
936+
if (panic_msg == null) {
937+
panic_msg = .{undefined} ** std.options.testing_max_panic_msg_size;
938+
@memcpy(panic_msg.?[0..msg.len], msg); // Message must be persistent, not stack-local.
939+
panic_msg.?[msg.len] = 0; // 0-sentinel for the len without separate field
940+
return error.SpawnZigTest; // Test will be run in separate process
941+
} else {
942+
@panic("std.testing.panic_msg must be only used in spawnExpectPanic");
943+
}
944+
} else {
945+
std.debug.assert(panic_msg == null);
946+
// panic runner continues running the test block
947+
}
948+
}
949+
919950
fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
920951
const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin|
921952
line_begin + 1

lib/std/zig/Server.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub const Message = struct {
4242
/// - 0 means not async
4343
/// * expected_panic_msg: [tests_len]u32,
4444
/// - null-terminated string_bytes index
45-
/// - 0 means does not expect pani
45+
/// - 0 means does not expect panic
4646
/// * string_bytes: [string_bytes_len]u8,
4747
pub const TestMetadata = extern struct {
4848
string_bytes_len: u32,

lib/test_runner.zig

Lines changed: 214 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ var log_err_count: usize = 0;
1111
var cmdline_buffer: [4096]u8 = undefined;
1212
var fba = std.heap.FixedBufferAllocator.init(&cmdline_buffer);
1313

14+
const Mode = enum {
15+
listen,
16+
terminal,
17+
panic_test,
18+
};
19+
20+
fn callError(args: [][:0]u8) noreturn {
21+
std.debug.print("invalid cli arguments:\n", .{});
22+
for (args) |arg| {
23+
std.debug.print("{s} ", .{arg});
24+
}
25+
std.debug.print("\n", .{});
26+
@panic("call error");
27+
}
28+
1429
pub fn main() void {
1530
if (builtin.zig_backend == .stage2_aarch64) {
1631
return mainSimple() catch @panic("test failure");
@@ -19,20 +34,33 @@ pub fn main() void {
1934
const args = std.process.argsAlloc(fba.allocator()) catch
2035
@panic("unable to parse command line args");
2136

22-
var listen = false;
37+
var i: u32 = 1;
38+
var test_i: ?u64 = null;
39+
var mode: Mode = .terminal;
2340

24-
for (args[1..]) |arg| {
25-
if (std.mem.eql(u8, arg, "--listen=-")) {
26-
listen = true;
41+
while (i < args.len) : (i += 1) {
42+
if (std.mem.eql(u8, args[i], "--listen=-")) {
43+
mode = .listen;
44+
} else if (std.mem.eql(u8, args[i], "--test_panic_index")) {
45+
i += 1;
46+
if (i < args.len) {
47+
test_i = std.fmt.parseInt(u64, args[i], 10) catch {
48+
callError(args);
49+
};
50+
mode = .panic_test;
51+
std.testing.is_panic_parentproc = false;
52+
} else {
53+
callError(args);
54+
}
2755
} else {
28-
@panic("unrecognized command line argument");
56+
callError(args);
2957
}
3058
}
3159

32-
if (listen) {
33-
return mainServer() catch @panic("internal test runner failure");
34-
} else {
35-
return mainTerminal();
60+
switch (mode) {
61+
.listen => return mainServer() catch @panic("internal test runner failure"),
62+
.terminal => return mainTerminal(args),
63+
.panic_test => return panicTest(test_i.?),
3664
}
3765
}
3866

@@ -124,7 +152,18 @@ fn mainServer() !void {
124152
}
125153
}
126154

127-
fn mainTerminal() void {
155+
// TODO
156+
// - [ ] has test_i:
157+
// * spawn and compare specific function
158+
// * compare result: if returning from execution => @panic("FoundNoPanicInTest");
159+
// - [ ] not test_i:
160+
// * iterate through all functions
161+
// * compare result: compare execution result with special case for panic msg "FoundNoPanicInTest"
162+
163+
fn mainTerminal(args: [][:0]const u8) void {
164+
var test_i_buf: [20]u8 = undefined;
165+
// TODO make environment buffer size configurable and use a sane default
166+
// Tradeoff: waste stack space or allocate on every panic test
128167
const test_fn_list = builtin.test_functions;
129168
var ok_count: usize = 0;
130169
var skip_count: usize = 0;
@@ -140,7 +179,6 @@ fn mainTerminal() void {
140179
// TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly
141180
// ignores the alignment of the slice.
142181
async_frame_buffer = &[_]u8{};
143-
144182
var leaks: usize = 0;
145183
for (test_fn_list, 0..) |test_fn, i| {
146184
std.testing.allocator_instance = .{};
@@ -183,9 +221,134 @@ fn mainTerminal() void {
183221
progress.log("SKIP\n", .{});
184222
test_node.end();
185223
},
224+
error.SpawnZigTest => {
225+
progress.log("error.SpawnZigTest\n", .{});
226+
if (!std.testing.can_panic_test)
227+
@panic("Found error.SpawnZigTest without panic test capabilities.");
228+
if (std.testing.panic_msg == null)
229+
@panic("Panic test expects `panic_msg` to be set. Use std.testing.spawnExpectPanic().");
230+
231+
const test_i_written = std.fmt.bufPrint(&test_i_buf, "{d}", .{i}) catch unreachable;
232+
var child_proc = std.ChildProcess.init(
233+
&.{ args[0], "--test_panic_index", test_i_written },
234+
std.testing.allocator,
235+
);
236+
progress.log("spawning '{s} {s} {s}'\n", .{ args[0], "--test_panic_index", test_i_written });
237+
238+
child_proc.stdin_behavior = .Ignore;
239+
child_proc.stdout_behavior = .Pipe;
240+
child_proc.stderr_behavior = .Pipe;
241+
child_proc.spawn() catch |spawn_err| {
242+
progress.log("FAIL spawn ({s})\n", .{@errorName(spawn_err)});
243+
fail_count += 1;
244+
test_node.end();
245+
continue;
246+
};
247+
248+
var stdout = std.ArrayList(u8).init(std.testing.allocator);
249+
defer stdout.deinit();
250+
var stderr = std.ArrayList(u8).init(std.testing.allocator);
251+
defer stderr.deinit();
252+
// child_process.zig: max_output_bytes: usize = 50 * 1024,
253+
child_proc.collectOutput(&stdout, &stderr, 50 * 1024) catch |collect_err| {
254+
progress.log("FAIL collect ({s})\n", .{@errorName(collect_err)});
255+
fail_count += 1;
256+
test_node.end();
257+
continue;
258+
};
259+
const term = child_proc.wait() catch |wait_err| {
260+
child_proc.cleanupStreams();
261+
progress.log("FAIL wait_error (exit_status: {d})\n", .{@errorName(wait_err)});
262+
fail_count += 1;
263+
test_node.end();
264+
continue;
265+
};
266+
switch (term) {
267+
.Exited => |code| {
268+
progress.log("FAIL term exited, status: {})\nstdout: ({s})\nstderr: ({s})\n", .{ code, stdout.items, stderr.items });
269+
fail_count += 1;
270+
test_node.end();
271+
continue;
272+
},
273+
.Signal => |code| {
274+
progress.log("Signal: {d}\n", .{code});
275+
// assert: panic message format: 'XYZ thread thread_id panic: msg'
276+
// Any signal can be returned on panic, if a custom signal
277+
// or panic handler was installed as part of the unit test.
278+
var pos_eol: usize = 0;
279+
var found_eol: bool = false;
280+
while (pos_eol < stderr.items.len) : (pos_eol += 1) {
281+
if (stderr.items[pos_eol] == '\n') {
282+
found_eol = true;
283+
break;
284+
}
285+
}
286+
287+
if (!found_eol) {
288+
progress.log("FAIL no end of line in panic format\nstdout: ({s})\nstderr: ({s})\n", .{ stdout.items, stderr.items });
289+
fail_count += 1;
290+
test_node.end();
291+
continue;
292+
}
293+
294+
var it = std.mem.tokenize(u8, stderr.items[0..pos_eol], " ");
295+
var parsed_panic_msg = false;
296+
while (it.next()) |word| { // 'thread thread_id panic: msg'
297+
if (!std.mem.eql(u8, word, "thread")) continue;
298+
const thread_id = it.next();
299+
if (thread_id == null) continue;
300+
_ = std.fmt.parseInt(u64, thread_id.?, 10) catch continue;
301+
const panic_txt = it.next();
302+
if (panic_txt == null) continue;
303+
if (!std.mem.eql(u8, panic_txt.?, "panic:")) continue;
304+
const panic_msg = it.next();
305+
if (panic_msg == null) continue;
306+
const panic_msg_start = it.index - panic_msg.?.len;
307+
const len_exp_panic_msg = std.mem.len(@as([*:0]u8, std.testing.panic_msg.?[0..]));
308+
const expected_panic_msg = std.testing.panic_msg.?[0..len_exp_panic_msg];
309+
const panic_msg_end = panic_msg_start + expected_panic_msg.len;
310+
if (panic_msg_end > pos_eol) break;
311+
312+
parsed_panic_msg = true;
313+
const current_panic_msg = stderr.items[panic_msg_start..panic_msg_end];
314+
315+
if (!std.mem.eql(u8, "SKIP (async test)", current_panic_msg) and !std.mem.eql(u8, expected_panic_msg, current_panic_msg)) {
316+
progress.log("FAIL expected_panic_msg: '{s}', got: '{s}'\n", .{ expected_panic_msg, current_panic_msg });
317+
std.testing.panic_msg = null;
318+
fail_count += 1;
319+
test_node.end();
320+
break;
321+
}
322+
std.testing.panic_msg = null;
323+
ok_count += 1;
324+
test_node.end();
325+
if (!have_tty) std.debug.print("OK\n", .{});
326+
break;
327+
}
328+
if (!parsed_panic_msg) {
329+
progress.log("FAIL invalid panic_msg format expect 'XYZ thread thread_id panic: msg'\nstdout: ({s})\nstderr: ({s})\n", .{ stdout.items, stderr.items });
330+
fail_count += 1;
331+
test_node.end();
332+
continue;
333+
}
334+
},
335+
.Stopped => |code| {
336+
fail_count += 1;
337+
progress.log("FAIL stopped, status: ({d})\nstdout: ({s})\nstderr: ({s})\n", .{ code, stdout.items, stderr.items });
338+
test_node.end();
339+
continue;
340+
},
341+
.Unknown => |code| {
342+
fail_count += 1;
343+
progress.log("FAIL unknown, status: ({d})\nstdout: ({s})\nstderr: ({s})\n", .{ code, stdout.items, stderr.items });
344+
test_node.end();
345+
continue;
346+
},
347+
}
348+
},
186349
else => {
187350
fail_count += 1;
188-
progress.log("FAIL ({s})\n", .{@errorName(err)});
351+
progress.log("FAIL unexpected error ({s})\n", .{@errorName(err)});
189352
if (@errorReturnTrace()) |trace| {
190353
std.debug.dumpStackTrace(trace.*);
191354
}
@@ -210,6 +373,45 @@ fn mainTerminal() void {
210373
}
211374
}
212375

376+
fn panicTest(test_i: u64) void {
377+
const test_fn_list = builtin.test_functions;
378+
var async_frame_buffer: []align(std.Target.stack_align) u8 = undefined;
379+
// TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly
380+
// ignores the alignment of the slice.
381+
async_frame_buffer = &[_]u8{};
382+
{
383+
std.testing.allocator_instance = .{};
384+
// custom panic handler to restore to save state and prevent memory
385+
// leakage is out of scope, so ignore memory leaks
386+
defer {
387+
if (std.testing.allocator_instance.deinit() == .leak) {
388+
@panic("internal test runner memory leak");
389+
}
390+
}
391+
std.testing.log_level = .warn;
392+
const result = if (test_fn_list[test_i].async_frame_size) |size| switch (std.options.io_mode) {
393+
.evented => blk: {
394+
if (async_frame_buffer.len < size) {
395+
std.heap.page_allocator.free(async_frame_buffer);
396+
async_frame_buffer = std.heap.page_allocator.alignedAlloc(u8, std.Target.stack_align, size) catch @panic("out of memory");
397+
}
398+
const casted_fn = @ptrCast(fn () callconv(.Async) anyerror!void, test_fn_list[test_i].func);
399+
break :blk await @asyncCall(async_frame_buffer, {}, casted_fn, .{});
400+
},
401+
.blocking => @panic("SKIP (async test)"),
402+
} else test_fn_list[test_i].func();
403+
404+
if (result) {
405+
std.os.exit(0);
406+
} else |err| {
407+
std.debug.print("FAIL unexpected error ({s})\n", .{@errorName(err)});
408+
if (@errorReturnTrace()) |trace| {
409+
std.debug.dumpStackTrace(trace.*);
410+
}
411+
}
412+
}
413+
}
414+
213415
pub fn log(
214416
comptime message_level: std.log.Level,
215417
comptime scope: @Type(.EnumLiteral),

0 commit comments

Comments
 (0)