Commit ae0f12885b

dweiller <4678790+dweiller@users.noreplay.github.com>
2022-11-02 12:41:20
test: add test_runner_path test
1 parent 33a401d
Changed files (4)
test
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);
+}
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;
+}
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);
+    }
+}
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", .{});