authorgravatar for matu3ba@users.noreply.github.commatu3ba <matu3ba@users.noreply.github.com> 2022-04-28 17:37:30+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-04-28 18:37:30+03:00
log7f13f5cd5f5a518638b15d7225eae2d88ec1efb5
tree2b63e2c8ed35d36671c5a8380e2ea290b4b5395b
parent8e3add8736be683b450c2754bedb064811baed0e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.testing: add writeZigFile for TmpDir

* remove need for manual string concatenation for building binaries in test blocks * include small program snippet to show how to get binary path with subslicing

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

lib/std/child_process.zig+5-12
...@@ -1357,23 +1357,16 @@ test "build and call child_process" {...@@ -1357,23 +1357,16 @@ 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 Windows1358 defer it.deinit(); // no-op unless WASI or Windows
1359 const testargs = try testing.getTestArgs(&it);1359 const testargs = try testing.getTestArgs(&it);
1360
1361 var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE1360 var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE
1362 defer tmp.cleanup();1361 defer tmp.cleanup();
1363 const tmpdirpath = try tmp.getFullPath(allocator);
1364 defer allocator.free(tmpdirpath);
1365 const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin'1362 const child_name = "child"; // no need for suffixes (.exe, .wasm) due to '-femit-bin'
1366 const suffix_zig = ".zig";1363 const zigfile_path = try tmp.writeZigFile(allocator, childstr, child_name);
1367 const child_path = try fs.path.join(allocator, &[_][]const u8{ tmpdirpath, child_name });1364 defer allocator.free(zigfile_path);
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);
13741365
1366 const binary = zigfile_path[0 .. zigfile_path.len - 4]; // '.zig' is 4 characters
1367 try testing.buildExe(testargs.zigexec, zigfile_path, binary);
1375 // spawn compiled file as child_process with argument 'hello world' + expect success1368 // spawn compiled file as child_process with argument 'hello world' + expect success
1376 const args = [_][]const u8{ child_path, "hello world" };1369 const args = [_][]const u8{ binary, "hello world" };
1377 var child_proc = try ChildProcess.init(&args, allocator);1370 var child_proc = try ChildProcess.init(&args, allocator);
1378 defer child_proc.deinit();1371 defer child_proc.deinit();
1379 const ret_val = try child_proc.spawnAndWait();1372 const ret_val = try child_proc.spawnAndWait();
lib/std/testing.zig+37
...@@ -374,6 +374,43 @@ pub const TmpDir = struct {...@@ -374,6 +374,43 @@ 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 }
377};414};
378415
379fn getCwdOrWasiPreopen() std.fs.Dir {416fn getCwdOrWasiPreopen() std.fs.Dir {