@@ -22,7 +22,8 @@ const Dir = std.fs.Dir;
22
22
const ArenaAllocator = std .heap .ArenaAllocator ;
23
23
24
24
test "chdir smoke test" {
25
- if (native_os == .wasi ) return error .SkipZigTest ; // WASI doesn't allow navigating outside of a preopen
25
+ if (native_os == .wasi and builtin .link_libc ) return error .SkipZigTest ;
26
+ if (native_os == .wasi and ! builtin .link_libc ) try os .initPreopensWasi (std .heap .page_allocator , "/preopens/cwd" );
26
27
27
28
// Get current working directory path
28
29
var old_cwd_buf : [fs .MAX_PATH_BYTES ]u8 = undefined ;
@@ -35,16 +36,42 @@ test "chdir smoke test" {
35
36
const new_cwd = try os .getcwd (new_cwd_buf [0.. ]);
36
37
try expect (mem .eql (u8 , old_cwd , new_cwd ));
37
38
}
38
- {
39
- // Next, change current working directory to one level above
39
+
40
+ // Next, change current working directory to one level above
41
+ if (native_os != .wasi ) { // WASI does not support navigating outside of Preopens
40
42
const parent = fs .path .dirname (old_cwd ) orelse unreachable ; // old_cwd should be absolute
41
43
try os .chdir (parent );
44
+
42
45
// Restore cwd because process may have other tests that do not tolerate chdir.
43
46
defer os .chdir (old_cwd ) catch unreachable ;
47
+
44
48
var new_cwd_buf : [fs .MAX_PATH_BYTES ]u8 = undefined ;
45
49
const new_cwd = try os .getcwd (new_cwd_buf [0.. ]);
46
50
try expect (mem .eql (u8 , parent , new_cwd ));
47
51
}
52
+
53
+ // Next, change current working directory to a temp directory one level below
54
+ {
55
+ // Create a tmp directory
56
+ var tmp_dir_buf : [fs .MAX_PATH_BYTES ]u8 = undefined ;
57
+ var tmp_dir_path = path : {
58
+ var allocator = std .heap .FixedBufferAllocator .init (& tmp_dir_buf );
59
+ break :path try fs .path .resolve (allocator .allocator (), &[_ ][]const u8 { old_cwd , "zig-test-tmp" });
60
+ };
61
+ var tmp_dir = try fs .cwd ().makeOpenPath ("zig-test-tmp" , .{});
62
+
63
+ // Change current working directory to tmp directory
64
+ try os .chdir ("zig-test-tmp" );
65
+
66
+ var new_cwd_buf : [fs .MAX_PATH_BYTES ]u8 = undefined ;
67
+ const new_cwd = try os .getcwd (new_cwd_buf [0.. ]);
68
+ try expect (mem .eql (u8 , tmp_dir_path , new_cwd ));
69
+
70
+ // Restore cwd because process may have other tests that do not tolerate chdir.
71
+ tmp_dir .close ();
72
+ os .chdir (old_cwd ) catch unreachable ;
73
+ try fs .cwd ().deleteDir ("zig-test-tmp" );
74
+ }
48
75
}
49
76
50
77
test "open smoke test" {
0 commit comments