build: use Child.exec in ZiglingStep.run

Update the run method to use Child.exec, instead of Child.spawn followed
by Child.wait.
This simplifies the code.
pull/2/head
Manlio Perillo 1 year ago
parent 40cbee8fa2
commit 11d8468539

@ -272,44 +272,31 @@ const ZiglingStep = struct {
resetLine(); resetLine();
print("Checking {s}...\n", .{self.exercise.main_file}); print("Checking {s}...\n", .{self.exercise.main_file});
const cwd = self.builder.build_root.path.?; const b = self.step.owner;
const argv = [_][]const u8{exe_path};
var child = std.ChildProcess.init(&argv, self.builder.allocator);
child.cwd = cwd;
child.env_map = self.builder.env_map;
child.stdin_behavior = .Inherit;
if (self.exercise.check_stdout) {
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Inherit;
} else {
child.stdout_behavior = .Inherit;
child.stderr_behavior = .Pipe;
}
child.spawn() catch |err| { // Allow up to 1 MB of stdout capture.
print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text }); const max_output_bytes = 1 * 1024 * 1024;
var result = Child.exec(.{
.allocator = b.allocator,
.argv = &.{exe_path},
.cwd = b.build_root.path.?,
.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 err; return err;
}; };
// Allow up to 1 MB of stdout capture.
const max_output_len = 1 * 1024 * 1024;
const output = if (self.exercise.check_stdout) const output = if (self.exercise.check_stdout)
try child.stdout.?.reader().readAllAlloc(self.builder.allocator, max_output_len) result.stdout
else else
try child.stderr.?.reader().readAllAlloc(self.builder.allocator, max_output_len); result.stderr;
// At this point stdout is closed, wait for the process to terminate.
const term = child.wait() catch |err| {
print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text });
return err;
};
// Make sure it exited cleanly. // Make sure it exited cleanly.
switch (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", .{ red_text, self.exercise.main_file, code, 0, reset_text }); print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{ red_text, self.exercise.main_file, code, 0, reset_text });

Loading…
Cancel
Save