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 {...@@ -27,12 +27,12 @@ pub fn build(b: *std.Build) void {
27 // This declares intent for the executable to be installed into the27 // This declares intent for the executable to be installed into the
28 // standard location when the user invokes the "install" step (the default28 // standard location when the user invokes the "install" step (the default
29 // step when running `zig build`).29 // step when running `zig build`).
30 exe.install();30 b.installArtifact(exe);
3131
32 // This *creates* a RunStep in the build graph, to be executed when another32 // This *creates* a RunStep in the build graph, to be executed when another
33 // step is evaluated that depends on it. The next line below will establish33 // step is evaluated that depends on it. The next line below will establish
34 // such a dependency.34 // such a dependency.
35 const run_cmd = exe.run();35 const run_cmd = b.addRunArtifact(exe);
3636
37 // By making the run step depend on the install step, it will be run from the37 // By making the run step depend on the install step, it will be run from the
38 // installation directory rather than directly from within the cache directory.38 // installation directory rather than directly from within the cache directory.
...@@ -52,16 +52,19 @@ pub fn build(b: *std.Build) void {...@@ -52,16 +52,19 @@ pub fn build(b: *std.Build) void {
52 const run_step = b.step("run", "Run the app");52 const run_step = b.step("run", "Run the app");
53 run_step.dependOn(&run_cmd.step);53 run_step.dependOn(&run_cmd.step);
5454
55 // Creates a step for unit testing.55 // Creates a step for unit testing. This only builds the test executable
56 const exe_tests = b.addTest(.{56 // but does not run it.
57 const unit_tests = b.addTest(.{
57 .root_source_file = .{ .path = "src/main.zig" },58 .root_source_file = .{ .path = "src/main.zig" },
58 .target = target,59 .target = target,
59 .optimize = optimize,60 .optimize = optimize,
60 });61 });
6162
63 const run_unit_tests = b.addRunArtifact(unit_tests);
64
62 // Similar to creating the run step earlier, this exposes a `test` step to65 // Similar to creating the run step earlier, this exposes a `test` step to
63 // the `zig build --help` menu, providing a way for the user to request66 // the `zig build --help` menu, providing a way for the user to request
64 // running the unit tests.67 // running the unit tests.
65 const test_step = b.step("test", "Run unit tests");68 const test_step = b.step("test", "Run unit tests");
66 test_step.dependOn(&exe_tests.step);69 test_step.dependOn(&run_unit_tests.step);
67}70}
lib/init-lib/build.zig+6-3
...@@ -27,18 +27,21 @@ pub fn build(b: *std.Build) void {...@@ -27,18 +27,21 @@ pub fn build(b: *std.Build) void {
27 // This declares intent for the library to be installed into the standard27 // This declares intent for the library to be installed into the standard
28 // location when the user invokes the "install" step (the default step when28 // location when the user invokes the "install" step (the default step when
29 // running `zig build`).29 // 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.
33 const main_tests = b.addTest(.{34 const main_tests = b.addTest(.{
34 .root_source_file = .{ .path = "src/main.zig" },35 .root_source_file = .{ .path = "src/main.zig" },
35 .target = target,36 .target = target,
36 .optimize = optimize,37 .optimize = optimize,
37 });38 });
3839
40 const run_main_tests = b.addRunArtifact(main_tests);
41
39 // This creates a build step. It will be visible in the `zig build --help` menu,42 // This creates a build step. It will be visible in the `zig build --help` menu,
40 // and can be selected like this: `zig build test`43 // and can be selected like this: `zig build test`
41 // This will evaluate the `test` step rather than the default, which is "install".44 // This will evaluate the `test` step rather than the default, which is "install".
42 const test_step = b.step("test", "Run library tests");45 const test_step = b.step("test", "Run library tests");
43 test_step.dependOn(&main_tests.step);46 test_step.dependOn(&run_main_tests.step);
44}47}