authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-14 22:24:03-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-23 16:27:38-07:00
loga4cc344aa0947f5b0d0e1a872e6d003b8e580976
treeb623b2ae31c5d89955a6fb137cc52b5eb212fb5a
parent1e785409bb77a4ae1c8a496acae64d94c34431b9

std.Build.Step.Compile: add a way to expect an error message

other than a compile error, specifically

1 files changed, 32 insertions(+), 8 deletions(-)

lib/std/Build/Step/Compile.zig+32-8
......@@ -236,6 +236,7 @@ pub const ExpectedCompileErrors = union(enum) {
236236 contains: []const u8,
237237 exact: []const []const u8,
238238 starts_with: []const u8,
239 stderr_contains: []const u8,
239240};
240241
241242pub const Entry = union(enum) {
......@@ -1945,24 +1946,24 @@ fn checkCompileErrors(compile: *Compile) !void {
19451946
19461947 const arena = compile.step.owner.allocator;
19471948
1948 var actual_stderr_list = std.ArrayList(u8).init(arena);
1949 var actual_errors_list = std.ArrayList(u8).init(arena);
19491950 try actual_eb.renderToWriter(.{
19501951 .ttyconf = .no_color,
19511952 .include_reference_trace = false,
19521953 .include_source_line = false,
1953 }, actual_stderr_list.writer());
1954 const actual_stderr = try actual_stderr_list.toOwnedSlice();
1954 }, actual_errors_list.writer());
1955 const actual_errors = try actual_errors_list.toOwnedSlice();
19551956
19561957 // Render the expected lines into a string that we can compare verbatim.
19571958 var expected_generated = std.ArrayList(u8).init(arena);
19581959 const expect_errors = compile.expect_errors.?;
19591960
1960 var actual_line_it = mem.splitScalar(u8, actual_stderr, '\n');
1961 var actual_line_it = mem.splitScalar(u8, actual_errors, '\n');
19611962
19621963 // TODO merge this with the testing.expectEqualStrings logic, and also CheckFile
19631964 switch (expect_errors) {
19641965 .starts_with => |expect_starts_with| {
1965 if (std.mem.startsWith(u8, actual_stderr, expect_starts_with)) return;
1966 if (std.mem.startsWith(u8, actual_errors, expect_starts_with)) return;
19661967 return compile.step.fail(
19671968 \\
19681969 \\========= should start with: ============
......@@ -1970,7 +1971,7 @@ fn checkCompileErrors(compile: *Compile) !void {
19701971 \\========= but not found: ================
19711972 \\{s}
19721973 \\=========================================
1973 , .{ expect_starts_with, actual_stderr });
1974 , .{ expect_starts_with, actual_errors });
19741975 },
19751976 .contains => |expect_line| {
19761977 while (actual_line_it.next()) |actual_line| {
......@@ -1978,6 +1979,29 @@ fn checkCompileErrors(compile: *Compile) !void {
19781979 return;
19791980 }
19801981
1982 return compile.step.fail(
1983 \\
1984 \\========= should contain: ===============
1985 \\{s}
1986 \\========= but not found: ================
1987 \\{s}
1988 \\=========================================
1989 , .{ expect_line, actual_errors });
1990 },
1991 .stderr_contains => |expect_line| {
1992 const actual_stderr: []const u8 = if (compile.step.result_error_msgs.items.len > 0)
1993 compile.step.result_error_msgs.items[0]
1994 else
1995 &.{};
1996 compile.step.result_error_msgs.clearRetainingCapacity();
1997
1998 var stderr_line_it = mem.splitScalar(u8, actual_stderr, '\n');
1999
2000 while (stderr_line_it.next()) |actual_line| {
2001 if (!matchCompileError(actual_line, expect_line)) continue;
2002 return;
2003 }
2004
19812005 return compile.step.fail(
19822006 \\
19832007 \\========= should contain: ===============
......@@ -2003,7 +2027,7 @@ fn checkCompileErrors(compile: *Compile) !void {
20032027 try expected_generated.append('\n');
20042028 }
20052029
2006 if (mem.eql(u8, expected_generated.items, actual_stderr)) return;
2030 if (mem.eql(u8, expected_generated.items, actual_errors)) return;
20072031
20082032 return compile.step.fail(
20092033 \\
......@@ -2012,7 +2036,7 @@ fn checkCompileErrors(compile: *Compile) !void {
20122036 \\========= but found: ====================
20132037 \\{s}
20142038 \\=========================================
2015 , .{ expected_generated.items, actual_stderr });
2039 , .{ expected_generated.items, actual_errors });
20162040 },
20172041 }
20182042}