aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-07-16 12:18:21 +0300
committerGitHub <noreply@github.com>2022-07-16 12:18:21 +0300
commitda94227f783ec3c92859c4713b80a668f1183f96 (patch)
tree74e36b830f8fdff1c836df1c74024c5477bd0f7c /lib/std/testing.zig
parent8f943b3d33432a26b7e242c1181e4220ed400501 (diff)
parent262f4c7b3a850594a75ec154db2ba8d5f9f517ab (diff)
downloadzig-da94227f783ec3c92859c4713b80a668f1183f96.tar.gz
zig-da94227f783ec3c92859c4713b80a668f1183f96.zip
Merge pull request #12060 from Vexu/IterableDir
std.fs: split `Dir` into `IterableDir`
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 },