diff options
author | Manlio Perillo <manlio.perillo@gmail.com> | 2023-05-06 18:02:18 +0200 |
---|---|---|
committer | Manlio Perillo <manlio.perillo@gmail.com> | 2023-05-06 18:12:04 +0200 |
commit | 1dd5852bec6fb72ebb1b712c7a78425dcc015390 (patch) | |
tree | 9717148034f0d7f915affffde03296659d824acd /test | |
parent | 397c6671c0050f707b0599a5fc96fab9621281e6 (diff) |
build: use multiline string literals when necessary
Update the output and hint fields in the `exercises` slice to use a
multiline string literal when the string have multiple lines or use the
`"` character. This will greatly improve readability.
Additionally, remove the trailing whitespace on each line and check it
in the validate_exercises function.
Update the output comparison logic in ZiglingStep, since the current
code assumes that the string has only one line.
Update test/tests.zig to use the new `CheckNamedStep` in test case 1,
since RunStep.StdIo.Check is no longer able to correctly check the
output.
Fixes #283
Diffstat (limited to 'test')
-rw-r--r-- | test/tests.zig | 58 |
1 files changed, 49 insertions, 9 deletions
diff --git a/test/tests.zig b/test/tests.zig index e00ece9..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); @@ -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, |