Commit 85b10eb07c

Andrew Kelley <andrew@ziglang.org>
2022-09-14 23:56:45
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.
1 parent 5523e20
Changed files (3)
src/main.zig
@@ -3057,6 +3057,7 @@ fn buildOutputType(
             gpa,
             arena,
             test_exec_args.items,
+            self_exe_path,
             arg_mode,
             target_info,
             watch,
@@ -3128,6 +3129,7 @@ fn buildOutputType(
                         gpa,
                         arena,
                         test_exec_args.items,
+                        self_exe_path,
                         arg_mode,
                         target_info,
                         watch,
@@ -3152,6 +3154,7 @@ fn buildOutputType(
                         gpa,
                         arena,
                         test_exec_args.items,
+                        self_exe_path,
                         arg_mode,
                         target_info,
                         watch,
@@ -3230,6 +3233,7 @@ fn runOrTest(
     gpa: Allocator,
     arena: Allocator,
     test_exec_args: []const ?[]const u8,
+    self_exe_path: []const u8,
     arg_mode: ArgMode,
     target_info: std.zig.system.NativeTargetInfo,
     watch: bool,
@@ -3258,16 +3262,20 @@ fn runOrTest(
     if (runtime_args_start) |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
     // the error message and invocation below.
     if (std.process.can_execv and arg_mode == .run and !watch) {
         // 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);
         const cmd = try std.mem.join(arena, " ", argv.items);
         fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });
     } else if (std.process.can_spawn) {
         var child = std.ChildProcess.init(argv.items, gpa);
+        child.env_map = &env_map;
         child.stdin_behavior = .Inherit;
         child.stdout_behavior = .Inherit;
         child.stderr_behavior = .Inherit;
src/test.zig
@@ -1214,6 +1214,7 @@ pub const TestContext = struct {
 
     fn run(self: *TestContext) !void {
         const host = try std.zig.system.NativeTargetInfo.detect(.{});
+        const zig_exe_path = try std.process.getEnvVarOwned(self.arena, "ZIG_EXE");
 
         var progress = std.Progress{};
         const root_node = progress.start("compiler", self.cases.items.len);
@@ -1272,6 +1273,7 @@ pub const TestContext = struct {
                     &prg_node,
                     case.*,
                     zig_lib_directory,
+                    zig_exe_path,
                     &aux_thread_pool,
                     global_cache_directory,
                     host,
@@ -1298,6 +1300,7 @@ pub const TestContext = struct {
         root_node: *std.Progress.Node,
         case: Case,
         zig_lib_directory: Compilation.Directory,
+        zig_exe_path: []const u8,
         thread_pool: *ThreadPool,
         global_cache_directory: Compilation.Directory,
         host: std.zig.system.NativeTargetInfo,
@@ -1329,8 +1332,6 @@ pub const TestContext = struct {
             &[_][]const u8{ tmp_dir_path, "zig-cache" },
         );
 
-        const zig_exe_path = @import("test_options").zig_exe_path;
-
         for (case.files.items) |file| {
             try tmp.dir.writeFile(file.path, file.src);
         }
build.zig
@@ -40,17 +40,12 @@ pub fn build(b: *Builder) !void {
     const docs_step = b.step("docs", "Build documentation");
     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.setBuildMode(mode);
     test_cases.addPackagePath("test_cases", "test/cases.zig");
     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 skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;