| ... | @@ -1323,91 +1323,3 @@ test "createNullDelimitedEnvMap" { | ... | @@ -1323,91 +1323,3 @@ test "createNullDelimitedEnvMap" { |
| 1323 | } | 1323 | } |
| 1324 | } | 1324 | } |
| 1325 | } | 1325 | } |
| 1326 | | | |
| 1327 | const childstr = | | |
| 1328 | \\ const std = @import("std"); | | |
| 1329 | \\ const builtin = @import("builtin"); | | |
| 1330 | \\ pub fn main() !void { | | |
| 1331 | \\ var it = try std.process.argsWithAllocator(std.testing.allocator); | | |
| 1332 | \\ defer it.deinit(); // no-op unless WASI or Windows | | |
| 1333 | \\ _ = it.next() orelse unreachable; // skip binary name | | |
| 1334 | \\ const input = it.next() orelse unreachable; | | |
| 1335 | \\ var expect_helloworld = "hello world".*; | | |
| 1336 | \\ try std.testing.expect(std.mem.eql(u8, &expect_helloworld, input)); | | |
| 1337 | \\ try std.testing.expect(it.next() == null); | | |
| 1338 | \\ try std.testing.expect(!it.skip()); | | |
| 1339 | \\ } | | |
| 1340 | ; | | |
| 1341 | | | |
| 1342 | test "build and call child_process" { | | |
| 1343 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | | |
| 1344 | const testing = std.testing; | | |
| 1345 | const allocator = testing.allocator; | | |
| 1346 | | | |
| 1347 | var it = try std.process.argsWithAllocator(allocator); | | |
| 1348 | defer it.deinit(); // no-op unless WASI or Windows | | |
| 1349 | const testargs = try testing.getTestArgs(&it); | | |
| 1350 | | | |
| 1351 | var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE | | |
| 1352 | defer tmp.cleanup(); | | |
| 1353 | const tmpdirpath = try tmp.getFullPath(allocator); | | |
| 1354 | defer allocator.free(tmpdirpath); | | |
| 1355 | const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin' | | |
| 1356 | const suffix_zig = ".zig"; | | |
| 1357 | const child_path = try fs.path.join(allocator, &[_][]const u8{ tmpdirpath, child_name }); | | |
| 1358 | defer allocator.free(child_path); | | |
| 1359 | const child_zig = try mem.concat(allocator, u8, &[_][]const u8{ child_path, suffix_zig }); | | |
| 1360 | defer allocator.free(child_zig); | | |
| 1361 | | | |
| 1362 | try tmp.dir.writeFile("child.zig", childstr); | | |
| 1363 | try testing.buildExe(testargs.zigexec, child_zig, child_path); | | |
| 1364 | | | |
| 1365 | // spawn compiled file as child_process with argument 'hello world' + expect success | | |
| 1366 | const args = [_][]const u8{ child_path, "hello world" }; | | |
| 1367 | var child_proc = ChildProcess.init(&args, allocator); | | |
| 1368 | const ret_val = try child_proc.spawnAndWait(); | | |
| 1369 | try testing.expectEqual(ret_val, .{ .Exited = 0 }); | | |
| 1370 | } | | |
| 1371 | | | |
| 1372 | test "creating a child process with stdin and stdout behavior set to StdIo.Pipe" { | | |
| 1373 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | | |
| 1374 | const testing = std.testing; | | |
| 1375 | const allocator = testing.allocator; | | |
| 1376 | | | |
| 1377 | var child_process = std.ChildProcess.init( | | |
| 1378 | &[_][]const u8{ testing.zig_exe_path, "fmt", "--stdin" }, | | |
| 1379 | allocator, | | |
| 1380 | ); | | |
| 1381 | child_process.stdin_behavior = .Pipe; | | |
| 1382 | child_process.stdout_behavior = .Pipe; | | |
| 1383 | | | |
| 1384 | try child_process.spawn(); | | |
| 1385 | | | |
| 1386 | const input_program = | | |
| 1387 | \\ const std = @import("std"); | | |
| 1388 | \\ pub fn main() void { | | |
| 1389 | \\ std.debug.print("Hello World", .{}); | | |
| 1390 | \\ } | | |
| 1391 | ; | | |
| 1392 | | | |
| 1393 | try child_process.stdin.?.writer().writeAll(input_program); | | |
| 1394 | child_process.stdin.?.close(); | | |
| 1395 | child_process.stdin = null; | | |
| 1396 | | | |
| 1397 | const out_bytes = try child_process.stdout.?.reader().readAllAlloc(allocator, std.math.maxInt(usize)); | | |
| 1398 | defer allocator.free(out_bytes); | | |
| 1399 | | | |
| 1400 | switch (try child_process.wait()) { | | |
| 1401 | .Exited => |code| if (code == 0) { | | |
| 1402 | const expected_program = | | |
| 1403 | \\const std = @import("std"); | | |
| 1404 | \\pub fn main() void { | | |
| 1405 | \\ std.debug.print("Hello World", .{}); | | |
| 1406 | \\} | | |
| 1407 | \\ | | |
| 1408 | ; | | |
| 1409 | try testing.expectEqualStrings(expected_program, out_bytes); | | |
| 1410 | }, | | |
| 1411 | else => unreachable, | | |
| 1412 | } | | |
| 1413 | } | | |