From e02caa7e2909c0ae7f7e8ea1378d78e0469b9ff8 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 27 Oct 2021 15:28:33 -0700 Subject: [PATCH] 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. --- src/main.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main.zig b/src/main.zig index a2dd8d1d96..afe5e324c2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2188,6 +2188,16 @@ fn buildOutputType( warn("--watch is not recommended with the stage1 backend; it leaks memory and is not capable of incremental compilation", .{}); } + if (test_exec_args.items.len == 0 and object_format == .c) default_exec_args: { + // Default to using `zig run` to execute the produced .c code from `zig test`. + const c_code_loc = emit_bin_loc orelse break :default_exec_args; + const c_code_directory = c_code_loc.directory orelse comp.bin_file.options.emit.?.directory; + const c_code_path = try fs.path.join(arena, &[_][]const u8{ + c_code_directory.path orelse ".", c_code_loc.basename, + }); + try test_exec_args.appendSlice(&.{ self_exe_path, "run", "-lc", c_code_path }); + } + const run_or_test = switch (arg_mode) { .run => true, .zig_test => !test_no_exec,