mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 14:25:16 +00:00
Merge pull request #11214 from iddev5/ay-build-runner
std: explicitly handle error.UnexpectedExitCode in build_runner
This commit is contained in:
commit
75c9936737
@ -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);
|
||||
}
|
||||
|
||||
@ -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,
|
||||
}
|
||||
};
|
||||
|
||||
@ -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 },
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user