From 406706fe6b5e969028a7c70247ebd1cb2b93102d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 10 Apr 2023 18:34:33 -0700 Subject: [PATCH] init-exe/init-lib template: fix use of install() and run() And while we're at it, make the unit tests be actually executed. --- lib/init-exe/build.zig | 13 ++++++++----- lib/init-lib/build.zig | 9 ++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/init-exe/build.zig b/lib/init-exe/build.zig index 2ef5b21fe9..abf8654f0f 100644 --- a/lib/init-exe/build.zig +++ b/lib/init-exe/build.zig @@ -27,12 +27,12 @@ pub fn build(b: *std.Build) void { // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default // step when running `zig build`). - exe.install(); + b.installArtifact(exe); // This *creates* a RunStep in the build graph, to be executed when another // step is evaluated that depends on it. The next line below will establish // such a dependency. - const run_cmd = exe.run(); + const run_cmd = b.addRunArtifact(exe); // By making the run step depend on the install step, it will be run from the // installation directory rather than directly from within the cache directory. @@ -52,16 +52,19 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - // Creates a step for unit testing. - const exe_tests = b.addTest(.{ + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const unit_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, }); + const run_unit_tests = b.addRunArtifact(unit_tests); + // Similar to creating the run step earlier, this exposes a `test` step to // the `zig build --help` menu, providing a way for the user to request // running the unit tests. const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&exe_tests.step); + test_step.dependOn(&run_unit_tests.step); } diff --git a/lib/init-lib/build.zig b/lib/init-lib/build.zig index 2887c170e6..70592d896d 100644 --- a/lib/init-lib/build.zig +++ b/lib/init-lib/build.zig @@ -27,18 +27,21 @@ pub fn build(b: *std.Build) void { // This declares intent for the library to be installed into the standard // location when the user invokes the "install" step (the default step when // running `zig build`). - lib.install(); + b.installArtifact(lib); - // Creates a step for unit testing. + // Creates a step for unit testing. This only builds the test executable + // but does not run it. const main_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, }); + const run_main_tests = b.addRunArtifact(main_tests); + // This creates a build step. It will be visible in the `zig build --help` menu, // and can be selected like this: `zig build test` // This will evaluate the `test` step rather than the default, which is "install". const test_step = b.step("test", "Run library tests"); - test_step.dependOn(&main_tests.step); + test_step.dependOn(&run_main_tests.step); }