Commit 5523e2061b

Martin Wickham <spexguy070@gmail.com>
2022-05-11 09:22:13
Move std.testing.zig_exe_path into build options
1 parent 61aaef0
lib/std/process.zig
@@ -822,7 +822,6 @@ test "args iterator" {
     const given_suffix = std.fs.path.basename(prog_name);
 
     try testing.expect(mem.eql(u8, expected_suffix, given_suffix));
-    try testing.expect(it.skip()); // Skip over zig_exe_path, passed to the test runner
     try testing.expect(it.next() == null);
     try testing.expect(!it.skip());
 }
lib/std/testing.zig
@@ -22,10 +22,6 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");
 /// TODO https://github.com/ziglang/zig/issues/5738
 pub var log_level = std.log.Level.warn;
 
-/// This is available to any test that wants to execute Zig in a child process.
-/// It will be the same executable that is running `zig test`.
-pub var zig_exe_path: []const u8 = undefined;
-
 /// This function is intended to be used only in tests. It prints diagnostics to stderr
 /// and then returns a test failure error when actual_error_union is not expected_error.
 pub fn expectError(expected_error: anyerror, actual_error_union: anytype) !void {
lib/test_runner.zig
@@ -6,29 +6,12 @@ pub const io_mode: io.Mode = builtin.test_io_mode;
 
 var log_err_count: usize = 0;
 
-var args_buffer: [std.fs.MAX_PATH_BYTES + std.mem.page_size]u8 = undefined;
-var args_allocator = std.heap.FixedBufferAllocator.init(&args_buffer);
-
-fn processArgs() void {
-    const args = std.process.argsAlloc(args_allocator.allocator()) catch {
-        @panic("Too many bytes passed over the CLI to the test runner");
-    };
-    if (args.len != 2) {
-        const self_name = if (args.len >= 1) args[0] else if (builtin.os.tag == .windows) "test.exe" else "test";
-        const zig_ext = if (builtin.os.tag == .windows) ".exe" else "";
-        std.debug.print("Usage: {s} path/to/zig{s}\n", .{ self_name, zig_ext });
-        @panic("Wrong number of command line arguments");
-    }
-    std.testing.zig_exe_path = args[1];
-}
-
 pub fn main() void {
     if (builtin.zig_backend != .stage1 and
         (builtin.zig_backend != .stage2_llvm or builtin.cpu.arch == .wasm32))
     {
         return main2() catch @panic("test failure");
     }
-    processArgs();
     const test_fn_list = builtin.test_functions;
     var ok_count: usize = 0;
     var skip_count: usize = 0;
src/main.zig
@@ -3057,7 +3057,6 @@ fn buildOutputType(
             gpa,
             arena,
             test_exec_args.items,
-            self_exe_path,
             arg_mode,
             target_info,
             watch,
@@ -3129,7 +3128,6 @@ fn buildOutputType(
                         gpa,
                         arena,
                         test_exec_args.items,
-                        self_exe_path,
                         arg_mode,
                         target_info,
                         watch,
@@ -3154,7 +3152,6 @@ fn buildOutputType(
                         gpa,
                         arena,
                         test_exec_args.items,
-                        self_exe_path,
                         arg_mode,
                         target_info,
                         watch,
@@ -3233,7 +3230,6 @@ 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,
@@ -3253,25 +3249,10 @@ fn runOrTest(
     defer argv.deinit();
 
     if (test_exec_args.len == 0) {
-        // when testing pass the zig_exe_path to argv
-        if (arg_mode == .zig_test)
-            try argv.appendSlice(&[_][]const u8{
-                exe_path, self_exe_path,
-            })
-            // when running just pass the current exe
-        else
-            try argv.appendSlice(&[_][]const u8{
-                exe_path,
-            });
+        try argv.append(exe_path);
     } else {
         for (test_exec_args) |arg| {
-            if (arg) |a| {
-                try argv.append(a);
-            } else {
-                try argv.appendSlice(&[_][]const u8{
-                    exe_path, self_exe_path,
-                });
-            }
+            try argv.append(arg orelse exe_path);
         }
     }
     if (runtime_args_start) |i| {
src/test.zig
@@ -1329,6 +1329,8 @@ 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);
         }
@@ -1351,7 +1353,7 @@ pub const TestContext = struct {
             try tmp.dir.writeFile(tmp_src_path, update.src);
 
             var zig_args = std.ArrayList([]const u8).init(arena);
-            try zig_args.append(std.testing.zig_exe_path);
+            try zig_args.append(zig_exe_path);
 
             if (case.is_test) {
                 try zig_args.append("test");
@@ -1545,7 +1547,7 @@ pub const TestContext = struct {
             .link_libc = case.link_libc,
             .use_llvm = use_llvm,
             .use_stage1 = null, // We already handled stage1 tests
-            .self_exe_path = std.testing.zig_exe_path,
+            .self_exe_path = zig_exe_path,
             // TODO instead of turning off color, pass in a std.Progress.Node
             .color = .off,
             // TODO: force self-hosted linkers with stage2 backend to avoid LLD creeping in
@@ -1795,7 +1797,7 @@ pub const TestContext = struct {
                                 continue :update; // Pass test.
                             }
                             try argv.appendSlice(&[_][]const u8{
-                                std.testing.zig_exe_path,
+                                zig_exe_path,
                                 "run",
                                 "-cflags",
                                 "-std=c99",
build.zig
@@ -46,6 +46,11 @@ pub fn build(b: *Builder) !void {
     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;