| author | |
| committer | |
| log | 3052597a734f87727fa7f1a0e92247f100df3e96 |
| tree | 5ad90f9006fe13801ca5f5c832d7dec957df88ad |
| parent | 9181c98225dccbe698efafffd9a7bc6f50b8b46d |
This reverts commit 7f13f5cd5f5a518638b15d7225eae2d88ec1efb5.
I'd like to review this one before it goes in. This is an awfully
specific API that I don't think belongs in std.testing. Also I don't
want any code snippets in doc strings. We have doctests for that.2 files changed, 12 insertions(+), 42 deletions(-)
lib/std/child_process.zig+12-5| ... | @@ -1357,16 +1357,23 @@ test "build and call child_process" { | ... | @@ -1357,16 +1357,23 @@ test "build and call child_process" { |
| 1357 | var it = try std.process.argsWithAllocator(allocator); | 1357 | var it = try std.process.argsWithAllocator(allocator); |
| 1358 | defer it.deinit(); // no-op unless WASI or Windows | 1358 | defer it.deinit(); // no-op unless WASI or Windows |
| 1359 | const testargs = try testing.getTestArgs(&it); | 1359 | const testargs = try testing.getTestArgs(&it); |
| 1360 | |||
| 1360 | var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE | 1361 | var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE |
| 1361 | defer tmp.cleanup(); | 1362 | defer tmp.cleanup(); |
| 1363 | const tmpdirpath = try tmp.getFullPath(allocator); | ||
| 1364 | defer allocator.free(tmpdirpath); | ||
| 1362 | const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin' | 1365 | const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin' |
| 1363 | const zigfile_path = try tmp.writeZigFile(allocator, childstr, child_name); | 1366 | const suffix_zig = ".zig"; |
| 1364 | defer allocator.free(zigfile_path); | 1367 | const child_path = try fs.path.join(allocator, &[_][]const u8{ tmpdirpath, child_name }); |
| 1368 | defer allocator.free(child_path); | ||
| 1369 | const child_zig = try mem.concat(allocator, u8, &[_][]const u8{ child_path, suffix_zig }); | ||
| 1370 | defer allocator.free(child_zig); | ||
| 1371 | |||
| 1372 | try tmp.dir.writeFile("child.zig", childstr); | ||
| 1373 | try testing.buildExe(testargs.zigexec, child_zig, child_path); | ||
| 1365 | 1374 | ||
| 1366 | const binary = zigfile_path[0 .. zigfile_path.len - 4]; // '.zig' is 4 characters | ||
| 1367 | try testing.buildExe(testargs.zigexec, zigfile_path, binary); | ||
| 1368 | // spawn compiled file as child_process with argument 'hello world' + expect success | 1375 | // spawn compiled file as child_process with argument 'hello world' + expect success |
| 1369 | const args = [_][]const u8{ binary, "hello world" }; | 1376 | const args = [_][]const u8{ child_path, "hello world" }; |
| 1370 | var child_proc = try ChildProcess.init(&args, allocator); | 1377 | var child_proc = try ChildProcess.init(&args, allocator); |
| 1371 | defer child_proc.deinit(); | 1378 | defer child_proc.deinit(); |
| 1372 | const ret_val = try child_proc.spawnAndWait(); | 1379 | const ret_val = try child_proc.spawnAndWait(); |
lib/std/testing.zig-37| ... | @@ -374,43 +374,6 @@ pub const TmpDir = struct { | ... | @@ -374,43 +374,6 @@ pub const TmpDir = struct { |
| 374 | self.parent_dir.close(); | 374 | self.parent_dir.close(); |
| 375 | self.* = undefined; | 375 | self.* = undefined; |
| 376 | } | 376 | } |
| 377 | |||
| 378 | /// Writes program string as zig file into tmp directory | ||
| 379 | /// Caller owns memory | ||
| 380 | /// | ||
| 381 | /// ``` | ||
| 382 | /// const progstr = "pub fn main() void {}\n"; | ||
| 383 | /// var it = try std.process.argsWithAllocator(std.testing.allocator); | ||
| 384 | /// defer it.deinit(); // no-op unless WASI or Windows | ||
| 385 | /// const testargs = try std.testing.getTestArgs(&it); | ||
| 386 | /// var tmp = std.testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE | ||
| 387 | /// defer tmp.cleanup(); | ||
| 388 | /// const zigfile_path = try tmp.writeZigFile(std.testing.allocator, progstr, "bruh"); | ||
| 389 | /// defer std.testing.allocator.free(zigfile_path); | ||
| 390 | /// const binary = zigfile_path[0 .. zigfile_path.len - 4]; // '.zig' is 4 characters | ||
| 391 | /// try std.testing.buildExe(testargs.zigexec, zigfile_path, binary); | ||
| 392 | /// ``` | ||
| 393 | pub fn writeZigFile( | ||
| 394 | self: *TmpDir, | ||
| 395 | alloc: std.mem.Allocator, | ||
| 396 | progstr: []const u8, | ||
| 397 | filename: []const u8, | ||
| 398 | ) ![]const u8 { | ||
| 399 | const tmpdir_path = try self.getFullPath(alloc); | ||
| 400 | defer alloc.free(tmpdir_path); | ||
| 401 | const suffix_zig = ".zig"; | ||
| 402 | const zigfile_path = try std.mem.concat(alloc, u8, &[_][]const u8{ | ||
| 403 | tmpdir_path, | ||
| 404 | std.fs.path.sep_str, | ||
| 405 | filename, | ||
| 406 | suffix_zig, | ||
| 407 | }); | ||
| 408 | errdefer alloc.free(zigfile_path); | ||
| 409 | const zigfile = try std.mem.concat(alloc, u8, &[_][]const u8{ filename, suffix_zig }); | ||
| 410 | defer alloc.free(zigfile); | ||
| 411 | try self.dir.writeFile(zigfile, progstr); | ||
| 412 | return zigfile_path; | ||
| 413 | } | ||
| 414 | }; | 377 | }; |
| 415 | 378 | ||
| 416 | fn getCwdOrWasiPreopen() std.fs.Dir { | 379 | fn getCwdOrWasiPreopen() std.fs.Dir { |