From a4cc344aa0947f5b0d0e1a872e6d003b8e580976 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 14 Oct 2024 22:24:03 -0700 Subject: [PATCH] std.Build.Step.Compile: add a way to expect an error message other than a compile error, specifically --- lib/std/Build/Step/Compile.zig | 40 +++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 469fbdf74a..e0b9494c0c 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -236,6 +236,7 @@ pub const ExpectedCompileErrors = union(enum) { contains: []const u8, exact: []const []const u8, starts_with: []const u8, + stderr_contains: []const u8, }; pub const Entry = union(enum) { @@ -1945,24 +1946,24 @@ fn checkCompileErrors(compile: *Compile) !void { const arena = compile.step.owner.allocator; - var actual_stderr_list = std.ArrayList(u8).init(arena); + var actual_errors_list = std.ArrayList(u8).init(arena); try actual_eb.renderToWriter(.{ .ttyconf = .no_color, .include_reference_trace = false, .include_source_line = false, - }, actual_stderr_list.writer()); - const actual_stderr = try actual_stderr_list.toOwnedSlice(); + }, actual_errors_list.writer()); + const actual_errors = try actual_errors_list.toOwnedSlice(); // Render the expected lines into a string that we can compare verbatim. var expected_generated = std.ArrayList(u8).init(arena); const expect_errors = compile.expect_errors.?; - var actual_line_it = mem.splitScalar(u8, actual_stderr, '\n'); + var actual_line_it = mem.splitScalar(u8, actual_errors, '\n'); // TODO merge this with the testing.expectEqualStrings logic, and also CheckFile switch (expect_errors) { .starts_with => |expect_starts_with| { - if (std.mem.startsWith(u8, actual_stderr, expect_starts_with)) return; + if (std.mem.startsWith(u8, actual_errors, expect_starts_with)) return; return compile.step.fail( \\ \\========= should start with: ============ @@ -1970,7 +1971,7 @@ fn checkCompileErrors(compile: *Compile) !void { \\========= but not found: ================ \\{s} \\========================================= - , .{ expect_starts_with, actual_stderr }); + , .{ expect_starts_with, actual_errors }); }, .contains => |expect_line| { while (actual_line_it.next()) |actual_line| { @@ -1978,6 +1979,29 @@ fn checkCompileErrors(compile: *Compile) !void { return; } + return compile.step.fail( + \\ + \\========= should contain: =============== + \\{s} + \\========= but not found: ================ + \\{s} + \\========================================= + , .{ expect_line, actual_errors }); + }, + .stderr_contains => |expect_line| { + const actual_stderr: []const u8 = if (compile.step.result_error_msgs.items.len > 0) + compile.step.result_error_msgs.items[0] + else + &.{}; + compile.step.result_error_msgs.clearRetainingCapacity(); + + var stderr_line_it = mem.splitScalar(u8, actual_stderr, '\n'); + + while (stderr_line_it.next()) |actual_line| { + if (!matchCompileError(actual_line, expect_line)) continue; + return; + } + return compile.step.fail( \\ \\========= should contain: =============== @@ -2003,7 +2027,7 @@ fn checkCompileErrors(compile: *Compile) !void { try expected_generated.append('\n'); } - if (mem.eql(u8, expected_generated.items, actual_stderr)) return; + if (mem.eql(u8, expected_generated.items, actual_errors)) return; return compile.step.fail( \\ @@ -2012,7 +2036,7 @@ fn checkCompileErrors(compile: *Compile) !void { \\========= but found: ==================== \\{s} \\========================================= - , .{ expected_generated.items, actual_stderr }); + , .{ expected_generated.items, actual_errors }); }, } }