From d976456ef665bf0aba3a83a8e7fccb4a92b2d3b2 Mon Sep 17 00:00:00 2001 From: Rabin Gaire Date: Wed, 20 Apr 2022 18:37:10 +0545 Subject: [PATCH] add test case for child_process spawn logic tests creating a child process with stdin/stdout behavior set to StdIo.Pipe. --- lib/std/child_process.zig | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index aef96cbde3..41d2cf2850 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -1379,3 +1379,49 @@ test "build and call child_process" { const ret_val = try child_proc.spawnAndWait(); try testing.expectEqual(ret_val, .{ .Exited = 0 }); } + +test "creating a child process with stdin/stdout behavior set to StdIo.Pipe" { + if (builtin.os.tag == .wasi) return error.SkipZigTest; + const testing = std.testing; + const allocator = testing.allocator; + + var child_process = try std.ChildProcess.init( + &[_][]const u8{ testing.zig_exe_path, "fmt", "--stdin" }, + allocator, + ); + defer child_process.deinit(); + child_process.stdin_behavior = .Pipe; + child_process.stdout_behavior = .Pipe; + + try child_process.spawn(); + + const input_program = + \\ const std = @import("std"); + \\ pub fn main() void { + \\ std.debug.print("Hello World", .{}); + \\ } + ; + + try child_process.stdin.?.writer().writeAll(input_program); + child_process.stdin.?.close(); + child_process.stdin = null; + + const out_bytes = try child_process.stdout.?.reader().readAllAlloc(allocator, std.math.maxInt(usize)); + defer allocator.free(out_bytes); + + switch (try child_process.wait()) { + .Exited => |code| if (code == 0) { + const expected_program = + \\const std = @import("std"); + \\pub fn main() void { + \\ std.debug.print("Hello World", .{}); + \\} + \\ + ; + try testing.expectEqualStrings(expected_program, out_bytes); + }, + else => { + try testing.expect(false); + } + } +}