aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2020-05-18 17:10:02 +0200
committerJakub Konka <kubkon@jakubkonka.com>2020-05-18 17:10:49 +0200
commit2a59ecd7eca7318e9a66d856b04dd9fa45b2d69d (patch)
tree9c3e45de923fc01c2fe9d836c7b815ef35e042bc /lib/std/testing.zig
parentf26ab568aaf60543d9fa09b53461458e5cdaddb3 (diff)
downloadzig-2a59ecd7eca7318e9a66d856b04dd9fa45b2d69d.tar.gz
zig-2a59ecd7eca7318e9a66d856b04dd9fa45b2d69d.zip
Integrate getTestDir with tmpDir logic
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig33
1 files changed, 17 insertions, 16 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 1592923ba8..34bebad043 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -14,21 +14,6 @@ pub var failing_allocator_instance = FailingAllocator.init(&base_allocator_insta
pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
var allocator_mem: [2 * 1024 * 1024]u8 = undefined;
-/// This function is intended to be used only in tests. It should be used in any testcase
-/// where we intend to test WASI and should be used a replacement for `std.fs.cwd()` in WASI.
-pub fn getTestDir() std.fs.Dir {
- if (@import("builtin").os.tag == .wasi) {
- var preopens = std.fs.wasi.PreopenList.init(allocator);
- defer preopens.deinit();
- preopens.populate() catch unreachable;
-
- const preopen = preopens.find(".") orelse unreachable;
- return std.fs.Dir{ .fd = preopen.fd };
- } else {
- return std.fs.cwd();
- }
-}
-
/// This function is intended to be used only in tests. It prints diagnostics to stderr
/// and then aborts when actual_error_union is not expected_error.
pub fn expectError(expected_error: anyerror, actual_error_union: var) void {
@@ -224,6 +209,21 @@ pub const TmpDir = struct {
}
};
+fn getCwdOrWasiPreopen() std.fs.Dir {
+ if (@import("builtin").os.tag == .wasi) {
+ var preopens = std.fs.wasi.PreopenList.init(allocator);
+ defer preopens.deinit();
+ preopens.populate() catch
+ @panic("unable to make tmp dir for testing: unable to populate preopens");
+ const preopen = preopens.find(".") orelse
+ @panic("unable to make tmp dir for testing: didn't find '.' in the preopens");
+
+ return std.fs.Dir{ .fd = preopen.fd };
+ } else {
+ return std.fs.cwd();
+ }
+}
+
pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;
std.crypto.randomBytes(&random_bytes) catch
@@ -231,7 +231,8 @@ pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
var sub_path: [TmpDir.sub_path_len]u8 = undefined;
std.fs.base64_encoder.encode(&sub_path, &random_bytes);
- var cache_dir = std.fs.cwd().makeOpenPath("zig-cache", .{}) catch
+ 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