| ... | @@ -1348,46 +1348,30 @@ const childstr = | ... | @@ -1348,46 +1348,30 @@ const childstr = |
| 1348 | test "build and call child_process" { | 1348 | test "build and call child_process" { |
| 1349 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | 1349 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 1350 | const testing = std.testing; | 1350 | const testing = std.testing; |
| 1351 | var it = try std.process.argsWithAllocator(std.testing.allocator); | 1351 | const allocator = testing.allocator; |
| | 1352 | |
| | 1353 | var it = try std.process.argsWithAllocator(allocator); |
| 1352 | defer it.deinit(); // no-op unless WASI or Windows | 1354 | defer it.deinit(); // no-op unless WASI or Windows |
| | 1355 | const testargs = try testing.getTestArgs(&it); |
| 1353 | | 1356 | |
| 1354 | _ = it.next() orelse unreachable; | | |
| 1355 | const zigexec = it.next() orelse unreachable; | | |
| 1356 | try testing.expect(it.next() == null); | | |
| 1357 | try testing.expect(!it.skip()); | | |
| 1358 | const cwd_str = try process.getCwdAlloc(testing.allocator); | | |
| 1359 | defer testing.allocator.free(cwd_str); | | |
| 1360 | var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE | 1357 | var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE |
| 1361 | defer tmp.cleanup(); | 1358 | defer tmp.cleanup(); |
| 1362 | const cache = "zig-cache"; | 1359 | const tmpdirpath = try tmp.getFullPath(allocator); |
| 1363 | const tmpdir = "tmp"; | 1360 | defer allocator.free(tmpdirpath); |
| 1364 | const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin' | 1361 | const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin' |
| 1365 | const suffix_zig = ".zig"; | 1362 | const suffix_zig = ".zig"; |
| 1366 | const child_path = try fs.path.join(testing.allocator, &[_][]const u8{ cwd_str, std.fs.path.sep_str, cache, tmpdir, &tmp.sub_path, child_name }); | 1363 | const child_path = try fs.path.join(allocator, &[_][]const u8{ tmpdirpath, child_name }); |
| 1367 | defer testing.allocator.free(child_path); | 1364 | defer allocator.free(child_path); |
| 1368 | | 1365 | const child_zig = try mem.concat(allocator, u8, &[_][]const u8{ child_path, suffix_zig }); |
| 1369 | const child_zig = try mem.concat(testing.allocator, u8, &[_][]const u8{ child_path, suffix_zig }); | 1366 | defer allocator.free(child_zig); |
| 1370 | defer testing.allocator.free(child_zig); | 1367 | |
| 1371 | const emit_flag = "-femit-bin="; | 1368 | try tmp.dir.writeFile("child.zig", childstr); |
| 1372 | const emit_bin = try mem.concat(testing.allocator, u8, &[_][]const u8{ emit_flag, child_path }); | 1369 | try testing.buildExe(testargs.zigexec, child_zig, child_path); |
| 1373 | defer testing.allocator.free(emit_bin); | 1370 | |
| 1374 | { | 1371 | // spawn compiled file as child_process with argument 'hello world' + expect success |
| 1375 | // 'zigexec build-exe path/to/child.zig -femit-bin=path/to/child' expect success | 1372 | const args = [_][]const u8{ child_path, "hello world" }; |
| 1376 | try tmp.dir.writeFile("child.zig", childstr); | 1373 | var child_proc = try ChildProcess.init(&args, allocator); |
| 1377 | const args = [_][]const u8{ zigexec, "build-exe", child_zig, emit_bin }; | 1374 | defer child_proc.deinit(); |
| 1378 | var procCompileChild = try ChildProcess.init(&args, testing.allocator); | 1375 | const ret_val = try child_proc.spawnAndWait(); |
| 1379 | defer procCompileChild.deinit(); | 1376 | try testing.expectEqual(ret_val, .{ .Exited = 0 }); |
| 1380 | try procCompileChild.spawn(); | | |
| 1381 | const ret_val = try procCompileChild.wait(); | | |
| 1382 | try testing.expectEqual(ret_val, .{ .Exited = 0 }); | | |
| 1383 | } | | |
| 1384 | { | | |
| 1385 | // spawn compiled file as child_process with argument 'hello world' + expect success | | |
| 1386 | const args = [_][]const u8{ child_path, "hello world" }; | | |
| 1387 | var child_proc = try ChildProcess.init(&args, testing.allocator); | | |
| 1388 | defer child_proc.deinit(); | | |
| 1389 | try child_proc.spawn(); | | |
| 1390 | const ret_val = try child_proc.wait(); | | |
| 1391 | try testing.expectEqual(ret_val, .{ .Exited = 0 }); | | |
| 1392 | } | | |
| 1393 | } | 1377 | } |