authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-27 15:28:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-27 15:28:33-07:00
loge02caa7e2909c0ae7f7e8ea1378d78e0469b9ff8
tree93bad2e6c3111bdc47b205a462585d30c79863dc
parent6504c57176a2a7def07c975af693940fe351bb19

zig test: when -ofmt=c, default to `zig run` as test exec

Previously when running `zig test` with the C backend, it would return `error.InvalidExe` because it would try to run the C code source file as a binary. Now, by default, it will try to run it with `zig run -lc foo.c` and this can be overridden with the standard `--test-cmd` flags.

1 files changed, 10 insertions(+), 0 deletions(-)

src/main.zig+10
...@@ -2188,6 +2188,16 @@ fn buildOutputType(...@@ -2188,6 +2188,16 @@ fn buildOutputType(
2188 warn("--watch is not recommended with the stage1 backend; it leaks memory and is not capable of incremental compilation", .{});2188 warn("--watch is not recommended with the stage1 backend; it leaks memory and is not capable of incremental compilation", .{});
2189 }2189 }
21902190
2191 if (test_exec_args.items.len == 0 and object_format == .c) default_exec_args: {
2192 // Default to using `zig run` to execute the produced .c code from `zig test`.
2193 const c_code_loc = emit_bin_loc orelse break :default_exec_args;
2194 const c_code_directory = c_code_loc.directory orelse comp.bin_file.options.emit.?.directory;
2195 const c_code_path = try fs.path.join(arena, &[_][]const u8{
2196 c_code_directory.path orelse ".", c_code_loc.basename,
2197 });
2198 try test_exec_args.appendSlice(&.{ self_exe_path, "run", "-lc", c_code_path });
2199 }
2200
2191 const run_or_test = switch (arg_mode) {2201 const run_or_test = switch (arg_mode) {
2192 .run => true,2202 .run => true,
2193 .zig_test => !test_no_exec,2203 .zig_test => !test_no_exec,