Testing with OS provided arguments

Hello,
I’m writing a(nother) CLI arg parser.
I want to test it by passing args through the OS.
I can write a shell script that would sequentially call my binary with arguments
but I thought I would ask if there is a more builtin way…

you can do something like this in build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
    // const mod = ...

    const test_exe = b.addExecutable(.{
        .name = "test_exe",
        .root_module = mod,
    });

    const run = b.addRunArtifact(test_exe);
    run.addArgs(&.{ "arg", "moreargs" });

    run.expectExitCode(0);
    run.expectStdOutEqual("output");

    const test_step = b.step("test", "run tests");

    test_step.dependOn(&run.step);
}
3 Likes

In addition, you can also allow for tests to be run on the full args passed to the build command:

    // This allows the user to pass arguments to the application in the build
    // command itself, like this: `zig build test -- arg1 arg2 etc`
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

The rest would be like @rpkak laid out.

1 Like

More options for how to do this in this thread:

2 Likes

Thank you all for your help!