From d9f9948b6536339747322b8a04431116b698892e Mon Sep 17 00:00:00 2001 From: iddev5 Date: Mon, 4 Apr 2022 18:34:19 +0530 Subject: [PATCH] std.build: add test for issue 10381 --- lib/std/build.zig | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/lib/std/build.zig b/lib/std/build.zig index f1287c7be5..902849a379 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -3492,3 +3492,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); +}