Skip to content

Commit cf2797b

Browse files
committed
accomodate zig test runner changes
1 parent 82cd4c1 commit cf2797b

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

build.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn build(b: *std.Build) !void {
3434
.root_source_file = b.path("src/metrics.zig"),
3535
.target = target,
3636
.optimize = optimize,
37-
.test_runner = b.path("test_runner.zig"),
37+
.test_runner = .{.path = b.path("test_runner.zig"), .mode = .simple},
3838
});
3939
const run_test = b.addRunArtifact(lib_test);
4040
run_test.has_side_effects = true;

test_runner.zig

+20-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// const tests = b.addTest(.{
33
// .target = target,
44
// .optimize = optimize,
5-
// .test_runner = "test_runner.zig", // add this line
5+
// .test_runner = .{ .path = b.path("test_runner.zig"), .mode = .simple }, // add this line
66
// .root_source_file = b.path("src/main.zig"),
77
// });
88

@@ -33,14 +33,11 @@ pub fn main() !void {
3333
var skip: usize = 0;
3434
var leak: usize = 0;
3535

36-
try std.posix.getrandom(std.mem.asBytes(&std.testing.random_seed));
37-
3836
const printer = Printer.init();
3937
printer.fmt("\r\x1b[0K", .{}); // beginning of line and clear to end of line
4038

4139
for (builtin.test_functions) |t| {
4240
if (isSetup(t)) {
43-
current_test = friendlyName(t.name);
4441
t.func() catch |err| {
4542
printer.status(.fail, "\nsetup \"{s}\" failed: {}\n", .{ t.name, err });
4643
return err;
@@ -63,16 +60,23 @@ pub fn main() !void {
6360
}
6461
}
6562

66-
const friendly_name = friendlyName(t.name);
63+
const friendly_name = blk: {
64+
const name = t.name;
65+
var it = std.mem.splitScalar(u8, name, '.');
66+
while (it.next()) |value| {
67+
if (std.mem.eql(u8, value, "test")) {
68+
const rest = it.rest();
69+
break :blk if (rest.len > 0) rest else name;
70+
}
71+
}
72+
break :blk name;
73+
};
74+
6775
current_test = friendly_name;
6876
std.testing.allocator_instance = .{};
6977
const result = t.func();
7078
current_test = null;
7179

72-
if (is_unnamed_test) {
73-
continue;
74-
}
75-
7680
const ns_taken = slowest.endTiming(friendly_name);
7781

7882
if (std.testing.allocator_instance.deinit() == .leak) {
@@ -110,7 +114,6 @@ pub fn main() !void {
110114

111115
for (builtin.test_functions) |t| {
112116
if (isTeardown(t)) {
113-
current_test = friendlyName(t.name);
114117
t.func() catch |err| {
115118
printer.status(.fail, "\nteardown \"{s}\" failed: {}\n", .{ t.name, err });
116119
return err;
@@ -133,17 +136,6 @@ pub fn main() !void {
133136
std.posix.exit(if (fail == 0) 0 else 1);
134137
}
135138

136-
fn friendlyName(name: []const u8) []const u8 {
137-
var it = std.mem.splitScalar(u8, name, '.');
138-
while (it.next()) |value| {
139-
if (std.mem.eql(u8, value, "test")) {
140-
const rest = it.rest();
141-
return if (rest.len > 0) rest else name;
142-
}
143-
}
144-
return name;
145-
}
146-
147139
const Printer = struct {
148140
out: std.fs.File.Writer,
149141

@@ -290,12 +282,14 @@ const Env = struct {
290282
}
291283
};
292284

293-
pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn {
294-
if (current_test) |ct| {
295-
std.debug.print("\x1b[31m{s}\npanic running \"{s}\"\n{s}\x1b[0m\n", .{ BORDER, ct, BORDER });
285+
pub const panic = std.debug.FullPanic(struct {
286+
pub fn panicFn(msg: []const u8, first_trace_addr: ?usize) noreturn {
287+
if (current_test) |ct| {
288+
std.debug.print("\x1b[31m{s}\npanic running \"{s}\"\n{s}\x1b[0m\n", .{ BORDER, ct, BORDER });
289+
}
290+
std.debug.defaultPanic(msg, first_trace_addr);
296291
}
297-
std.debug.defaultPanic(msg, error_return_trace, ret_addr);
298-
}
292+
}.panicFn);
299293

300294
fn isUnnamed(t: std.builtin.TestFn) bool {
301295
const marker = ".test_";

0 commit comments

Comments
 (0)