authorgravatar for rabingaire20@gmail.comRabin Gaire <rabingaire20@gmail.com> 2022-04-20 18:37:10+05:45
committergravatar for rabingaire20@gmail.comRabin Gaire <rabingaire20@gmail.com> 2022-04-20 18:37:10+05:45
logd976456ef665bf0aba3a83a8e7fccb4a92b2d3b2
treec6bd3f295f3d69f80ea98bf238d03b27db6224c9
parent9e4cd1f4e6c3195665daa80c6a438cfd4daa92b1

add test case for child_process spawn logic

tests creating a child process with stdin/stdout behavior set to StdIo.Pipe.

1 files changed, 46 insertions(+), 0 deletions(-)

lib/std/child_process.zig+46
...@@ -1379,3 +1379,49 @@ test "build and call child_process" {...@@ -1379,3 +1379,49 @@ test "build and call child_process" {
1379 const ret_val = try child_proc.spawnAndWait();1379 const ret_val = try child_proc.spawnAndWait();
1380 try testing.expectEqual(ret_val, .{ .Exited = 0 });1380 try testing.expectEqual(ret_val, .{ .Exited = 0 });
1381}1381}
1382
1383test "creating a child process with stdin/stdout behavior set to StdIo.Pipe" {
1384 if (builtin.os.tag == .wasi) return error.SkipZigTest;
1385 const testing = std.testing;
1386 const allocator = testing.allocator;
1387
1388 var child_process = try std.ChildProcess.init(
1389 &[_][]const u8{ testing.zig_exe_path, "fmt", "--stdin" },
1390 allocator,
1391 );
1392 defer child_process.deinit();
1393 child_process.stdin_behavior = .Pipe;
1394 child_process.stdout_behavior = .Pipe;
1395
1396 try child_process.spawn();
1397
1398 const input_program =
1399 \\ const std = @import("std");
1400 \\ pub fn main() void {
1401 \\ std.debug.print("Hello World", .{});
1402 \\ }
1403 ;
1404
1405 try child_process.stdin.?.writer().writeAll(input_program);
1406 child_process.stdin.?.close();
1407 child_process.stdin = null;
1408
1409 const out_bytes = try child_process.stdout.?.reader().readAllAlloc(allocator, std.math.maxInt(usize));
1410 defer allocator.free(out_bytes);
1411
1412 switch (try child_process.wait()) {
1413 .Exited => |code| if (code == 0) {
1414 const expected_program =
1415 \\const std = @import("std");
1416 \\pub fn main() void {
1417 \\ std.debug.print("Hello World", .{});
1418 \\}
1419 \\
1420 ;
1421 try testing.expectEqualStrings(expected_program, out_bytes);
1422 },
1423 else => {
1424 try testing.expect(false);
1425 }
1426 }
1427}