diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-04-28 18:38:44 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-28 18:38:44 +0300 |
| commit | 75c9936737a6ba991d4ef187ddc9d51bc0ad0998 (patch) | |
| tree | d9dde306427984cd5dd82d87884b361ad3081a72 /lib/std | |
| parent | 7f13f5cd5f5a518638b15d7225eae2d88ec1efb5 (diff) | |
| parent | d9f9948b6536339747322b8a04431116b698892e (diff) | |
| download | zig-75c9936737a6ba991d4ef187ddc9d51bc0ad0998.tar.gz zig-75c9936737a6ba991d4ef187ddc9d51bc0ad0998.zip | |
Merge pull request #11214 from iddev5/ay-build-runner
std: explicitly handle error.UnexpectedExitCode in build_runner
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/build.zig | 55 | ||||
| -rw-r--r-- | lib/std/special/build_runner.zig | 2 | ||||
| -rw-r--r-- | lib/std/testing.zig | 30 |
3 files changed, 86 insertions, 1 deletions
diff --git a/lib/std/build.zig b/lib/std/build.zig index f4ef5be888..c126fa50b4 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -3582,3 +3582,58 @@ test "LibExeObjStep.addPackage" { const dupe = exe.packages.items[0]; try std.testing.expectEqualStrings(pkg_top.name, dupe.name); } + +test "build_runner issue 10381" { + if (builtin.os.tag == .wasi) return error.SkipZigTest; + + const progstr = + \\ pub fn main() u8 { + \\ return 1; + \\ } + ; + + const buildstr = + \\ const std = @import("std"); + \\ pub fn build(b: *std.build.Builder) void { + \\ const exe = b.addExecutable("source", "source.zig"); + \\ exe.install(); + \\ const run_cmd = exe.run(); + \\ run_cmd.step.dependOn(b.getInstallStep()); + \\ const run_step = b.step("run", "Run"); + \\ run_step.dependOn(&run_cmd.step); + \\ } + ; + + const testing = std.testing; + const allocator = testing.allocator; + + var it = try std.process.argsWithAllocator(allocator); + defer it.deinit(); + const testargs = try testing.getTestArgs(&it); + + var tmpdir = testing.tmpDir(.{ .no_follow = true }); + defer tmpdir.cleanup(); + const tmpdir_path = try tmpdir.getFullPath(allocator); + defer allocator.free(tmpdir_path); + + try tmpdir.dir.writeFile("source.zig", progstr); + try tmpdir.dir.writeFile("build.zig", buildstr); + + const cwd_path = try std.process.getCwdAlloc(allocator); + defer allocator.free(cwd_path); + const lib_dir = try std.fs.path.join(allocator, &.{ cwd_path, "lib" }); + defer allocator.free(lib_dir); + + const result = try testing.runZigBuild(testargs.zigexec, .{ + .subcmd = "run", + .cwd = tmpdir_path, + .lib_dir = lib_dir, + }); + defer { + allocator.free(result.stdout); + allocator.free(result.stderr); + } + + try testing.expectEqual(result.term, .{ .Exited = 1 }); + try testing.expect(std.mem.indexOf(u8, result.stderr, "error: UnexpectedExitCode") == null); +} diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 523723ddf2..e844866c79 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -209,7 +209,7 @@ pub fn main() !void { error.InvalidStepName => { return usageAndErr(builder, true, stderr_stream); }, - error.UncleanExit => process.exit(1), + error.UnexpectedExitCode, error.UncleanExit => process.exit(1), else => return err, } }; diff --git a/lib/std/testing.zig b/lib/std/testing.zig index df2d6c7a43..d105ff46f9 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -487,6 +487,36 @@ pub fn buildExe(zigexec: []const u8, zigfile: []const u8, binfile: []const u8) ! try expectEqual(ret_val, .{ .Exited = 0 }); } +/// Spawns a zig build runner process 'zigexec build subcmd' and +/// expects success +/// If specified, runs zig build in the cwd path +/// If specified, uses the specified lib_dir for zig standard library +/// instead of compiler's default library directory +pub fn runZigBuild(zigexec: []const u8, options: struct { + subcmd: ?[]const u8 = null, + cwd: ?[]const u8 = null, + lib_dir: ?[]const u8 = null, +}) !std.ChildProcess.ExecResult { + var args = std.ArrayList([]const u8).init(allocator); + defer args.deinit(); + + try args.appendSlice(&.{ zigexec, "build" }); + if (options.subcmd) |subcmd| try args.append(subcmd); + if (options.lib_dir) |lib_dir| try args.append(lib_dir); + + var result = try std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = args.items, + .cwd = if (options.cwd) |c| c else null, + }); + errdefer { + allocator.free(result.stdout); + allocator.free(result.stderr); + } + + return result; +} + test "expectEqual nested array" { const a = [2][2]f32{ [_]f32{ 1.0, 0.0 }, |
