build: refactor test code

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-09-21 11:24:28 -07:00
parent 164e52cd85
commit aaac8ebbfd

View file

@ -48,24 +48,24 @@ pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
if (target.getCpuArch() != .wasm32) { if (target.getCpuArch() != .wasm32) {
const tests_step = b.step("test", "Run tests"); // Creates a step for unit testing. This only builds the test executable
tests_step.dependOn(&testStep(b, optimize, target).step); // but does not run it.
} const unit_tests = b.addTest(.{
} .root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
var iter = module(b, optimize, target).dependencies.iterator();
while (iter.next()) |e| {
unit_tests.addModule(e.key_ptr.*, e.value_ptr.*);
}
fn testStep(b: *std.Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *std.build.RunStep { // Exposes a `test` step to the `zig build --help` menu, providing a way for the user to
const main_tests = b.addTest(.{ // request running the unit tests.
.name = "mach-tests", const run_unit_tests = b.addRunArtifact(unit_tests);
.root_source_file = .{ .path = "src/main.zig" }, const test_step = b.step("test", "Run unit tests");
.target = target, test_step.dependOn(&run_unit_tests.step);
.optimize = optimize,
});
var iter = module(b, optimize, target).dependencies.iterator();
while (iter.next()) |e| {
main_tests.addModule(e.key_ptr.*, e.value_ptr.*);
} }
b.installArtifact(main_tests);
return b.addRunArtifact(main_tests);
} }
pub const App = struct { pub const App = struct {