Skip to content

Instantly share code, notes, and snippets.

@perillo
Last active March 28, 2023 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perillo/fcafbe87e5364e68e5bf70b3f4280499 to your computer and use it in GitHub Desktop.
Save perillo/fcafbe87e5364e68e5bf70b3f4280499 to your computer and use it in GitHub Desktop.

Build file

const std = @import("std");
const tests = @import("tests-tmp.zig");

pub fn build(b: *std.Build) void {
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(tests.addCliTests(b));
}

Test file

const std = @import("std");
const Step = std.Build.Step;

pub fn addCliTests(b: *std.Build) *Step {
    const step = b.step("test-cli", "Test the command line interface");
    const s = std.fs.path.sep_str;

    // Test `zig init-lib`.
    const tmp_path = b.makeTempPath();
    const init_lib = b.addSystemCommand(&.{ b.zig_exe, "init-lib" });
    init_lib.cwd = tmp_path;
    init_lib.setName("zig init-lib");
    init_lib.expectStdOutEqual("");
    init_lib.expectStdErrEqual("info: Created build.zig\n" ++
        "info: Created src" ++ s ++ "main.zig\n" ++
        "info: Next, try `zig build --help` or `zig build test`\n");

    const run_test = b.addSystemCommand(&.{ b.zig_exe, "build", "test" });
    run_test.cwd = tmp_path;
    run_test.setName("zig build test");
    run_test.expectStdOutEqual("");
    run_test.step.dependOn(&init_lib.step);

    const cleanup = b.addRemoveDirTree(tmp_path);
    cleanup.step.dependOn(&run_test.step);

    step.dependOn(&cleanup.step);

    return step;
}
strace -f -v -s 80 build/stage3/bin/zig build --build-file build-tmp.zig test &> /tmp/strace 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment