aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 8fa5fb68f8..97274533df 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -363,6 +363,22 @@ pub const TmpDir = struct {
}
};
+pub const TmpIterableDir = struct {
+ iterable_dir: std.fs.IterableDir,
+ parent_dir: std.fs.Dir,
+ sub_path: [sub_path_len]u8,
+
+ const random_bytes_count = 12;
+ const sub_path_len = std.fs.base64_encoder.calcSize(random_bytes_count);
+
+ pub fn cleanup(self: *TmpIterableDir) void {
+ self.iterable_dir.close();
+ self.parent_dir.deleteTree(&self.sub_path) catch {};
+ self.parent_dir.close();
+ self.* = undefined;
+ }
+};
+
fn getCwdOrWasiPreopen() std.fs.Dir {
if (builtin.os.tag == .wasi and !builtin.link_libc) {
var preopens = std.fs.wasi.PreopenList.init(allocator);
@@ -400,6 +416,28 @@ pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
};
}
+pub fn tmpIterableDir(opts: std.fs.Dir.OpenDirOptions) TmpIterableDir {
+ var random_bytes: [TmpIterableDir.random_bytes_count]u8 = undefined;
+ std.crypto.random.bytes(&random_bytes);
+ var sub_path: [TmpIterableDir.sub_path_len]u8 = undefined;
+ _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes);
+
+ var cwd = getCwdOrWasiPreopen();
+ var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch
+ @panic("unable to make tmp dir for testing: unable to make and open zig-cache dir");
+ defer cache_dir.close();
+ var parent_dir = cache_dir.makeOpenPath("tmp", .{}) catch
+ @panic("unable to make tmp dir for testing: unable to make and open zig-cache/tmp dir");
+ var dir = parent_dir.makeOpenPathIterable(&sub_path, opts) catch
+ @panic("unable to make tmp dir for testing: unable to make and open the tmp dir");
+
+ return .{
+ .iterable_dir = dir,
+ .parent_dir = parent_dir,
+ .sub_path = sub_path,
+ };
+}
+
test "expectEqual nested array" {
const a = [2][2]f32{
[_]f32{ 1.0, 0.0 },