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.
This commit is contained in:
Manlio Perillo 2023-05-19 19:21:21 +02:00
parent 699f877bd5
commit d0de9e5348

View file

@ -266,10 +266,9 @@ const ZiglingStep = struct {
.cwd_dir = b.build_root.handle, .cwd_dir = b.build_root.handle,
.max_output_bytes = max_output_bytes, .max_output_bytes = max_output_bytes,
}) catch |err| { }) catch |err| {
print("{s}Unable to spawn {s}: {s}{s}\n", .{ return self.step.fail("unable to spawn {s}: {s}", .{
red_text, exe_path, @errorName(err), reset_text, exe_path, @errorName(err),
}); });
return err;
}; };
switch (self.exercise.kind) { switch (self.exercise.kind) {
@ -285,17 +284,15 @@ const ZiglingStep = struct {
switch (result.term) { switch (result.term) {
.Exited => |code| { .Exited => |code| {
if (code != 0) { if (code != 0) {
print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{ return self.step.fail("{s} exited with error code {d} (expected {})", .{
red_text, self.exercise.main_file, code, 0, reset_text, self.exercise.main_file, code, 0,
}); });
return error.BadExitCode;
} }
}, },
else => { else => {
print("{s}{s} terminated unexpectedly{s}\n", .{ return self.step.fail("{s} terminated unexpectedly", .{
red_text, self.exercise.main_file, reset_text, self.exercise.main_file,
}); });
return error.UnexpectedTermination;
}, },
} }
@ -310,19 +307,14 @@ const ZiglingStep = struct {
const output = try trimLines(b.allocator, raw_output); const output = try trimLines(b.allocator, raw_output);
const exercise_output = self.exercise.output; const exercise_output = self.exercise.output;
if (!std.mem.eql(u8, output, self.exercise.output)) { if (!std.mem.eql(u8, output, self.exercise.output)) {
const red = red_text; return self.step.fail(
const reset = reset_text;
print(
\\ \\
\\{s}========= expected this output: =========={s} \\========= expected this output: ==========
\\{s} \\{s}
\\{s}========= but found: ====================={s} \\========= but found: =====================
\\{s} \\{s}
\\{s}=========================================={s} \\==========================================
\\ , .{ exercise_output, output });
, .{ red, reset, exercise_output, red, reset, output, red, reset });
return error.InvalidOutput;
} }
print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text }); print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text });
@ -333,19 +325,15 @@ const ZiglingStep = struct {
.Exited => |code| { .Exited => |code| {
if (code != 0) { if (code != 0) {
// The test failed. // The test failed.
print("{s}{s}{s}\n", .{ const stderr = std.mem.trimRight(u8, result.stderr, " \r\n");
red_text, result.stderr, reset_text,
});
return error.TestFailed; return self.step.fail("\n{s}", .{stderr});
} }
}, },
else => { else => {
print("{s}{s} terminated unexpectedly{s}\n", .{ return self.step.fail("{s} terminated unexpectedly", .{
red_text, self.exercise.main_file, reset_text, self.exercise.main_file,
}); });
return error.UnexpectedTermination;
}, },
} }