authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-03 01:35:36-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-04 00:27:09-08:00
log2fee64ceb08328ccc9f98879a544eae971248493
tree5f4d947be13aebc656e30baf6502cb410d32e459
parent0b856d12a09df8e9b3a09494ec5488b771c20d0b

update init template for new main API


3 files changed, 26 insertions(+), 17 deletions(-)

lib/init/src/main.zig+11-9
......@@ -3,19 +3,21 @@ const Io = std.Io;
33
44const _NAME = @import(".NAME");
55
6pub fn main() !void {
6pub fn main(init: std.process.Init) !void {
77 // Prints to stderr, unbuffered, ignoring potential errors.
88 std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
99
10 // In order to allocate memory we must construct an `Allocator` instance.
11 var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
12 defer _ = debug_allocator.deinit(); // This checks for leaks.
13 const gpa = debug_allocator.allocator();
10 // This is appropriate for anything that lives as long as the process.
11 const arena: std.mem.Allocator = init.arena.allocator();
1412
15 // In order to do I/O operations we must construct an `Io` instance.
16 var threaded: std.Io.Threaded = .init(gpa, .{});
17 defer threaded.deinit();
18 const io = threaded.io();
13 // Accessing command line arguments:
14 const args = try init.minimal.args.toSlice(arena);
15 for (args) |arg| {
16 std.log.info("arg: {s}", .{arg});
17 }
18
19 // In order to do I/O operations need an `Io` instance.
20 const io = init.io;
1921
2022 // Stdout is for the actual output of your application, for example if you
2123 // are implementing gzip, then only the compressed bytes should be sent to
lib/std/Build/Step/Run.zig+14-7
......@@ -603,18 +603,25 @@ pub fn removeEnvironmentVariable(run: *Run, key: []const u8) void {
603603
604604/// Adds a check for exact stderr match. Does not add any other checks.
605605pub fn expectStdErrEqual(run: *Run, bytes: []const u8) void {
606 const new_check: StdIo.Check = .{ .expect_stderr_exact = run.step.owner.dupe(bytes) };
607 run.addCheck(new_check);
606 run.addCheck(.{ .expect_stderr_exact = run.step.owner.dupe(bytes) });
607}
608
609pub fn expectStdErrMatch(run: *Run, bytes: []const u8) void {
610 run.addCheck(.{ .expect_stderr_match = run.step.owner.dupe(bytes) });
608611}
609612
610613/// Adds a check for exact stdout match as well as a check for exit code 0, if
611614/// there is not already an expected termination check.
612615pub fn expectStdOutEqual(run: *Run, bytes: []const u8) void {
613 const new_check: StdIo.Check = .{ .expect_stdout_exact = run.step.owner.dupe(bytes) };
614 run.addCheck(new_check);
615 if (!run.hasTermCheck()) {
616 run.expectExitCode(0);
617 }
616 run.addCheck(.{ .expect_stdout_exact = run.step.owner.dupe(bytes) });
617 if (!run.hasTermCheck()) run.expectExitCode(0);
618}
619
620/// Adds a check for stdout match as well as a check for exit code 0, if there
621/// is not already an expected termination check.
622pub fn expectStdOutMatch(run: *Run, bytes: []const u8) void {
623 run.addCheck(.{ .expect_stdout_match = run.step.owner.dupe(bytes) });
624 if (!run.hasTermCheck()) run.expectExitCode(0);
618625}
619626
620627pub fn expectExitCode(run: *Run, code: u8) void {
test/tests.zig+1-1
......@@ -2062,7 +2062,7 @@ pub fn addCliTests(b: *std.Build) *Step {
20622062 run_run.setCwd(.{ .cwd_relative = tmp_path });
20632063 run_run.setName("zig build run");
20642064 run_run.expectStdOutEqual("Run `zig build test` to run the tests.\n");
2065 run_run.expectStdErrEqual("All your codebase are belong to us.\n");
2065 run_run.expectStdErrMatch("All your codebase are belong to us.\n");
20662066 run_run.step.dependOn(&init_exe.step);
20672067
20682068 const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path });