ZIG_EXE envirnoment variable instead of testing build options

No longer introduce build options for tests. Instead, ZIG_EXE
environment variable is added to any invocation of `zig run` or `zig
test`.

The end result of this branch is the same: there is no longer a
mandatory positional command line argument when invoking zig test
binaries directly.
This commit is contained in:
Andrew Kelley 2022-09-14 14:56:45 -07:00
parent 5523e2061b
commit 85b10eb07c
3 changed files with 13 additions and 9 deletions

View file

@ -40,17 +40,12 @@ pub fn build(b: *Builder) !void {
const docs_step = b.step("docs", "Build documentation"); const docs_step = b.step("docs", "Build documentation");
docs_step.dependOn(&docgen_cmd.step); docs_step.dependOn(&docgen_cmd.step);
var test_cases = b.addTest("src/test.zig"); const test_cases = b.addTest("src/test.zig");
test_cases.stack_size = stack_size; test_cases.stack_size = stack_size;
test_cases.setBuildMode(mode); test_cases.setBuildMode(mode);
test_cases.addPackagePath("test_cases", "test/cases.zig"); test_cases.addPackagePath("test_cases", "test/cases.zig");
test_cases.single_threaded = single_threaded; test_cases.single_threaded = single_threaded;
const test_options = b.addOptions();
test_options.addOption([]const u8, "zig_exe_path", b.zig_exe);
test_cases.addOptions("test_options", test_options);
test_cases.step.dependOn(&test_options.step);
const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"}); const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false; const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;

View file

@ -3057,6 +3057,7 @@ fn buildOutputType(
gpa, gpa,
arena, arena,
test_exec_args.items, test_exec_args.items,
self_exe_path,
arg_mode, arg_mode,
target_info, target_info,
watch, watch,
@ -3128,6 +3129,7 @@ fn buildOutputType(
gpa, gpa,
arena, arena,
test_exec_args.items, test_exec_args.items,
self_exe_path,
arg_mode, arg_mode,
target_info, target_info,
watch, watch,
@ -3152,6 +3154,7 @@ fn buildOutputType(
gpa, gpa,
arena, arena,
test_exec_args.items, test_exec_args.items,
self_exe_path,
arg_mode, arg_mode,
target_info, target_info,
watch, watch,
@ -3230,6 +3233,7 @@ fn runOrTest(
gpa: Allocator, gpa: Allocator,
arena: Allocator, arena: Allocator,
test_exec_args: []const ?[]const u8, test_exec_args: []const ?[]const u8,
self_exe_path: []const u8,
arg_mode: ArgMode, arg_mode: ArgMode,
target_info: std.zig.system.NativeTargetInfo, target_info: std.zig.system.NativeTargetInfo,
watch: bool, watch: bool,
@ -3258,16 +3262,20 @@ fn runOrTest(
if (runtime_args_start) |i| { if (runtime_args_start) |i| {
try argv.appendSlice(all_args[i..]); try argv.appendSlice(all_args[i..]);
} }
var env_map = try std.process.getEnvMap(arena);
try env_map.put("ZIG_EXE", self_exe_path);
// We do not execve for tests because if the test fails we want to print // We do not execve for tests because if the test fails we want to print
// the error message and invocation below. // the error message and invocation below.
if (std.process.can_execv and arg_mode == .run and !watch) { if (std.process.can_execv and arg_mode == .run and !watch) {
// execv releases the locks; no need to destroy the Compilation here. // execv releases the locks; no need to destroy the Compilation here.
const err = std.process.execv(gpa, argv.items); const err = std.process.execve(gpa, argv.items, &env_map);
try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc); try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc);
const cmd = try std.mem.join(arena, " ", argv.items); const cmd = try std.mem.join(arena, " ", argv.items);
fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd }); fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });
} else if (std.process.can_spawn) { } else if (std.process.can_spawn) {
var child = std.ChildProcess.init(argv.items, gpa); var child = std.ChildProcess.init(argv.items, gpa);
child.env_map = &env_map;
child.stdin_behavior = .Inherit; child.stdin_behavior = .Inherit;
child.stdout_behavior = .Inherit; child.stdout_behavior = .Inherit;
child.stderr_behavior = .Inherit; child.stderr_behavior = .Inherit;

View file

@ -1214,6 +1214,7 @@ pub const TestContext = struct {
fn run(self: *TestContext) !void { fn run(self: *TestContext) !void {
const host = try std.zig.system.NativeTargetInfo.detect(.{}); const host = try std.zig.system.NativeTargetInfo.detect(.{});
const zig_exe_path = try std.process.getEnvVarOwned(self.arena, "ZIG_EXE");
var progress = std.Progress{}; var progress = std.Progress{};
const root_node = progress.start("compiler", self.cases.items.len); const root_node = progress.start("compiler", self.cases.items.len);
@ -1272,6 +1273,7 @@ pub const TestContext = struct {
&prg_node, &prg_node,
case.*, case.*,
zig_lib_directory, zig_lib_directory,
zig_exe_path,
&aux_thread_pool, &aux_thread_pool,
global_cache_directory, global_cache_directory,
host, host,
@ -1298,6 +1300,7 @@ pub const TestContext = struct {
root_node: *std.Progress.Node, root_node: *std.Progress.Node,
case: Case, case: Case,
zig_lib_directory: Compilation.Directory, zig_lib_directory: Compilation.Directory,
zig_exe_path: []const u8,
thread_pool: *ThreadPool, thread_pool: *ThreadPool,
global_cache_directory: Compilation.Directory, global_cache_directory: Compilation.Directory,
host: std.zig.system.NativeTargetInfo, host: std.zig.system.NativeTargetInfo,
@ -1329,8 +1332,6 @@ pub const TestContext = struct {
&[_][]const u8{ tmp_dir_path, "zig-cache" }, &[_][]const u8{ tmp_dir_path, "zig-cache" },
); );
const zig_exe_path = @import("test_options").zig_exe_path;
for (case.files.items) |file| { for (case.files.items) |file| {
try tmp.dir.writeFile(file.path, file.src); try tmp.dir.writeFile(file.path, file.src);
} }