diff options
author | Manlio Perillo <manlio.perillo@gmail.com> | 2023-05-19 19:21:21 +0200 |
---|---|---|
committer | Manlio Perillo <manlio.perillo@gmail.com> | 2023-05-19 19:27:44 +0200 |
commit | d0de9e534898451016e8f72328e70d76452c7c15 (patch) | |
tree | 40934019854a49e41356a38b49822a7ecc6da6c4 | |
parent | 699f877bd51bc93717c76573a61a7b5598f1bd82 (diff) |
build: use `std.Build.Step.fail` to report errors
Have all error messages handled in a single place (printError), by using
the `std.Build.Step.fail` method. Ensure that the first letter in the
error message is lower case and remove coloring, since it is done in the
`ZiglingStep.printError` method.
Additionally, in the `ZiglingStep.check_test` method, remove trailing
whitespace from stderr.
-rw-r--r-- | build.zig | 42 |
1 files changed, 15 insertions, 27 deletions
@@ -266,10 +266,9 @@ const ZiglingStep = struct { .cwd_dir = b.build_root.handle, .max_output_bytes = max_output_bytes, }) catch |err| { - print("{s}Unable to spawn {s}: {s}{s}\n", .{ - red_text, exe_path, @errorName(err), reset_text, + return self.step.fail("unable to spawn {s}: {s}", .{ + exe_path, @errorName(err), }); - return err; }; switch (self.exercise.kind) { @@ -285,17 +284,15 @@ const ZiglingStep = struct { switch (result.term) { .Exited => |code| { if (code != 0) { - print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{ - red_text, self.exercise.main_file, code, 0, reset_text, + return self.step.fail("{s} exited with error code {d} (expected {})", .{ + self.exercise.main_file, code, 0, }); - return error.BadExitCode; } }, else => { - print("{s}{s} terminated unexpectedly{s}\n", .{ - red_text, self.exercise.main_file, reset_text, + return self.step.fail("{s} terminated unexpectedly", .{ + self.exercise.main_file, }); - return error.UnexpectedTermination; }, } @@ -310,19 +307,14 @@ const ZiglingStep = struct { const output = try trimLines(b.allocator, raw_output); const exercise_output = self.exercise.output; if (!std.mem.eql(u8, output, self.exercise.output)) { - const red = red_text; - const reset = reset_text; - - print( + return self.step.fail( \\ - \\{s}========= expected this output: =========={s} + \\========= expected this output: ========== \\{s} - \\{s}========= but found: ====================={s} + \\========= but found: ===================== \\{s} - \\{s}=========================================={s} - \\ - , .{ red, reset, exercise_output, red, reset, output, red, reset }); - return error.InvalidOutput; + \\========================================== + , .{ exercise_output, output }); } print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text }); @@ -333,19 +325,15 @@ const ZiglingStep = struct { .Exited => |code| { if (code != 0) { // The test failed. - print("{s}{s}{s}\n", .{ - red_text, result.stderr, reset_text, - }); + const stderr = std.mem.trimRight(u8, result.stderr, " \r\n"); - return error.TestFailed; + return self.step.fail("\n{s}", .{stderr}); } }, else => { - print("{s}{s} terminated unexpectedly{s}\n", .{ - red_text, self.exercise.main_file, reset_text, + return self.step.fail("{s} terminated unexpectedly", .{ + self.exercise.main_file, }); - - return error.UnexpectedTermination; }, } |