authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-28 11:20:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-28 11:20:53-07:00
log3052597a734f87727fa7f1a0e92247f100df3e96
tree5ad90f9006fe13801ca5f5c832d7dec957df88ad
parent9181c98225dccbe698efafffd9a7bc6f50b8b46d

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.

2 files changed, 12 insertions(+), 42 deletions(-)

lib/std/child_process.zig+12-5
......@@ -1357,16 +1357,23 @@ test "build and call child_process" {
13571357 var it = try std.process.argsWithAllocator(allocator);
13581358 defer it.deinit(); // no-op unless WASI or Windows
13591359 const testargs = try testing.getTestArgs(&it);
1360
13601361 var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE
13611362 defer tmp.cleanup();
1363 const tmpdirpath = try tmp.getFullPath(allocator);
1364 defer allocator.free(tmpdirpath);
13621365 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);
1364 defer allocator.free(zigfile_path);
1366 const suffix_zig = ".zig";
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);
13651374
1366 const binary = zigfile_path[0 .. zigfile_path.len - 4]; // '.zig' is 4 characters
1367 try testing.buildExe(testargs.zigexec, zigfile_path, binary);
13681375 // 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" };
13701377 var child_proc = try ChildProcess.init(&args, allocator);
13711378 defer child_proc.deinit();
13721379 const ret_val = try child_proc.spawnAndWait();
lib/std/testing.zig-37
......@@ -374,43 +374,6 @@ pub const TmpDir = struct {
374374 self.parent_dir.close();
375375 self.* = undefined;
376376 }
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 }
414377};
415378
416379fn getCwdOrWasiPreopen() std.fs.Dir {