authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-04-28 18:38:44+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-04-28 18:38:44+03:00
log75c9936737a6ba991d4ef187ddc9d51bc0ad0998
treed9dde306427984cd5dd82d87884b361ad3081a72
parent7f13f5cd5f5a518638b15d7225eae2d88ec1efb5
parentd9f9948b6536339747322b8a04431116b698892e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11214 from iddev5/ay-build-runner

std: explicitly handle error.UnexpectedExitCode in build_runner

3 files changed, 86 insertions(+), 1 deletions(-)

lib/std/build.zig+55
...@@ -3582,3 +3582,58 @@ test "LibExeObjStep.addPackage" {...@@ -3582,3 +3582,58 @@ test "LibExeObjStep.addPackage" {
3582 const dupe = exe.packages.items[0];3582 const dupe = exe.packages.items[0];
3583 try std.testing.expectEqualStrings(pkg_top.name, dupe.name);3583 try std.testing.expectEqualStrings(pkg_top.name, dupe.name);
3584}3584}
3585
3586test "build_runner issue 10381" {
3587 if (builtin.os.tag == .wasi) return error.SkipZigTest;
3588
3589 const progstr =
3590 \\ pub fn main() u8 {
3591 \\ return 1;
3592 \\ }
3593 ;
3594
3595 const buildstr =
3596 \\ const std = @import("std");
3597 \\ pub fn build(b: *std.build.Builder) void {
3598 \\ const exe = b.addExecutable("source", "source.zig");
3599 \\ exe.install();
3600 \\ const run_cmd = exe.run();
3601 \\ run_cmd.step.dependOn(b.getInstallStep());
3602 \\ const run_step = b.step("run", "Run");
3603 \\ run_step.dependOn(&run_cmd.step);
3604 \\ }
3605 ;
3606
3607 const testing = std.testing;
3608 const allocator = testing.allocator;
3609
3610 var it = try std.process.argsWithAllocator(allocator);
3611 defer it.deinit();
3612 const testargs = try testing.getTestArgs(&it);
3613
3614 var tmpdir = testing.tmpDir(.{ .no_follow = true });
3615 defer tmpdir.cleanup();
3616 const tmpdir_path = try tmpdir.getFullPath(allocator);
3617 defer allocator.free(tmpdir_path);
3618
3619 try tmpdir.dir.writeFile("source.zig", progstr);
3620 try tmpdir.dir.writeFile("build.zig", buildstr);
3621
3622 const cwd_path = try std.process.getCwdAlloc(allocator);
3623 defer allocator.free(cwd_path);
3624 const lib_dir = try std.fs.path.join(allocator, &.{ cwd_path, "lib" });
3625 defer allocator.free(lib_dir);
3626
3627 const result = try testing.runZigBuild(testargs.zigexec, .{
3628 .subcmd = "run",
3629 .cwd = tmpdir_path,
3630 .lib_dir = lib_dir,
3631 });
3632 defer {
3633 allocator.free(result.stdout);
3634 allocator.free(result.stderr);
3635 }
3636
3637 try testing.expectEqual(result.term, .{ .Exited = 1 });
3638 try testing.expect(std.mem.indexOf(u8, result.stderr, "error: UnexpectedExitCode") == null);
3639}
lib/std/special/build_runner.zig+1-1
...@@ -209,7 +209,7 @@ pub fn main() !void {...@@ -209,7 +209,7 @@ pub fn main() !void {
209 error.InvalidStepName => {209 error.InvalidStepName => {
210 return usageAndErr(builder, true, stderr_stream);210 return usageAndErr(builder, true, stderr_stream);
211 },211 },
212 error.UncleanExit => process.exit(1),212 error.UnexpectedExitCode, error.UncleanExit => process.exit(1),
213 else => return err,213 else => return err,
214 }214 }
215 };215 };
lib/std/testing.zig+30
...@@ -487,6 +487,36 @@ pub fn buildExe(zigexec: []const u8, zigfile: []const u8, binfile: []const u8) !...@@ -487,6 +487,36 @@ pub fn buildExe(zigexec: []const u8, zigfile: []const u8, binfile: []const u8) !
487 try expectEqual(ret_val, .{ .Exited = 0 });487 try expectEqual(ret_val, .{ .Exited = 0 });
488}488}
489489
490/// Spawns a zig build runner process 'zigexec build subcmd' and
491/// expects success
492/// If specified, runs zig build in the cwd path
493/// If specified, uses the specified lib_dir for zig standard library
494/// instead of compiler's default library directory
495pub fn runZigBuild(zigexec: []const u8, options: struct {
496 subcmd: ?[]const u8 = null,
497 cwd: ?[]const u8 = null,
498 lib_dir: ?[]const u8 = null,
499}) !std.ChildProcess.ExecResult {
500 var args = std.ArrayList([]const u8).init(allocator);
501 defer args.deinit();
502
503 try args.appendSlice(&.{ zigexec, "build" });
504 if (options.subcmd) |subcmd| try args.append(subcmd);
505 if (options.lib_dir) |lib_dir| try args.append(lib_dir);
506
507 var result = try std.ChildProcess.exec(.{
508 .allocator = allocator,
509 .argv = args.items,
510 .cwd = if (options.cwd) |c| c else null,
511 });
512 errdefer {
513 allocator.free(result.stdout);
514 allocator.free(result.stderr);
515 }
516
517 return result;
518}
519
490test "expectEqual nested array" {520test "expectEqual nested array" {
491 const a = [2][2]f32{521 const a = [2][2]f32{
492 [_]f32{ 1.0, 0.0 },522 [_]f32{ 1.0, 0.0 },