@@ -12,38 +12,58 @@ CLI testing for Zig.
1212
1313 This updates ` build.zig.zon ` .
1414
15- 2 . Write your test file. Example: ` test/echo .zig ` .
15+ 2 . Write your test file. Example: ` test/mycli .zig ` .
1616
1717 ``` zig
18- const std = @import("std");
19- const cmdtest = @import("cmdtest");
20- const testing = std.testing;
18+ const std = @import("std");
19+ const cmdtest = @import("cmdtest");
20+ const testing = std.testing;
2121
22- test "echo " {
23- const argv = &[_][]const u8{"echo", "hello "};
24- var result = try cmdtest.run(.{ .argv = argv });
25- defer result.deinit();
22+ test "via exe name " {
23+ const argv = &[_][]const u8{"mycli "};
24+ var result = try cmdtest.run(.{ .argv = argv });
25+ defer result.deinit();
2626
27- try testing.expectEqualStrings("hello\n", result.stdout);
28- }
27+ try testing.expectEqualStrings("project-exe\n", result.stderr);
28+ }
29+
30+ test "via path" {
31+ const argv = &[_][]const u8{"./zig-out/bin/mycli"};
32+ var result = try cmdtest.run(.{ .argv = argv });
33+ defer result.deinit();
34+
35+ try testing.expectEqualStrings("project-exe\n", result.stderr);
36+ }
2937 ```
3038
31393 . Register the test in ` build.zig ` :
3240
3341 ``` zig
34- const std = @import("std");
35- const cmdtest = @import("cmdtest");
36-
37- pub fn build(b: *std.Build) void {
38- // ...
39- const echo_test = cmdtest.add(b, .{
40- .name = "echo",
41- .test_file = b.path("test/echo.zig"),
42- });
43-
44- const test_step = b.step("test", "Run tests");
45- test_step.dependOn(&echo_test.step);
46- }
42+ const std = @import("std");
43+ const cmdtest = @import("cmdtest");
44+
45+ pub fn build(b: *std.Build) void {
46+ const target = b.standardTargetOptions(.{});
47+
48+ // Your CLI
49+ const cli = b.addExecutable(.{
50+ .name = "mycli",
51+ .root_module = b.createModule(.{
52+ .root_source_file = b.path("src/main.zig"),
53+ .target = target,
54+ }),
55+ });
56+ b.installArtifact(cli);
57+
58+ // Register new test
59+ const cli_test = cmdtest.add(b, .{
60+ .name = "mycli",
61+ .test_file = b.path("test/mycli.zig"),
62+ });
63+
64+ const test_step = b.step("test", "Run tests");
65+ test_step.dependOn(&cli_test.step);
66+ }
4767 ```
4868
49694 . Run the tests:
@@ -72,11 +92,11 @@ const cmdtest = @import("cmdtest");
7292const testing = std.testing;
7393
7494test "echo" {
75- const argv = &[_][]const u8{"echo", "hello"};
76- var result = try cmdtest.run(.{ .argv = argv });
77- defer result.deinit();
95+ const argv = &[_][]const u8{"echo", "hello"};
96+ var result = try cmdtest.run(.{ .argv = argv });
97+ defer result.deinit();
7898
79- try testing.expectEqualStrings("hello\n", result.stdout);
99+ try testing.expectEqualStrings("hello\n", result.stdout);
80100}
81101```
82102
@@ -88,12 +108,12 @@ const cmdtest = @import("cmdtest");
88108const testing = std.testing;
89109
90110test "cat" {
91- const argv = &[_][]const u8{"cat"};
92- const input = "a\nb\n";
93- var result = try cmdtest.run(.{ .argv = argv, .stdin = input });
94- defer result.deinit();
111+ const argv = &[_][]const u8{"cat"};
112+ const input = "a\nb\n";
113+ var result = try cmdtest.run(.{ .argv = argv, .stdin = input });
114+ defer result.deinit();
95115
96- try testing.expectEqualStrings(input, result.stdout);
116+ try testing.expectEqualStrings(input, result.stdout);
97117}
98118```
99119
0 commit comments