diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-11-18 14:47:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-18 14:47:21 +0200 |
| commit | 8082323dfd65c20328991d9a0d6740b779b26670 (patch) | |
| tree | e689d430ba34ad788499fa85dff7ad4552dcc77e /test | |
| parent | 5f6f38ff3160c75c03ce0477904051e6d8af0c80 (diff) | |
| parent | a1b123bccbc2ae50442fcc1544b0da0595401038 (diff) | |
| download | zig-8082323dfd65c20328991d9a0d6740b779b26670.tar.gz zig-8082323dfd65c20328991d9a0d6740b779b26670.zip | |
Merge pull request #13411 from dweiller/custom-test-runner
Custom test runner
Diffstat (limited to 'test')
| -rw-r--r-- | test/standalone.zig | 1 | ||||
| -rw-r--r-- | test/standalone/test_runner_path/build.zig | 11 | ||||
| -rw-r--r-- | test/standalone/test_runner_path/test.zig | 9 | ||||
| -rw-r--r-- | test/standalone/test_runner_path/test_runner.zig | 52 |
4 files changed, 73 insertions, 0 deletions
diff --git a/test/standalone.zig b/test/standalone.zig index d235e491ef..30e517cc0d 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -15,6 +15,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.add("test/standalone/main_return_error/error_u8_non_zero.zig"); cases.add("test/standalone/noreturn_call/inline.zig"); cases.add("test/standalone/noreturn_call/as_arg.zig"); + cases.addBuildFile("test/standalone/test_runner_path/build.zig", .{ .requires_stage2 = true }); cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{}); cases.addBuildFile("test/standalone/shared_library/build.zig", .{}); cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{}); diff --git a/test/standalone/test_runner_path/build.zig b/test/standalone/test_runner_path/build.zig new file mode 100644 index 0000000000..738cac9783 --- /dev/null +++ b/test/standalone/test_runner_path/build.zig @@ -0,0 +1,11 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const test_exe = b.addTestExe("test", "test.zig"); + test_exe.test_runner = "test_runner.zig"; + + const test_run = test_exe.run(); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&test_run.step); +} diff --git a/test/standalone/test_runner_path/test.zig b/test/standalone/test_runner_path/test.zig new file mode 100644 index 0000000000..52f3c693bc --- /dev/null +++ b/test/standalone/test_runner_path/test.zig @@ -0,0 +1,9 @@ +test "test runner path pass" {} + +test "test runner path fail" { + return error.Fail; +} + +test "test runner path skip" { + return error.SkipZigTest; +} diff --git a/test/standalone/test_runner_path/test_runner.zig b/test/standalone/test_runner_path/test_runner.zig new file mode 100644 index 0000000000..f49ff55cae --- /dev/null +++ b/test/standalone/test_runner_path/test_runner.zig @@ -0,0 +1,52 @@ +const std = @import("std"); +const io = std.io; +const builtin = @import("builtin"); + +pub const io_mode: io.Mode = builtin.test_io_mode; + +pub fn main() void { + const test_fn_list = builtin.test_functions; + var ok_count: usize = 0; + var skip_count: usize = 0; + var fail_count: usize = 0; + + var async_frame_buffer: []align(std.Target.stack_align) u8 = undefined; + // TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly + // ignores the alignment of the slice. + async_frame_buffer = &[_]u8{}; + + for (test_fn_list) |test_fn| { + const result = if (test_fn.async_frame_size) |size| switch (io_mode) { + .evented => blk: { + if (async_frame_buffer.len < size) { + std.heap.page_allocator.free(async_frame_buffer); + async_frame_buffer = std.heap.page_allocator.alignedAlloc(u8, std.Target.stack_align, size) catch @panic("out of memory"); + } + const casted_fn = @ptrCast(fn () callconv(.Async) anyerror!void, test_fn.func); + break :blk await @asyncCall(async_frame_buffer, {}, casted_fn, .{}); + }, + .blocking => { + skip_count += 1; + continue; + }, + } else test_fn.func(); + if (result) |_| { + ok_count += 1; + } else |err| switch (err) { + error.SkipZigTest => { + skip_count += 1; + }, + else => { + fail_count += 1; + }, + } + } + if (ok_count == test_fn_list.len) { + std.debug.print("All {d} tests passed.\n", .{ok_count}); + } else { + std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count }); + } + if (ok_count != 1 or skip_count != 1 or fail_count != 1) { + std.process.exit(1); + } +} |
