From 750998eef62a70dbea8c3ba1d44245f506370911 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Fri, 25 Aug 2023 14:59:10 -0400 Subject: [PATCH] Build: fail tests that log errors, like `zig test` does --- lib/std/Build/Step.zig | 3 ++- lib/std/Build/Step/Run.zig | 20 +++++++++++++++----- lib/std/zig/Server.zig | 5 ++--- lib/test_runner.zig | 7 ++++++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig index 475fc82e1a..13b19f4750 100644 --- a/lib/std/Build/Step.zig +++ b/lib/std/Build/Step.zig @@ -45,10 +45,11 @@ pub const TestResults = struct { fail_count: u32 = 0, skip_count: u32 = 0, leak_count: u32 = 0, + log_err_count: u32 = 0, test_count: u32 = 0, pub fn isSuccess(tr: TestResults) bool { - return tr.fail_count == 0 and tr.leak_count == 0; + return tr.fail_count == 0 and tr.leak_count == 0 and tr.log_err_count == 0; } pub fn passCount(tr: TestResults) u32 { diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 02489782b2..8659622f54 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -1048,6 +1048,7 @@ fn evalZigTest( var skip_count: u32 = 0; var leak_count: u32 = 0; var test_count: u32 = 0; + var log_err_count: u32 = 0; var metadata: ?TestMetadata = null; @@ -1112,14 +1113,22 @@ fn evalZigTest( const TrHdr = std.zig.Server.Message.TestResults; const tr_hdr = @as(*align(1) const TrHdr, @ptrCast(body)); - fail_count += @intFromBool(tr_hdr.flags.fail); - skip_count += @intFromBool(tr_hdr.flags.skip); - leak_count += @intFromBool(tr_hdr.flags.leak); + fail_count +|= @intFromBool(tr_hdr.flags.fail); + skip_count +|= @intFromBool(tr_hdr.flags.skip); + leak_count +|= @intFromBool(tr_hdr.flags.leak); + log_err_count +|= tr_hdr.flags.log_err_count; - if (tr_hdr.flags.fail or tr_hdr.flags.leak) { + if (tr_hdr.flags.fail or tr_hdr.flags.leak or tr_hdr.flags.log_err_count > 0) { const name = std.mem.sliceTo(md.string_bytes[md.names[tr_hdr.index]..], 0); const msg = std.mem.trim(u8, stderr.readableSlice(0), "\n"); - const label = if (tr_hdr.flags.fail) "failed" else "leaked"; + const label = if (tr_hdr.flags.fail) + "failed" + else if (tr_hdr.flags.leak) + "leaked" + else if (tr_hdr.flags.log_err_count > 0) + "logged errors" + else + unreachable; if (msg.len > 0) { try self.step.addError("'{s}' {s}: {s}", .{ name, label, msg }); } else { @@ -1153,6 +1162,7 @@ fn evalZigTest( .fail_count = fail_count, .skip_count = skip_count, .leak_count = leak_count, + .log_err_count = log_err_count, }, .test_metadata = metadata, }; diff --git a/lib/std/zig/Server.zig b/lib/std/zig/Server.zig index 468219f8f0..15795176a0 100644 --- a/lib/std/zig/Server.zig +++ b/lib/std/zig/Server.zig @@ -53,12 +53,11 @@ pub const Message = struct { index: u32, flags: Flags, - pub const Flags = packed struct(u8) { + pub const Flags = packed struct(u32) { fail: bool, skip: bool, leak: bool, - - reserved: u5 = 0, + log_err_count: u29 = 0, }; }; diff --git a/lib/test_runner.zig b/lib/test_runner.zig index 0b7ee24fde..18608d298a 100644 --- a/lib/test_runner.zig +++ b/lib/test_runner.zig @@ -90,6 +90,7 @@ fn mainServer() !void { .run_test => { std.testing.allocator_instance = .{}; + log_err_count = 0; const index = try server.receiveBody_u32(); const test_fn = builtin.test_functions[index]; if (test_fn.async_frame_size != null) @@ -113,6 +114,10 @@ fn mainServer() !void { .fail = fail, .skip = skip, .leak = leak, + .log_err_count = std.math.lossyCast(std.meta.FieldType( + std.zig.Server.Message.TestResults.Flags, + .log_err_count, + ), log_err_count), }, }); }, @@ -218,7 +223,7 @@ pub fn log( args: anytype, ) void { if (@intFromEnum(message_level) <= @intFromEnum(std.log.Level.err)) { - log_err_count += 1; + log_err_count +|= 1; } if (@intFromEnum(message_level) <= @intFromEnum(std.testing.log_level)) { std.debug.print(