Skip to content

Better message when a test fails #15520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/std/Build/Step/Run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ fn evalZigTest(
const md = metadata.?;

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);
Expand All @@ -1120,20 +1121,32 @@ fn evalZigTest(

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 err = body[@sizeOf(TrHdr)..][0..tr_hdr.err_len];
const msg = std.mem.trim(u8, stderr.readableSlice(0), "\n");
const label = if (tr_hdr.flags.fail)
"failed"

const label = if (tr_hdr.flags.fail and tr_hdr.flags.leak)
"leaked and failed"
else if (tr_hdr.flags.leak)
"leaked"
else if (tr_hdr.flags.fail)
"failed"
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 });
if (err.len > 0)
try self.step.addError("'{s}' {s} with error {s}:\n{s}", .{ name, label, err, msg })
else
try self.step.addError("'{s}' {s}:\n{s}", .{ name, label, msg });
} else {
try self.step.addError("'{s}' {s}", .{ name, label });
if (err.len > 0)
try self.step.addError("'{s}' {s} with error {s}", .{ name, label, err })
else
try self.step.addError("'{s}' {s}", .{ name, label });
}

stderr.discard(msg.len);
}

Expand Down
19 changes: 14 additions & 5 deletions lib/std/zig/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ pub const Message = struct {
/// - 0 means not async
/// * expected_panic_msg: [tests_len]u32,
/// - null-terminated string_bytes index
/// - 0 means does not expect pani
/// - 0 means does not expect panic
/// * string_bytes: [string_bytes_len]u8,
pub const TestMetadata = extern struct {
string_bytes_len: u32,
tests_len: u32,
};

/// Trailing:
/// * error_name: [err_len]u8
/// - err_len == 0 means there was no error
pub const TestResults = extern struct {
index: u32,
flags: Flags,
err_len: usize,

pub const Flags = packed struct(u32) {
fail: bool,
Expand Down Expand Up @@ -179,14 +183,19 @@ pub fn serveEmitBinPath(

pub fn serveTestResults(
s: *Server,
msg: OutMessage.TestResults,
error_name: []const u8,
results: OutMessage.TestResults,
) !void {
const msg_le = bswap(msg);
assert(results.err_len == error_name.len);
const result_le = bswap(results);
const bytes_len = @sizeOf(OutMessage.TestResults) + error_name.len;

try s.serveMessage(.{
.tag = .test_results,
.bytes_len = @as(u32, @intCast(@sizeOf(OutMessage.TestResults))),
.bytes_len = @intCast(bytes_len),
}, &.{
std.mem.asBytes(&msg_le),
std.mem.asBytes(&result_le),
error_name,
});
}

Expand Down
32 changes: 20 additions & 12 deletions lib/test_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,36 @@ fn mainServer() !void {
var fail = false;
var skip = false;
var leak = false;
test_fn.func() catch |err| switch (err) {
var err_name: []const u8 = "";

test_fn.func() catch |e| switch (e) {
error.SkipZigTest => skip = true,
else => {
fail = true;
err_name = @errorName(e);
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
}
},
};

leak = std.testing.allocator_instance.deinit() == .leak;
try server.serveTestResults(.{
.index = index,
.flags = .{
.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),
try server.serveTestResults(
err_name,
.{
.index = index,
.flags = .{
.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),
},
.err_len = err_name.len,
},
});
);
},

else => {
Expand Down