Commit d9f9948b65

iddev5 <ayushbardhan5@gmail.com>
2022-04-04 15:04:19
std.build: add test for issue 10381
1 parent 8d3e7aa
Changed files (1)
lib
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);
+}