authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-14 14:56:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-14 14:56:45-07:00
log85b10eb07c27d021804473ee54a0b5942e4784d5
tree1c3450ba81bed2e4125bf21e21ce2e03631ca53e
parent5523e2061b37de2b884c1c53fdcefb823755c7d0

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.

3 files changed, 13 insertions(+), 9 deletions(-)

build.zig+1-6
......@@ -40,17 +40,12 @@ pub fn build(b: *Builder) !void {
4040 const docs_step = b.step("docs", "Build documentation");
4141 docs_step.dependOn(&docgen_cmd.step);
4242
43 var test_cases = b.addTest("src/test.zig");
43 const test_cases = b.addTest("src/test.zig");
4444 test_cases.stack_size = stack_size;
4545 test_cases.setBuildMode(mode);
4646 test_cases.addPackagePath("test_cases", "test/cases.zig");
4747 test_cases.single_threaded = single_threaded;
4848
49 const test_options = b.addOptions();
50 test_options.addOption([]const u8, "zig_exe_path", b.zig_exe);
51 test_cases.addOptions("test_options", test_options);
52 test_cases.step.dependOn(&test_options.step);
53
5449 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
5550
5651 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;
src/main.zig+9-1
......@@ -3057,6 +3057,7 @@ fn buildOutputType(
30573057 gpa,
30583058 arena,
30593059 test_exec_args.items,
3060 self_exe_path,
30603061 arg_mode,
30613062 target_info,
30623063 watch,
......@@ -3128,6 +3129,7 @@ fn buildOutputType(
31283129 gpa,
31293130 arena,
31303131 test_exec_args.items,
3132 self_exe_path,
31313133 arg_mode,
31323134 target_info,
31333135 watch,
......@@ -3152,6 +3154,7 @@ fn buildOutputType(
31523154 gpa,
31533155 arena,
31543156 test_exec_args.items,
3157 self_exe_path,
31553158 arg_mode,
31563159 target_info,
31573160 watch,
......@@ -3230,6 +3233,7 @@ fn runOrTest(
32303233 gpa: Allocator,
32313234 arena: Allocator,
32323235 test_exec_args: []const ?[]const u8,
3236 self_exe_path: []const u8,
32333237 arg_mode: ArgMode,
32343238 target_info: std.zig.system.NativeTargetInfo,
32353239 watch: bool,
......@@ -3258,16 +3262,20 @@ fn runOrTest(
32583262 if (runtime_args_start) |i| {
32593263 try argv.appendSlice(all_args[i..]);
32603264 }
3265 var env_map = try std.process.getEnvMap(arena);
3266 try env_map.put("ZIG_EXE", self_exe_path);
3267
32613268 // We do not execve for tests because if the test fails we want to print
32623269 // the error message and invocation below.
32633270 if (std.process.can_execv and arg_mode == .run and !watch) {
32643271 // execv releases the locks; no need to destroy the Compilation here.
3265 const err = std.process.execv(gpa, argv.items);
3272 const err = std.process.execve(gpa, argv.items, &env_map);
32663273 try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc);
32673274 const cmd = try std.mem.join(arena, " ", argv.items);
32683275 fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });
32693276 } else if (std.process.can_spawn) {
32703277 var child = std.ChildProcess.init(argv.items, gpa);
3278 child.env_map = &env_map;
32713279 child.stdin_behavior = .Inherit;
32723280 child.stdout_behavior = .Inherit;
32733281 child.stderr_behavior = .Inherit;
src/test.zig+3-2
......@@ -1214,6 +1214,7 @@ pub const TestContext = struct {
12141214
12151215 fn run(self: *TestContext) !void {
12161216 const host = try std.zig.system.NativeTargetInfo.detect(.{});
1217 const zig_exe_path = try std.process.getEnvVarOwned(self.arena, "ZIG_EXE");
12171218
12181219 var progress = std.Progress{};
12191220 const root_node = progress.start("compiler", self.cases.items.len);
......@@ -1272,6 +1273,7 @@ pub const TestContext = struct {
12721273 &prg_node,
12731274 case.*,
12741275 zig_lib_directory,
1276 zig_exe_path,
12751277 &aux_thread_pool,
12761278 global_cache_directory,
12771279 host,
......@@ -1298,6 +1300,7 @@ pub const TestContext = struct {
12981300 root_node: *std.Progress.Node,
12991301 case: Case,
13001302 zig_lib_directory: Compilation.Directory,
1303 zig_exe_path: []const u8,
13011304 thread_pool: *ThreadPool,
13021305 global_cache_directory: Compilation.Directory,
13031306 host: std.zig.system.NativeTargetInfo,
......@@ -1329,8 +1332,6 @@ pub const TestContext = struct {
13291332 &[_][]const u8{ tmp_dir_path, "zig-cache" },
13301333 );
13311334
1332 const zig_exe_path = @import("test_options").zig_exe_path;
1333
13341335 for (case.files.items) |file| {
13351336 try tmp.dir.writeFile(file.path, file.src);
13361337 }