aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2020-05-05 17:23:49 +0200
committerJakub Konka <kubkon@jakubkonka.com>2020-05-18 16:09:49 +0200
commitd43c08a3e517f29b3dcaf5aa7860ed185815a305 (patch)
tree9e7728c7f34c872f5ce032b131db6fcaa6077307 /lib/std/testing.zig
parentfeade9ef0010b1b47d7216e786ed964d09612c2b (diff)
downloadzig-d43c08a3e517f29b3dcaf5aa7860ed185815a305.tar.gz
zig-d43c08a3e517f29b3dcaf5aa7860ed185815a305.zip
Add/fix missing WASI functionality to pass libstd tests
This rather large commit adds/fixes missing WASI functionality in `libstd` needed to pass the `libstd` tests. As such, now by default tests targeting `wasm32-wasi` target are enabled in `test/tests.zig` module. However, they can be disabled by passing the `-Dskip-wasi=true` flag when invoking the `zig build test` command. When the flag is set to `false`, i.e., when WASI tests are included, `wasmtime` with `--dir=.` is used as the default testing command. Since the majority of `libstd` tests were relying on `fs.cwd()` call to get current working directory handle wrapped in `Dir` struct, in order to make the tests WASI-friendly, `fs.cwd()` call was replaced with `testing.getTestDir()` function which resolved to either `fs.cwd()` for non-WASI targets, or tries to fetch the preopen list from the WASI runtime and extract a preopen for '.' path. The summary of changes introduced by this commit: * implement `Dir.makeDir` and `Dir.openDir` targeting WASI * implement `Dir.deleteFile` and `Dir.deleteDir` targeting WASI * fix `os.close` and map errors in `unlinkat` * move WASI-specific `mkdirat` and `unlinkat` from `std.fs.wasi` to `std.os` module * implement `lseek_{SET, CUR, END}` targeting WASI * implement `futimens` targeting WASI * implement `ftruncate` targeting WASI * implement `readv`, `writev`, `pread{v}`, `pwrite{v}` targeting WASI * make sure ANSI escape codes are _not_ used in stderr or stdout in WASI, as WASI always sanitizes stderr, and sanitizes stdout if fd is a TTY * fix specifying WASI rights when opening/creating files/dirs * tweak `AtomicFile` to be WASI-compatible * implement `os.renameatWasi` for WASI-compliant `os.renameat` function * implement sleep() targeting WASI * fix `process.getEnvMap` targeting WASI
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 0f6cefb787..1592923ba8 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -14,6 +14,21 @@ 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 {