From 4056c6dbcb133824709fd5bc14bbb56d856f0080 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 4 Aug 2022 12:18:00 +0200 Subject: [PATCH] build: add test subcommand zig build test can now run tests defined in main.zig and all files it imports due to std.testing.refAllDecls. --- build.zig | 7 ++++++- src/main.zig | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index bcc8e50..6c9ded1 100644 --- a/build.zig +++ b/build.zig @@ -17,7 +17,12 @@ pub fn build(b: *std.build.Builder) void { if (b.args) |args| { run_cmd.addArgs(args); } - const run_step = b.step("run", "run the executable"); run_step.dependOn(&run_cmd.step); + + const exe_tests = b.addTest("src/main.zig"); + exe_tests.setTarget(target); + exe_tests.setBuildMode(bmode); + const test_step = b.step("test", "run tests"); + test_step.dependOn(&exe_tests.step); } diff --git a/src/main.zig b/src/main.zig index d67e08f..9bce252 100644 --- a/src/main.zig +++ b/src/main.zig @@ -176,3 +176,8 @@ fn zigStdPath(alloc: std.mem.Allocator) ![]const u8 { defer std.json.parseFree(Env, jenv, opt); return alloc.dupe(u8, jenv.std_dir); } + +test { + // run tests found in all @import'ed files. + std.testing.refAllDecls(@This()); +}