Skip to content

Commit 406706f

Browse files
committed
init-exe/init-lib template: fix use of install() and run()
And while we're at it, make the unit tests be actually executed.
1 parent 7221e95 commit 406706f

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/init-exe/build.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ pub fn build(b: *std.Build) void {
2727
// This declares intent for the executable to be installed into the
2828
// standard location when the user invokes the "install" step (the default
2929
// step when running `zig build`).
30-
exe.install();
30+
b.installArtifact(exe);
3131

3232
// This *creates* a RunStep in the build graph, to be executed when another
3333
// step is evaluated that depends on it. The next line below will establish
3434
// such a dependency.
35-
const run_cmd = exe.run();
35+
const run_cmd = b.addRunArtifact(exe);
3636

3737
// By making the run step depend on the install step, it will be run from the
3838
// installation directory rather than directly from within the cache directory.
@@ -52,16 +52,19 @@ pub fn build(b: *std.Build) void {
5252
const run_step = b.step("run", "Run the app");
5353
run_step.dependOn(&run_cmd.step);
5454

55-
// Creates a step for unit testing.
56-
const exe_tests = b.addTest(.{
55+
// Creates a step for unit testing. This only builds the test executable
56+
// but does not run it.
57+
const unit_tests = b.addTest(.{
5758
.root_source_file = .{ .path = "src/main.zig" },
5859
.target = target,
5960
.optimize = optimize,
6061
});
6162

63+
const run_unit_tests = b.addRunArtifact(unit_tests);
64+
6265
// Similar to creating the run step earlier, this exposes a `test` step to
6366
// the `zig build --help` menu, providing a way for the user to request
6467
// running the unit tests.
6568
const test_step = b.step("test", "Run unit tests");
66-
test_step.dependOn(&exe_tests.step);
69+
test_step.dependOn(&run_unit_tests.step);
6770
}

lib/init-lib/build.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ pub fn build(b: *std.Build) void {
2727
// This declares intent for the library to be installed into the standard
2828
// location when the user invokes the "install" step (the default step when
2929
// running `zig build`).
30-
lib.install();
30+
b.installArtifact(lib);
3131

32-
// Creates a step for unit testing.
32+
// Creates a step for unit testing. This only builds the test executable
33+
// but does not run it.
3334
const main_tests = b.addTest(.{
3435
.root_source_file = .{ .path = "src/main.zig" },
3536
.target = target,
3637
.optimize = optimize,
3738
});
3839

40+
const run_main_tests = b.addRunArtifact(main_tests);
41+
3942
// This creates a build step. It will be visible in the `zig build --help` menu,
4043
// and can be selected like this: `zig build test`
4144
// This will evaluate the `test` step rather than the default, which is "install".
4245
const test_step = b.step("test", "Run library tests");
43-
test_step.dependOn(&main_tests.step);
46+
test_step.dependOn(&run_main_tests.step);
4447
}

0 commit comments

Comments
 (0)