authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-14 18:02:21-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-09-14 18:02:21-04:00
logd834b180118cf336b09b8f664e294ec2222e6d16
tree1c3450ba81bed2e4125bf21e21ce2e03631ca53e
parent61aaef0b074dc2567e4c35c9cb55cb042c18d065
parent85b10eb07c27d021804473ee54a0b5942e4784d5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11633 from SpexGuy

Remove `std.testing.zig_exe_path` in favor of `ZIG_EXE` environment variable

6 files changed, 14 insertions(+), 44 deletions(-)

build.zig+1-1
......@@ -40,7 +40,7 @@ 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");
lib/std/process.zig-1
......@@ -822,7 +822,6 @@ test "args iterator" {
822822 const given_suffix = std.fs.path.basename(prog_name);
823823
824824 try testing.expect(mem.eql(u8, expected_suffix, given_suffix));
825 try testing.expect(it.skip()); // Skip over zig_exe_path, passed to the test runner
826825 try testing.expect(it.next() == null);
827826 try testing.expect(!it.skip());
828827}
lib/std/testing.zig-4
......@@ -22,10 +22,6 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");
2222/// TODO https://github.com/ziglang/zig/issues/5738
2323pub var log_level = std.log.Level.warn;
2424
25/// This is available to any test that wants to execute Zig in a child process.
26/// It will be the same executable that is running `zig test`.
27pub var zig_exe_path: []const u8 = undefined;
28
2925/// This function is intended to be used only in tests. It prints diagnostics to stderr
3026/// and then returns a test failure error when actual_error_union is not expected_error.
3127pub fn expectError(expected_error: anyerror, actual_error_union: anytype) !void {
lib/test_runner.zig-17
......@@ -6,29 +6,12 @@ pub const io_mode: io.Mode = builtin.test_io_mode;
66
77var log_err_count: usize = 0;
88
9var args_buffer: [std.fs.MAX_PATH_BYTES + std.mem.page_size]u8 = undefined;
10var args_allocator = std.heap.FixedBufferAllocator.init(&args_buffer);
11
12fn processArgs() void {
13 const args = std.process.argsAlloc(args_allocator.allocator()) catch {
14 @panic("Too many bytes passed over the CLI to the test runner");
15 };
16 if (args.len != 2) {
17 const self_name = if (args.len >= 1) args[0] else if (builtin.os.tag == .windows) "test.exe" else "test";
18 const zig_ext = if (builtin.os.tag == .windows) ".exe" else "";
19 std.debug.print("Usage: {s} path/to/zig{s}\n", .{ self_name, zig_ext });
20 @panic("Wrong number of command line arguments");
21 }
22 std.testing.zig_exe_path = args[1];
23}
24
259pub fn main() void {
2610 if (builtin.zig_backend != .stage1 and
2711 (builtin.zig_backend != .stage2_llvm or builtin.cpu.arch == .wasm32))
2812 {
2913 return main2() catch @panic("test failure");
3014 }
31 processArgs();
3215 const test_fn_list = builtin.test_functions;
3316 var ok_count: usize = 0;
3417 var skip_count: usize = 0;
src/main.zig+7-18
......@@ -3253,40 +3253,29 @@ fn runOrTest(
32533253 defer argv.deinit();
32543254
32553255 if (test_exec_args.len == 0) {
3256 // when testing pass the zig_exe_path to argv
3257 if (arg_mode == .zig_test)
3258 try argv.appendSlice(&[_][]const u8{
3259 exe_path, self_exe_path,
3260 })
3261 // when running just pass the current exe
3262 else
3263 try argv.appendSlice(&[_][]const u8{
3264 exe_path,
3265 });
3256 try argv.append(exe_path);
32663257 } else {
32673258 for (test_exec_args) |arg| {
3268 if (arg) |a| {
3269 try argv.append(a);
3270 } else {
3271 try argv.appendSlice(&[_][]const u8{
3272 exe_path, self_exe_path,
3273 });
3274 }
3259 try argv.append(arg orelse exe_path);
32753260 }
32763261 }
32773262 if (runtime_args_start) |i| {
32783263 try argv.appendSlice(all_args[i..]);
32793264 }
3265 var env_map = try std.process.getEnvMap(arena);
3266 try env_map.put("ZIG_EXE", self_exe_path);
3267
32803268 // We do not execve for tests because if the test fails we want to print
32813269 // the error message and invocation below.
32823270 if (std.process.can_execv and arg_mode == .run and !watch) {
32833271 // execv releases the locks; no need to destroy the Compilation here.
3284 const err = std.process.execv(gpa, argv.items);
3272 const err = std.process.execve(gpa, argv.items, &env_map);
32853273 try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc);
32863274 const cmd = try std.mem.join(arena, " ", argv.items);
32873275 fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });
32883276 } else if (std.process.can_spawn) {
32893277 var child = std.ChildProcess.init(argv.items, gpa);
3278 child.env_map = &env_map;
32903279 child.stdin_behavior = .Inherit;
32913280 child.stdout_behavior = .Inherit;
32923281 child.stderr_behavior = .Inherit;
src/test.zig+6-3
......@@ -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,
......@@ -1351,7 +1354,7 @@ pub const TestContext = struct {
13511354 try tmp.dir.writeFile(tmp_src_path, update.src);
13521355
13531356 var zig_args = std.ArrayList([]const u8).init(arena);
1354 try zig_args.append(std.testing.zig_exe_path);
1357 try zig_args.append(zig_exe_path);
13551358
13561359 if (case.is_test) {
13571360 try zig_args.append("test");
......@@ -1545,7 +1548,7 @@ pub const TestContext = struct {
15451548 .link_libc = case.link_libc,
15461549 .use_llvm = use_llvm,
15471550 .use_stage1 = null, // We already handled stage1 tests
1548 .self_exe_path = std.testing.zig_exe_path,
1551 .self_exe_path = zig_exe_path,
15491552 // TODO instead of turning off color, pass in a std.Progress.Node
15501553 .color = .off,
15511554 // TODO: force self-hosted linkers with stage2 backend to avoid LLD creeping in
......@@ -1795,7 +1798,7 @@ pub const TestContext = struct {
17951798 continue :update; // Pass test.
17961799 }
17971800 try argv.appendSlice(&[_][]const u8{
1798 std.testing.zig_exe_path,
1801 zig_exe_path,
17991802 "run",
18001803 "-cflags",
18011804 "-std=c99",