diff options
author | Chris Boesch <chrboesch@noreply.codeberg.org> | 2023-05-06 19:15:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-06 19:15:49 +0200 |
commit | b963f2659edfc423f44badbfe52d113f86097ea4 (patch) | |
tree | 368654e9339f92b02eb740fd488e7a92c74208bb /test/tests.zig | |
parent | f1368f4f812cce360e932a2b337f07aa0f11ef65 (diff) | |
parent | df833e61e8c7f06bf88c263265295316539222b0 (diff) |
Merge branch 'ratfactor:main' into testing
Diffstat (limited to 'test/tests.zig')
-rw-r--r-- | test/tests.zig | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/test/tests.zig b/test/tests.zig index 0fd3286..a8a7c4c 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -43,18 +43,17 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { }); cmd.setName(b.fmt("zig build -Dhealed -Dn={} test", .{n})); cmd.expectExitCode(0); + cmd.step.dependOn(&heal_step.step); - if (ex.check_stdout) { - expectStdOutMatch(cmd, ex.output); - cmd.expectStdErrEqual(""); - } else { - expectStdErrMatch(cmd, ex.output); - cmd.expectStdOutEqual(""); - } + const output = if (ex.check_stdout) + cmd.captureStdOut() + else + cmd.captureStdErr(); - cmd.step.dependOn(&heal_step.step); + const verify = CheckNamedStep.create(b, ex, output); + verify.step.dependOn(&cmd.step); - case_step.dependOn(&cmd.step); + case_step.dependOn(&verify.step); } const cleanup = b.addRemoveDirTree(tmp_path); @@ -175,7 +174,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step { const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dn=1" }); cmd.setName("zig build -Dn=1"); cmd.expectExitCode(1); - expectStdErrMatch(cmd, exercises[0].hint); + expectStdErrMatch(cmd, exercises[0].hint orelse ""); cmd.step.dependOn(case_step); @@ -196,6 +195,47 @@ fn createCase(b: *Build, name: []const u8) *Step { return case_step; } +/// Checks the output of `zig build -Dn=n test`. +const CheckNamedStep = struct { + step: Step, + exercise: Exercise, + output: FileSource, + + pub fn create(owner: *Build, exercise: Exercise, output: FileSource) *CheckNamedStep { + const self = owner.allocator.create(CheckNamedStep) catch @panic("OOM"); + self.* = .{ + .step = Step.init(.{ + .id = .custom, + .name = "check-named", + .owner = owner, + .makeFn = make, + }), + .exercise = exercise, + .output = output, + }; + + return self; + } + + fn make(step: *Step, _: *std.Progress.Node) !void { + const b = step.owner; + const self = @fieldParentPtr(CheckNamedStep, "step", step); + + // Allow up to 1 MB of output capture. + const max_bytes = 1 * 1024 * 1024; + const path = self.output.getPath(b); + const raw_output = try fs.cwd().readFileAlloc(b.allocator, path, max_bytes); + + const actual = try root.trimLines(b.allocator, raw_output); + const expect = self.exercise.output; + if (!mem.eql(u8, expect, actual)) { + return step.fail("{s}: expected to see \"{s}\", found \"{s}\"", .{ + self.exercise.main_file, expect, actual, + }); + } + } +}; + /// Checks the output of `zig build` or `zig build -Dn=1 start`. const CheckStep = struct { step: Step, |