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 {...@@ -46,6 +46,11 @@ pub fn build(b: *Builder) !void {
46 test_cases.addPackagePath("test_cases", "test/cases.zig");46 test_cases.addPackagePath("test_cases", "test/cases.zig");
47 test_cases.single_threaded = single_threaded;47 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
49 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});54 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
5055
51 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;56 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" {...@@ -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+2-21
...@@ -3057,7 +3057,6 @@ fn buildOutputType(...@@ -3057,7 +3057,6 @@ fn buildOutputType(
3057 gpa,3057 gpa,
3058 arena,3058 arena,
3059 test_exec_args.items,3059 test_exec_args.items,
3060 self_exe_path,
3061 arg_mode,3060 arg_mode,
3062 target_info,3061 target_info,
3063 watch,3062 watch,
...@@ -3129,7 +3128,6 @@ fn buildOutputType(...@@ -3129,7 +3128,6 @@ fn buildOutputType(
3129 gpa,3128 gpa,
3130 arena,3129 arena,
3131 test_exec_args.items,3130 test_exec_args.items,
3132 self_exe_path,
3133 arg_mode,3131 arg_mode,
3134 target_info,3132 target_info,
3135 watch,3133 watch,
...@@ -3154,7 +3152,6 @@ fn buildOutputType(...@@ -3154,7 +3152,6 @@ fn buildOutputType(
3154 gpa,3152 gpa,
3155 arena,3153 arena,
3156 test_exec_args.items,3154 test_exec_args.items,
3157 self_exe_path,
3158 arg_mode,3155 arg_mode,
3159 target_info,3156 target_info,
3160 watch,3157 watch,
...@@ -3233,7 +3230,6 @@ fn runOrTest(...@@ -3233,7 +3230,6 @@ fn runOrTest(
3233 gpa: Allocator,3230 gpa: Allocator,
3234 arena: Allocator,3231 arena: Allocator,
3235 test_exec_args: []const ?[]const u8,3232 test_exec_args: []const ?[]const u8,
3236 self_exe_path: []const u8,
3237 arg_mode: ArgMode,3233 arg_mode: ArgMode,
3238 target_info: std.zig.system.NativeTargetInfo,3234 target_info: std.zig.system.NativeTargetInfo,
3239 watch: bool,3235 watch: bool,
...@@ -3253,25 +3249,10 @@ fn runOrTest(...@@ -3253,25 +3249,10 @@ fn runOrTest(
3253 defer argv.deinit();3249 defer argv.deinit();
32543250
3255 if (test_exec_args.len == 0) {3251 if (test_exec_args.len == 0) {
3256 // when testing pass the zig_exe_path to argv3252 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 {3253 } else {
3267 for (test_exec_args) |arg| {3254 for (test_exec_args) |arg| {
3268 if (arg) |a| {3255 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 }3256 }
3276 }3257 }
3277 if (runtime_args_start) |i| {3258 if (runtime_args_start) |i| {
src/test.zig+5-3
...@@ -1329,6 +1329,8 @@ pub const TestContext = struct {...@@ -1329,6 +1329,8 @@ pub const TestContext = struct {
1329 &[_][]const u8{ tmp_dir_path, "zig-cache" },1329 &[_][]const u8{ tmp_dir_path, "zig-cache" },
1330 );1330 );
13311331
1332 const zig_exe_path = @import("test_options").zig_exe_path;
1333
1332 for (case.files.items) |file| {1334 for (case.files.items) |file| {
1333 try tmp.dir.writeFile(file.path, file.src);1335 try tmp.dir.writeFile(file.path, file.src);
1334 }1336 }
...@@ -1351,7 +1353,7 @@ pub const TestContext = struct {...@@ -1351,7 +1353,7 @@ pub const TestContext = struct {
1351 try tmp.dir.writeFile(tmp_src_path, update.src);1353 try tmp.dir.writeFile(tmp_src_path, update.src);
13521354
1353 var zig_args = std.ArrayList([]const u8).init(arena);1355 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
1356 if (case.is_test) {1358 if (case.is_test) {
1357 try zig_args.append("test");1359 try zig_args.append("test");
...@@ -1545,7 +1547,7 @@ pub const TestContext = struct {...@@ -1545,7 +1547,7 @@ pub const TestContext = struct {
1545 .link_libc = case.link_libc,1547 .link_libc = case.link_libc,
1546 .use_llvm = use_llvm,1548 .use_llvm = use_llvm,
1547 .use_stage1 = null, // We already handled stage1 tests1549 .use_stage1 = null, // We already handled stage1 tests
1548 .self_exe_path = std.testing.zig_exe_path,1550 .self_exe_path = zig_exe_path,
1549 // TODO instead of turning off color, pass in a std.Progress.Node1551 // TODO instead of turning off color, pass in a std.Progress.Node
1550 .color = .off,1552 .color = .off,
1551 // TODO: force self-hosted linkers with stage2 backend to avoid LLD creeping in1553 // TODO: force self-hosted linkers with stage2 backend to avoid LLD creeping in
...@@ -1795,7 +1797,7 @@ pub const TestContext = struct {...@@ -1795,7 +1797,7 @@ pub const TestContext = struct {
1795 continue :update; // Pass test.1797 continue :update; // Pass test.
1796 }1798 }
1797 try argv.appendSlice(&[_][]const u8{1799 try argv.appendSlice(&[_][]const u8{
1798 std.testing.zig_exe_path,1800 zig_exe_path,
1799 "run",1801 "run",
1800 "-cflags",1802 "-cflags",
1801 "-std=c99",1803 "-std=c99",