authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-18 15:28:25+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-18 15:37:44+01:00
log663e112773f91ccbff9f9825147b1a2c0bb5e00c
tree9e7082363288f53c015bcf73140819301648f440
parent64feae3ac35af700e5f3d2e36f9482d1b58496ef

std: add chdir smoke test


2 files changed, 37 insertions(+), 6 deletions(-)

lib/std/os.zig+13-6
...@@ -2312,11 +2312,7 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {...@@ -2312,11 +2312,7 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
2312 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;2312 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;
2313 const len = try std.unicode.utf8ToUtf16Le(utf16_dir_path[0..], dir_path);2313 const len = try std.unicode.utf8ToUtf16Le(utf16_dir_path[0..], dir_path);
2314 if (len > utf16_dir_path.len) return error.NameTooLong;2314 if (len > utf16_dir_path.len) return error.NameTooLong;
2315 2315 return chdirW(utf16_dir_path[0..len]);
2316 windows.SetCurrentDirectory(utf16_dir_path[0..len]) catch |err| switch (err) {
2317 error.NoDevice => return error.FileSystem,
2318 else => |e| return e,
2319 };
2320 } else {2316 } else {
2321 const dir_path_c = try toPosixPath(dir_path);2317 const dir_path_c = try toPosixPath(dir_path);
2322 return chdirZ(&dir_path_c);2318 return chdirZ(&dir_path_c);
...@@ -2328,7 +2324,10 @@ pub const chdirC = @compileError("deprecated: renamed to chdirZ");...@@ -2328,7 +2324,10 @@ pub const chdirC = @compileError("deprecated: renamed to chdirZ");
2328/// Same as `chdir` except the parameter is null-terminated.2324/// Same as `chdir` except the parameter is null-terminated.
2329pub fn chdirZ(dir_path: [*:0]const u8) ChangeCurDirError!void {2325pub fn chdirZ(dir_path: [*:0]const u8) ChangeCurDirError!void {
2330 if (builtin.os.tag == .windows) {2326 if (builtin.os.tag == .windows) {
2331 return chdir(mem.spanZ(dir_path));2327 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;
2328 const len = try std.unicode.utf8ToUtf16Le(utf16_dir_path[0..], dir_path);
2329 if (len > utf16_dir_path.len) return error.NameTooLong;
2330 return chdirW(utf16_dir_path[0..len]);
2332 }2331 }
2333 switch (errno(system.chdir(dir_path))) {2332 switch (errno(system.chdir(dir_path))) {
2334 0 => return,2333 0 => return,
...@@ -2344,6 +2343,14 @@ pub fn chdirZ(dir_path: [*:0]const u8) ChangeCurDirError!void {...@@ -2344,6 +2343,14 @@ pub fn chdirZ(dir_path: [*:0]const u8) ChangeCurDirError!void {
2344 }2343 }
2345}2344}
23462345
2346/// Windows-only. Same as `chdir` except the paramter is WTF16 encoded.
2347pub fn chdirW(dir_path: []const u16) ChangeCurDirError!void {
2348 windows.SetCurrentDirectory(dir_path) catch |err| switch (err) {
2349 error.NoDevice => return error.FileSystem,
2350 else => |e| return e,
2351 };
2352}
2353
2347pub const FchdirError = error{2354pub const FchdirError = error{
2348 AccessDenied,2355 AccessDenied,
2349 NotDir,2356 NotDir,
lib/std/os/test.zig+24
...@@ -25,6 +25,30 @@ const tmpDir = std.testing.tmpDir;...@@ -25,6 +25,30 @@ const tmpDir = std.testing.tmpDir;
25const Dir = std.fs.Dir;25const Dir = std.fs.Dir;
26const ArenaAllocator = std.heap.ArenaAllocator;26const ArenaAllocator = std.heap.ArenaAllocator;
2727
28test "chdir smoke test" {
29 if (builtin.os.tag == .wasi) return error.SkipZigTest;
30
31 // Get current working directory path
32 var old_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
33 const old_cwd = try os.getcwd(old_cwd_buf[0..]);
34
35 {
36 // Firstly, changing to itself should have no effect
37 try os.chdir(old_cwd);
38 var new_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
39 const new_cwd = try os.getcwd(new_cwd_buf[0..]);
40 expect(mem.eql(u8, old_cwd, new_cwd));
41 }
42 {
43 // Next, change current working directory to one level above
44 const parent = fs.path.dirname(old_cwd) orelse unreachable; // old_cwd should be absolute
45 try os.chdir(parent);
46 var new_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
47 const new_cwd = try os.getcwd(new_cwd_buf[0..]);
48 expect(mem.eql(u8, parent, new_cwd));
49 }
50}
51
28test "open smoke test" {52test "open smoke test" {
29 if (builtin.os.tag == .wasi) return error.SkipZigTest;53 if (builtin.os.tag == .wasi) return error.SkipZigTest;
3054