authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-10 18:34:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-10 18:35:14-07:00
log406706fe6b5e969028a7c70247ebd1cb2b93102d
treebe15ce4abc4ffe78efd0b5aaa522124741923ad1
parent7221e9560ef03e8298f8fe8f9b1610459fceb5c7

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

And while we're at it, make the unit tests be actually executed.

2 files changed, 14 insertions(+), 8 deletions(-)

lib/init-exe/build.zig+8-5
......@@ -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+6-3
......@@ -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}