Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zig build system does not fail when a test fails #15009

Closed
Hejsil opened this issue Mar 19, 2023 · 10 comments
Closed

Zig build system does not fail when a test fails #15009

Hejsil opened this issue Mar 19, 2023 · 10 comments
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. enhancement Solving this issue will likely involve adding new logic or components to the codebase. zig build system std.Build, the build runner, `zig build` subcommand, package management
Milestone

Comments

@Hejsil
Copy link
Contributor

Hejsil commented Mar 19, 2023

Zig Version

0.11.0-dev.2165+8f481dfc3

Steps to Reproduce and Observed Behavior

// build.zig
const std = @import("std");

pub fn build(b: *std.Build) void {
    const exe_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
    });
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&exe_tests.step);
}
// src/main.zig
test "simple test" {
    unreachable;
}
$ zig build test

Expected Behavior

I expect to get a stack trace in my terminal showing the test failure. Instead, the command silently exits with a successful status code.

Running zig build test --verbose we can see that it seems to be actually running the test:

$ zig build test --verbose
<path-to>/zig test <path-to>/src/main.zig --cache-dir <path-to>/zig-cache --global-cache-dir <path-to>/.cache/zig --name test --enable-cache --listen=-

If I take the same command, remove --listen=-, this works just fine:

$ <path-to>/zig test <path-to>/src/main.zig --cache-dir <path-to>/zig-cache --global-cache-dir <path-to>/.cache/zig --name test --enable-cache
<path-to>/zig-cache/o/25622b855bc097280dff3f8cb897732b
Test [1/1] test.simple test... thread 80083 panic: reached unreachable code
<path-to>/src/main.zig:2:5: 0x20dc95 in test.simple test (test)
    unreachable;
    ^
...

@Hejsil Hejsil added the bug Observed behavior contradicts documented or intended behavior label Mar 19, 2023
@scheibo
Copy link
Contributor

scheibo commented Mar 19, 2023

After #14647:

There is no difference between test and test_exe anymore, so the latter is gone. You have to add a run step for test execution now - that's a breaking change

@Hejsil
Copy link
Contributor Author

Hejsil commented Mar 19, 2023

After #14647:

There is no difference between test and test_exe anymore, so the latter is gone. You have to add a run step for test execution now - that's a breaking change

That does seem to be the issue. Seems you now need:

test_step.dependOn(&exe_tests.run().step);

Would be nice if zig init-exe had this as I turned to that command to do a small reproduction :)

@scheibo
Copy link
Contributor

scheibo commented Mar 19, 2023

Would be nice if zig init-exe had this as I turned to that command to do a small reproduction :)

Agreed! And I'm sure this will be called out in the release notes when theyre written, because I also found this to be a rather nefarious breaking change (a good breaking change would break everyones tests. this one sadly makes them all pass!)

@andrewrk
Copy link
Member

I agree it is nefarious - perhaps addTest could be renamed to addTestExecutable and the previous name could be a compile error that explains the new behavior.

perillo added a commit to perillo/zig that referenced this issue Mar 24, 2023
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 in init-lib, for consistency with the
exe_tests variable in init-exe.

Add a RunStep to exe_tests and lib_tests.

Remove an empty line in the addCliTests function in tests/tests.zig.

Closes ziglang#15009
perillo added a commit to perillo/zig that referenced this issue Mar 26, 2023
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 in init-lib, for consistency with the
exe_tests variable in init-exe.

Add a RunStep to exe_tests and lib_tests.

Remove an empty line in the addCliTests function in tests/tests.zig.

Closes ziglang#15009
perillo added a commit to perillo/zig that referenced this issue Mar 26, 2023
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.

Add a RunStep to exe_tests and lib_tests, using the new
Build.addRunArtifact function.

Remove an empty line in the addCliTests function in tests/tests.zig.

Closes ziglang#15009
perillo added a commit to perillo/zig that referenced this issue Mar 31, 2023
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
perillo added a commit to perillo/zig that referenced this issue Mar 31, 2023
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
perillo added a commit to perillo/zig that referenced this issue Apr 3, 2023
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
@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. zig build system std.Build, the build runner, `zig build` subcommand, package management labels Apr 10, 2023
@andrewrk andrewrk added this to the 0.11.0 milestone Apr 10, 2023
@nurpax
Copy link
Contributor

nurpax commented May 6, 2023

I'm trying to get my tests to run after upgrading to Zig 0.11.0-dev.2892+fd6200eda but I just can't get the tests to actually run. Just like OP, if I take the last test execution command from zig build test --verbose and remove the -listen=- flag, tests run just fine. I thought I'm doing everything below that's been discussed in this thread:

    const test_exe = b.addTest(
        .{
            .target = target,
            .optimize = optimize,
            .root_source_file = .{ .path = "src/main.zig" },
        },
    );
    addMainDeps(b, test_exe, target, optimize);
    test_exe.addCSourceFile("src/sgtesting.c", &.{});
    b.installArtifact(test_exe);
    const run_test = b.addRunArtifact(test_exe);
    if (b.args) |args| {
        run_test.addArgs(args);
    }
    const test_step = b.step("test", "Run tests");
    test_step.dependOn(&run_test.step);

@leecannon
Copy link
Contributor

leecannon commented May 6, 2023

@nurpax if you run zig build test -fsummary what does it show?

If the run step shows as cached it means the code hasn't changed so the tests won't run again, but the summary will include the number of passed test cases from the prior execution.

To force the tests to always run you would need to add run_test.has_side_effects = true;.

@nurpax
Copy link
Contributor

nurpax commented May 6, 2023

C:\Users\janne\dev\platformerzig>zig build -fsummary test
Build Summary: 3/3 steps succeeded
test cached
+- run test cached
   +- zig test Debug native cached 1s MaxRSS:22M

If I run the test.exe from the zig-cache dir, here's what it prints:

All 12 tests passed.

Adding run_test.has_side_effetcs=true, I see:

Build Summary: 3/3 steps succeeded; 12/12 tests passed
test success
+- run test 12 passed 246ms MaxRSS:25M
   +- zig test Debug native cached 1s MaxRSS:22M

Without -fsummary, it prints nothing. It's a little confusing.

@andrewrk andrewrk modified the milestones: 0.11.0, 0.11.1 Jul 24, 2023
@andrewrk andrewrk modified the milestones: 0.11.1, 0.12.0 Jan 29, 2024
@perillo
Copy link
Contributor

perillo commented Feb 8, 2024

@Vexu: wasn't this fixed in 406706f?

@Vexu
Copy link
Member

Vexu commented Feb 8, 2024

Yes, but it could still be mitigated like #15009 (comment) suggests.

I'll leave it to Andrew to decide.

@andrewrk andrewrk removed the bug Observed behavior contradicts documented or intended behavior label Mar 21, 2024
@andrewrk
Copy link
Member

I apologize for handling this upgrade poorly. I should have done it in a strategy that caused compile errors in the build script rather than changing the behavior silently. However, I think the opportunity to handle the upgrade more smoothly has passed, and so I am going to merely improve the doc comments for addTest and then move on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. enhancement Solving this issue will likely involve adding new logic or components to the codebase. zig build system std.Build, the build runner, `zig build` subcommand, package management
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants