authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-29 12:01:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-29 12:01:45-07:00
log9aa431cba34699ae35f7905398a0c8263b2ad453
tree5af0bbb9e59833dbb3013b1ea8225800dd14f901
parent8238d4b33585a715c58ab559cd001dd3ea1db55b

test harness: include case names for compile errors

in the progress nodes

2 files changed, 33 insertions(+), 13 deletions(-)

src/test.zig+32-12
......@@ -125,6 +125,7 @@ pub const TestContext = struct {
125125 /// you can keep it mostly consistent, with small changes, testing the
126126 /// effects of the incremental compilation.
127127 src: [:0]const u8,
128 name: []const u8,
128129 case: union(enum) {
129130 /// Check the main binary output file against an expected set of bytes.
130131 /// This is most useful with, for example, `-ofmt=c`.
......@@ -190,6 +191,7 @@ pub const TestContext = struct {
190191 self.emit_h = true;
191192 self.updates.append(.{
192193 .src = src,
194 .name = "update",
193195 .case = .{ .Header = result },
194196 }) catch @panic("out of memory");
195197 }
......@@ -199,6 +201,7 @@ pub const TestContext = struct {
199201 pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) void {
200202 self.updates.append(.{
201203 .src = src,
204 .name = "update",
202205 .case = .{ .Execution = result },
203206 }) catch @panic("out of memory");
204207 }
......@@ -208,15 +211,25 @@ pub const TestContext = struct {
208211 pub fn addCompareObjectFile(self: *Case, src: [:0]const u8, result: []const u8) void {
209212 self.updates.append(.{
210213 .src = src,
214 .name = "update",
211215 .case = .{ .CompareObjectFile = result },
212216 }) catch @panic("out of memory");
213217 }
214218
219 pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) void {
220 return self.addErrorNamed("update", src, errors);
221 }
222
215223 /// Adds a subcase in which the module is updated with `src`, which
216224 /// should contain invalid input, and ensures that compilation fails
217225 /// for the expected reasons, given in sequential order in `errors` in
218226 /// the form `:line:column: error: message`.
219 pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) void {
227 pub fn addErrorNamed(
228 self: *Case,
229 name: []const u8,
230 src: [:0]const u8,
231 errors: []const []const u8,
232 ) void {
220233 var array = self.updates.allocator.alloc(ErrorMsg, errors.len) catch @panic("out of memory");
221234 for (errors) |err_msg_line, i| {
222235 if (std.mem.startsWith(u8, err_msg_line, "error: ")) {
......@@ -279,7 +292,11 @@ pub const TestContext = struct {
279292 },
280293 };
281294 }
282 self.updates.append(.{ .src = src, .case = .{ .Error = array } }) catch @panic("out of memory");
295 self.updates.append(.{
296 .src = src,
297 .name = name,
298 .case = .{ .Error = array },
299 }) catch @panic("out of memory");
283300 }
284301
285302 /// Adds a subcase in which the module is updated with `src`, and
......@@ -616,7 +633,7 @@ pub const TestContext = struct {
616633 if (skip_compile_errors) return;
617634
618635 const gpa = general_purpose_allocator.allocator();
619 var case: ?*Case = null;
636 var opt_case: ?*Case = null;
620637
621638 var it = dir.iterate();
622639 while (try it.next()) |entry| {
......@@ -671,10 +688,9 @@ pub const TestContext = struct {
671688 // The entire file contents is the source, including the manifest
672689 const src = try gpa.dupeZ(u8, contents);
673690
674 // Create a new test case, if necessary
675 case = if (one_test_case_per_file or case == null) blk: {
691 const case = opt_case orelse case: {
676692 ctx.cases.append(TestContext.Case{
677 .name = if (one_test_case_per_file) case_name else name,
693 .name = name,
678694 .target = .{},
679695 .backend = backend,
680696 .updates = std.ArrayList(TestContext.Update).init(ctx.cases.allocator),
......@@ -682,11 +698,15 @@ pub const TestContext = struct {
682698 .output_mode = output_mode,
683699 .files = std.ArrayList(TestContext.File).init(ctx.cases.allocator),
684700 }) catch @panic("out of memory");
685 break :blk &ctx.cases.items[ctx.cases.items.len - 1];
686 } else case.?;
687
688 // Add our update + expected errors
689 case.?.addError(src, errors.items);
701 break :case &ctx.cases.items[ctx.cases.items.len - 1];
702 };
703 if (one_test_case_per_file) {
704 case.name = case_name;
705 case.addError(src, errors.items);
706 opt_case = null;
707 } else {
708 case.addErrorNamed(case_name, src, errors.items);
709 }
690710 } else {
691711 return error.InvalidFile; // Manifests are currently mandatory
692712 }
......@@ -1018,7 +1038,7 @@ pub const TestContext = struct {
10181038 defer comp.destroy();
10191039
10201040 for (case.updates.items) |update, update_index| {
1021 var update_node = root_node.start("update", 3);
1041 var update_node = root_node.start(update.name, 3);
10221042 update_node.activate();
10231043 defer update_node.end();
10241044
test/compile_errors.zig+1-1
......@@ -14,7 +14,7 @@ pub fn addCases(ctx: *TestContext) !void {
1414 defer stage2_dir.close();
1515
1616 const one_test_case_per_file = false;
17 try ctx.addErrorCasesFromDir("stage2 compile errors", stage2_dir, .stage2, .Obj, false, one_test_case_per_file);
17 try ctx.addErrorCasesFromDir("stage2", stage2_dir, .stage2, .Obj, false, one_test_case_per_file);
1818 }
1919
2020 {