init-exe/init-lib template: fix use of install() and run()

And while we're at it, make the unit tests be actually executed.
This commit is contained in:
Andrew Kelley 2023-04-10 18:34:33 -07:00
parent 7221e9560e
commit 406706fe6b
2 changed files with 14 additions and 8 deletions

View file

@ -27,12 +27,12 @@ pub fn build(b: *std.Build) void {
// This declares intent for the executable to be installed into the // This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default // standard location when the user invokes the "install" step (the default
// step when running `zig build`). // step when running `zig build`).
exe.install(); b.installArtifact(exe);
// This *creates* a RunStep in the build graph, to be executed when another // 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 // step is evaluated that depends on it. The next line below will establish
// such a dependency. // 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 // 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. // 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"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. // Creates a step for unit testing. This only builds the test executable
const exe_tests = b.addTest(.{ // but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = .{ .path = "src/main.zig" },
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to // 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 // the `zig build --help` menu, providing a way for the user to request
// running the unit tests. // running the unit tests.
const test_step = b.step("test", "Run unit tests"); const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step); test_step.dependOn(&run_unit_tests.step);
} }

View file

@ -27,18 +27,21 @@ pub fn build(b: *std.Build) void {
// This declares intent for the library to be installed into the standard // This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when // location when the user invokes the "install" step (the default step when
// running `zig build`). // 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(.{ const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = .{ .path = "src/main.zig" },
.target = target, .target = target,
.optimize = optimize, .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, // This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test` // and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install". // This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step); test_step.dependOn(&run_main_tests.step);
} }