Skip to content

Commit 014aabf

Browse files
committed
lib/init-exe, lib/init-lib: ensure tests runs again
After commit ede5dcf (make the build runner and test runner talk to each other), the std.Build.addTest function no longer runs tests, but the build.zig files in init-exe and init-lib where not updated. Rename main_tests to lib_tests and "Run library tests" to "Run unit tests", for consistency with init-exe. Use the new Build.addRunArtifact function, instead of the deprecated CompileStep.run method. Add a RunStep to exe_tests and lib_tests. In the addCliTests function in tests/test.zig: - set the new RunStep.is_test_command field to true, when running the `zig build test` command. See ziglang#15104. - Remove an empty line in the addCliTests function in tests/tests.zig. Closes ziglang#15009
1 parent 3eab605 commit 014aabf

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

lib/init-exe/build.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn build(b: *std.Build) void {
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.
@@ -59,9 +59,12 @@ pub fn build(b: *std.Build) void {
5959
.optimize = optimize,
6060
});
6161

62+
// A RunStep must be added to run the unit tests.
63+
const run_test = b.addRunArtifact(exe_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_test.step);
6770
}

lib/init-lib/build.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ pub fn build(b: *std.Build) void {
3030
lib.install();
3131

3232
// Creates a step for unit testing.
33-
const main_tests = b.addTest(.{
33+
const lib_tests = b.addTest(.{
3434
.root_source_file = .{ .path = "src/main.zig" },
3535
.target = target,
3636
.optimize = optimize,
3737
});
3838

39+
// A RunStep must be added to run the unit tests.
40+
const run_test = b.addRunArtifact(lib_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".
42-
const test_step = b.step("test", "Run library tests");
43-
test_step.dependOn(&main_tests.step);
45+
const test_step = b.step("test", "Run unit tests");
46+
test_step.dependOn(&run_test.step);
4447
}

lib/std/Build/RunStep.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ captured_stderr: ?*Output = null,
7373
has_side_effects: bool = false,
7474

7575
/// Set this to true if RunStep runs a zig test.
76-
/// See https://github.com/ziglang/zig/issues/15104.
76+
///
77+
/// This is necessary to prevent the first child process to close stdin, and
78+
/// the grandchild process to make stdin a pipe, causing the test executable to
79+
/// fail to spawn with a NotDir error.
7780
is_test_command: bool = false,
7881

7982
pub const StdIo = union(enum) {

test/tests.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ pub fn addCliTests(b: *std.Build) *Step {
663663
const s = std.fs.path.sep_str;
664664

665665
{
666-
667666
// Test `zig init-lib`.
668667
const tmp_path = b.makeTempPath();
669668
const init_lib = b.addSystemCommand(&.{ b.zig_exe, "init-lib" });
@@ -676,6 +675,7 @@ pub fn addCliTests(b: *std.Build) *Step {
676675

677676
const run_test = b.addSystemCommand(&.{ b.zig_exe, "build", "test" });
678677
run_test.cwd = tmp_path;
678+
run_test.is_test_command = true;
679679
run_test.setName("zig build test");
680680
run_test.expectStdOutEqual("");
681681
run_test.step.dependOn(&init_lib.step);
@@ -710,6 +710,7 @@ pub fn addCliTests(b: *std.Build) *Step {
710710

711711
const run_test = b.addSystemCommand(&.{ b.zig_exe, "build", "test" });
712712
run_test.cwd = tmp_path;
713+
run_test.is_test_command = true;
713714
run_test.setName("zig build test");
714715
run_test.expectStdOutEqual("");
715716
run_test.step.dependOn(&init_exe.step);

0 commit comments

Comments
 (0)