From 3052597a734f87727fa7f1a0e92247f100df3e96 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 28 Apr 2022 11:20:53 -0700 Subject: [PATCH] Revert "std.testing: add writeZigFile for TmpDir" 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. --- lib/std/child_process.zig | 17 ++++++++++++----- lib/std/testing.zig | 37 ------------------------------------- 2 files changed, 12 insertions(+), 42 deletions(-) diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index f3c35dde2f..f2b978ba9f 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -1357,16 +1357,23 @@ test "build and call child_process" { var it = try std.process.argsWithAllocator(allocator); defer it.deinit(); // no-op unless WASI or Windows const testargs = try testing.getTestArgs(&it); + var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE defer tmp.cleanup(); + const tmpdirpath = try tmp.getFullPath(allocator); + defer allocator.free(tmpdirpath); const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin' - const zigfile_path = try tmp.writeZigFile(allocator, childstr, child_name); - defer allocator.free(zigfile_path); + const suffix_zig = ".zig"; + const child_path = try fs.path.join(allocator, &[_][]const u8{ tmpdirpath, child_name }); + defer allocator.free(child_path); + const child_zig = try mem.concat(allocator, u8, &[_][]const u8{ child_path, suffix_zig }); + defer allocator.free(child_zig); + + try tmp.dir.writeFile("child.zig", childstr); + try testing.buildExe(testargs.zigexec, child_zig, child_path); - const binary = zigfile_path[0 .. zigfile_path.len - 4]; // '.zig' is 4 characters - try testing.buildExe(testargs.zigexec, zigfile_path, binary); // spawn compiled file as child_process with argument 'hello world' + expect success - const args = [_][]const u8{ binary, "hello world" }; + const args = [_][]const u8{ child_path, "hello world" }; var child_proc = try ChildProcess.init(&args, allocator); defer child_proc.deinit(); const ret_val = try child_proc.spawnAndWait(); diff --git a/lib/std/testing.zig b/lib/std/testing.zig index b90fbc2e4f..016e84acd8 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -374,43 +374,6 @@ pub const TmpDir = struct { self.parent_dir.close(); self.* = undefined; } - - /// Writes program string as zig file into tmp directory - /// Caller owns memory - /// - /// ``` - /// const progstr = "pub fn main() void {}\n"; - /// var it = try std.process.argsWithAllocator(std.testing.allocator); - /// defer it.deinit(); // no-op unless WASI or Windows - /// const testargs = try std.testing.getTestArgs(&it); - /// var tmp = std.testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE - /// defer tmp.cleanup(); - /// const zigfile_path = try tmp.writeZigFile(std.testing.allocator, progstr, "bruh"); - /// defer std.testing.allocator.free(zigfile_path); - /// const binary = zigfile_path[0 .. zigfile_path.len - 4]; // '.zig' is 4 characters - /// try std.testing.buildExe(testargs.zigexec, zigfile_path, binary); - /// ``` - pub fn writeZigFile( - self: *TmpDir, - alloc: std.mem.Allocator, - progstr: []const u8, - filename: []const u8, - ) ![]const u8 { - const tmpdir_path = try self.getFullPath(alloc); - defer alloc.free(tmpdir_path); - const suffix_zig = ".zig"; - const zigfile_path = try std.mem.concat(alloc, u8, &[_][]const u8{ - tmpdir_path, - std.fs.path.sep_str, - filename, - suffix_zig, - }); - errdefer alloc.free(zigfile_path); - const zigfile = try std.mem.concat(alloc, u8, &[_][]const u8{ filename, suffix_zig }); - defer alloc.free(zigfile); - try self.dir.writeFile(zigfile, progstr); - return zigfile_path; - } }; fn getCwdOrWasiPreopen() std.fs.Dir {