authorgravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2022-05-11 02:22:13-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-14 14:42:58-07:00
log5523e2061b37de2b884c1c53fdcefb823755c7d0
treeca47846fc11ebbbefd0d2c8d1f35b17887407ec1
parent61aaef0b074dc2567e4c35c9cb55cb042c18d065

Move std.testing.zig_exe_path into build options


6 files changed, 12 insertions(+), 46 deletions(-)

build.zig+5
......@@ -46,6 +46,11 @@ pub fn build(b: *Builder) !void {
4646 test_cases.addPackagePath("test_cases", "test/cases.zig");
4747 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
4954 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
5055
5156 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;
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+2-21
......@@ -3057,7 +3057,6 @@ fn buildOutputType(
30573057 gpa,
30583058 arena,
30593059 test_exec_args.items,
3060 self_exe_path,
30613060 arg_mode,
30623061 target_info,
30633062 watch,
......@@ -3129,7 +3128,6 @@ fn buildOutputType(
31293128 gpa,
31303129 arena,
31313130 test_exec_args.items,
3132 self_exe_path,
31333131 arg_mode,
31343132 target_info,
31353133 watch,
......@@ -3154,7 +3152,6 @@ fn buildOutputType(
31543152 gpa,
31553153 arena,
31563154 test_exec_args.items,
3157 self_exe_path,
31583155 arg_mode,
31593156 target_info,
31603157 watch,
......@@ -3233,7 +3230,6 @@ fn runOrTest(
32333230 gpa: Allocator,
32343231 arena: Allocator,
32353232 test_exec_args: []const ?[]const u8,
3236 self_exe_path: []const u8,
32373233 arg_mode: ArgMode,
32383234 target_info: std.zig.system.NativeTargetInfo,
32393235 watch: bool,
......@@ -3253,25 +3249,10 @@ fn runOrTest(
32533249 defer argv.deinit();
32543250
32553251 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 });
3252 try argv.append(exe_path);
32663253 } else {
32673254 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 }
3255 try argv.append(arg orelse exe_path);
32753256 }
32763257 }
32773258 if (runtime_args_start) |i| {
src/test.zig+5-3
......@@ -1329,6 +1329,8 @@ pub const TestContext = struct {
13291329 &[_][]const u8{ tmp_dir_path, "zig-cache" },
13301330 );
13311331
1332 const zig_exe_path = @import("test_options").zig_exe_path;
1333
13321334 for (case.files.items) |file| {
13331335 try tmp.dir.writeFile(file.path, file.src);
13341336 }
......@@ -1351,7 +1353,7 @@ pub const TestContext = struct {
13511353 try tmp.dir.writeFile(tmp_src_path, update.src);
13521354
13531355 var zig_args = std.ArrayList([]const u8).init(arena);
1354 try zig_args.append(std.testing.zig_exe_path);
1356 try zig_args.append(zig_exe_path);
13551357
13561358 if (case.is_test) {
13571359 try zig_args.append("test");
......@@ -1545,7 +1547,7 @@ pub const TestContext = struct {
15451547 .link_libc = case.link_libc,
15461548 .use_llvm = use_llvm,
15471549 .use_stage1 = null, // We already handled stage1 tests
1548 .self_exe_path = std.testing.zig_exe_path,
1550 .self_exe_path = zig_exe_path,
15491551 // TODO instead of turning off color, pass in a std.Progress.Node
15501552 .color = .off,
15511553 // TODO: force self-hosted linkers with stage2 backend to avoid LLD creeping in
......@@ -1795,7 +1797,7 @@ pub const TestContext = struct {
17951797 continue :update; // Pass test.
17961798 }
17971799 try argv.appendSlice(&[_][]const u8{
1798 std.testing.zig_exe_path,
1800 zig_exe_path,
17991801 "run",
18001802 "-cflags",
18011803 "-std=c99",