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 {...@@ -40,17 +40,12 @@ pub fn build(b: *Builder) !void {
40 const docs_step = b.step("docs", "Build documentation");40 const docs_step = b.step("docs", "Build documentation");
41 docs_step.dependOn(&docgen_cmd.step);41 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");
44 test_cases.stack_size = stack_size;44 test_cases.stack_size = stack_size;
45 test_cases.setBuildMode(mode);45 test_cases.setBuildMode(mode);
46 test_cases.addPackagePath("test_cases", "test/cases.zig");46 test_cases.addPackagePath("test_cases", "test/cases.zig");
47 test_cases.single_threaded = single_threaded;47 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
54 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});49 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
5550
56 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;51 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(...@@ -3057,6 +3057,7 @@ fn buildOutputType(
3057 gpa,3057 gpa,
3058 arena,3058 arena,
3059 test_exec_args.items,3059 test_exec_args.items,
3060 self_exe_path,
3060 arg_mode,3061 arg_mode,
3061 target_info,3062 target_info,
3062 watch,3063 watch,
...@@ -3128,6 +3129,7 @@ fn buildOutputType(...@@ -3128,6 +3129,7 @@ fn buildOutputType(
3128 gpa,3129 gpa,
3129 arena,3130 arena,
3130 test_exec_args.items,3131 test_exec_args.items,
3132 self_exe_path,
3131 arg_mode,3133 arg_mode,
3132 target_info,3134 target_info,
3133 watch,3135 watch,
...@@ -3152,6 +3154,7 @@ fn buildOutputType(...@@ -3152,6 +3154,7 @@ fn buildOutputType(
3152 gpa,3154 gpa,
3153 arena,3155 arena,
3154 test_exec_args.items,3156 test_exec_args.items,
3157 self_exe_path,
3155 arg_mode,3158 arg_mode,
3156 target_info,3159 target_info,
3157 watch,3160 watch,
...@@ -3230,6 +3233,7 @@ fn runOrTest(...@@ -3230,6 +3233,7 @@ fn runOrTest(
3230 gpa: Allocator,3233 gpa: Allocator,
3231 arena: Allocator,3234 arena: Allocator,
3232 test_exec_args: []const ?[]const u8,3235 test_exec_args: []const ?[]const u8,
3236 self_exe_path: []const u8,
3233 arg_mode: ArgMode,3237 arg_mode: ArgMode,
3234 target_info: std.zig.system.NativeTargetInfo,3238 target_info: std.zig.system.NativeTargetInfo,
3235 watch: bool,3239 watch: bool,
...@@ -3258,16 +3262,20 @@ fn runOrTest(...@@ -3258,16 +3262,20 @@ fn runOrTest(
3258 if (runtime_args_start) |i| {3262 if (runtime_args_start) |i| {
3259 try argv.appendSlice(all_args[i..]);3263 try argv.appendSlice(all_args[i..]);
3260 }3264 }
3265 var env_map = try std.process.getEnvMap(arena);
3266 try env_map.put("ZIG_EXE", self_exe_path);
3267
3261 // We do not execve for tests because if the test fails we want to print3268 // We do not execve for tests because if the test fails we want to print
3262 // the error message and invocation below.3269 // the error message and invocation below.
3263 if (std.process.can_execv and arg_mode == .run and !watch) {3270 if (std.process.can_execv and arg_mode == .run and !watch) {
3264 // execv releases the locks; no need to destroy the Compilation here.3271 // 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);
3266 try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc);3273 try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc);
3267 const cmd = try std.mem.join(arena, " ", argv.items);3274 const cmd = try std.mem.join(arena, " ", argv.items);
3268 fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });3275 fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });
3269 } else if (std.process.can_spawn) {3276 } else if (std.process.can_spawn) {
3270 var child = std.ChildProcess.init(argv.items, gpa);3277 var child = std.ChildProcess.init(argv.items, gpa);
3278 child.env_map = &env_map;
3271 child.stdin_behavior = .Inherit;3279 child.stdin_behavior = .Inherit;
3272 child.stdout_behavior = .Inherit;3280 child.stdout_behavior = .Inherit;
3273 child.stderr_behavior = .Inherit;3281 child.stderr_behavior = .Inherit;
src/test.zig+3-2
...@@ -1214,6 +1214,7 @@ pub const TestContext = struct {...@@ -1214,6 +1214,7 @@ pub const TestContext = struct {
12141214
1215 fn run(self: *TestContext) !void {1215 fn run(self: *TestContext) !void {
1216 const host = try std.zig.system.NativeTargetInfo.detect(.{});1216 const host = try std.zig.system.NativeTargetInfo.detect(.{});
1217 const zig_exe_path = try std.process.getEnvVarOwned(self.arena, "ZIG_EXE");
12171218
1218 var progress = std.Progress{};1219 var progress = std.Progress{};
1219 const root_node = progress.start("compiler", self.cases.items.len);1220 const root_node = progress.start("compiler", self.cases.items.len);
...@@ -1272,6 +1273,7 @@ pub const TestContext = struct {...@@ -1272,6 +1273,7 @@ pub const TestContext = struct {
1272 &prg_node,1273 &prg_node,
1273 case.*,1274 case.*,
1274 zig_lib_directory,1275 zig_lib_directory,
1276 zig_exe_path,
1275 &aux_thread_pool,1277 &aux_thread_pool,
1276 global_cache_directory,1278 global_cache_directory,
1277 host,1279 host,
...@@ -1298,6 +1300,7 @@ pub const TestContext = struct {...@@ -1298,6 +1300,7 @@ pub const TestContext = struct {
1298 root_node: *std.Progress.Node,1300 root_node: *std.Progress.Node,
1299 case: Case,1301 case: Case,
1300 zig_lib_directory: Compilation.Directory,1302 zig_lib_directory: Compilation.Directory,
1303 zig_exe_path: []const u8,
1301 thread_pool: *ThreadPool,1304 thread_pool: *ThreadPool,
1302 global_cache_directory: Compilation.Directory,1305 global_cache_directory: Compilation.Directory,
1303 host: std.zig.system.NativeTargetInfo,1306 host: std.zig.system.NativeTargetInfo,
...@@ -1329,8 +1332,6 @@ pub const TestContext = struct {...@@ -1329,8 +1332,6 @@ pub const TestContext = struct {
1329 &[_][]const u8{ tmp_dir_path, "zig-cache" },1332 &[_][]const u8{ tmp_dir_path, "zig-cache" },
1330 );1333 );
13311334
1332 const zig_exe_path = @import("test_options").zig_exe_path;
1333
1334 for (case.files.items) |file| {1335 for (case.files.items) |file| {
1335 try tmp.dir.writeFile(file.path, file.src);1336 try tmp.dir.writeFile(file.path, file.src);
1336 }1337 }