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 {...@@ -40,7 +40,7 @@ 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");
lib/std/process.zig-1
...@@ -822,7 +822,6 @@ test "args iterator" {...@@ -822,7 +822,6 @@ test "args iterator" {
822 const given_suffix = std.fs.path.basename(prog_name);822 const given_suffix = std.fs.path.basename(prog_name);
823823
824 try testing.expect(mem.eql(u8, expected_suffix, given_suffix));824 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
826 try testing.expect(it.next() == null);825 try testing.expect(it.next() == null);
827 try testing.expect(!it.skip());826 try testing.expect(!it.skip());
828}827}
lib/std/testing.zig-4
...@@ -22,10 +22,6 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");...@@ -22,10 +22,6 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");
22/// TODO https://github.com/ziglang/zig/issues/573822/// TODO https://github.com/ziglang/zig/issues/5738
23pub var log_level = std.log.Level.warn;23pub 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
29/// This function is intended to be used only in tests. It prints diagnostics to stderr25/// This function is intended to be used only in tests. It prints diagnostics to stderr
30/// and then returns a test failure error when actual_error_union is not expected_error.26/// and then returns a test failure error when actual_error_union is not expected_error.
31pub fn expectError(expected_error: anyerror, actual_error_union: anytype) !void {27pub 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;...@@ -6,29 +6,12 @@ pub const io_mode: io.Mode = builtin.test_io_mode;
66
7var log_err_count: usize = 0;7var 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
25pub fn main() void {9pub fn main() void {
26 if (builtin.zig_backend != .stage1 and10 if (builtin.zig_backend != .stage1 and
27 (builtin.zig_backend != .stage2_llvm or builtin.cpu.arch == .wasm32))11 (builtin.zig_backend != .stage2_llvm or builtin.cpu.arch == .wasm32))
28 {12 {
29 return main2() catch @panic("test failure");13 return main2() catch @panic("test failure");
30 }14 }
31 processArgs();
32 const test_fn_list = builtin.test_functions;15 const test_fn_list = builtin.test_functions;
33 var ok_count: usize = 0;16 var ok_count: usize = 0;
34 var skip_count: usize = 0;17 var skip_count: usize = 0;
src/main.zig+7-18
...@@ -3253,40 +3253,29 @@ fn runOrTest(...@@ -3253,40 +3253,29 @@ fn runOrTest(
3253 defer argv.deinit();3253 defer argv.deinit();
32543254
3255 if (test_exec_args.len == 0) {3255 if (test_exec_args.len == 0) {
3256 // when testing pass the zig_exe_path to argv3256 try argv.append(exe_path);
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 });
3266 } else {3257 } else {
3267 for (test_exec_args) |arg| {3258 for (test_exec_args) |arg| {
3268 if (arg) |a| {3259 try argv.append(arg orelse exe_path);
3269 try argv.append(a);
3270 } else {
3271 try argv.appendSlice(&[_][]const u8{
3272 exe_path, self_exe_path,
3273 });
3274 }
3275 }3260 }
3276 }3261 }
3277 if (runtime_args_start) |i| {3262 if (runtime_args_start) |i| {
3278 try argv.appendSlice(all_args[i..]);3263 try argv.appendSlice(all_args[i..]);
3279 }3264 }
3265 var env_map = try std.process.getEnvMap(arena);
3266 try env_map.put("ZIG_EXE", self_exe_path);
3267
3280 // 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
3281 // the error message and invocation below.3269 // the error message and invocation below.
3282 if (std.process.can_execv and arg_mode == .run and !watch) {3270 if (std.process.can_execv and arg_mode == .run and !watch) {
3283 // execv releases the locks; no need to destroy the Compilation here.3271 // 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);
3285 try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc);3273 try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc);
3286 const cmd = try std.mem.join(arena, " ", argv.items);3274 const cmd = try std.mem.join(arena, " ", argv.items);
3287 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 });
3288 } else if (std.process.can_spawn) {3276 } else if (std.process.can_spawn) {
3289 var child = std.ChildProcess.init(argv.items, gpa);3277 var child = std.ChildProcess.init(argv.items, gpa);
3278 child.env_map = &env_map;
3290 child.stdin_behavior = .Inherit;3279 child.stdin_behavior = .Inherit;
3291 child.stdout_behavior = .Inherit;3280 child.stdout_behavior = .Inherit;
3292 child.stderr_behavior = .Inherit;3281 child.stderr_behavior = .Inherit;
src/test.zig+6-3
...@@ -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,
...@@ -1351,7 +1354,7 @@ pub const TestContext = struct {...@@ -1351,7 +1354,7 @@ pub const TestContext = struct {
1351 try tmp.dir.writeFile(tmp_src_path, update.src);1354 try tmp.dir.writeFile(tmp_src_path, update.src);
13521355
1353 var zig_args = std.ArrayList([]const u8).init(arena);1356 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
1356 if (case.is_test) {1359 if (case.is_test) {
1357 try zig_args.append("test");1360 try zig_args.append("test");
...@@ -1545,7 +1548,7 @@ pub const TestContext = struct {...@@ -1545,7 +1548,7 @@ pub const TestContext = struct {
1545 .link_libc = case.link_libc,1548 .link_libc = case.link_libc,
1546 .use_llvm = use_llvm,1549 .use_llvm = use_llvm,
1547 .use_stage1 = null, // We already handled stage1 tests1550 .use_stage1 = null, // We already handled stage1 tests
1548 .self_exe_path = std.testing.zig_exe_path,1551 .self_exe_path = zig_exe_path,
1549 // TODO instead of turning off color, pass in a std.Progress.Node1552 // TODO instead of turning off color, pass in a std.Progress.Node
1550 .color = .off,1553 .color = .off,
1551 // TODO: force self-hosted linkers with stage2 backend to avoid LLD creeping in1554 // TODO: force self-hosted linkers with stage2 backend to avoid LLD creeping in
...@@ -1795,7 +1798,7 @@ pub const TestContext = struct {...@@ -1795,7 +1798,7 @@ pub const TestContext = struct {
1795 continue :update; // Pass test.1798 continue :update; // Pass test.
1796 }1799 }
1797 try argv.appendSlice(&[_][]const u8{1800 try argv.appendSlice(&[_][]const u8{
1798 std.testing.zig_exe_path,1801 zig_exe_path,
1799 "run",1802 "run",
1800 "-cflags",1803 "-cflags",
1801 "-std=c99",1804 "-std=c99",