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 {
23122312 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;
23132313 const len = try std.unicode.utf8ToUtf16Le(utf16_dir_path[0..], dir_path);
23142314 if (len > utf16_dir_path.len) return error.NameTooLong;
2315
2316 windows.SetCurrentDirectory(utf16_dir_path[0..len]) catch |err| switch (err) {
2317 error.NoDevice => return error.FileSystem,
2318 else => |e| return e,
2319 };
2315 return chdirW(utf16_dir_path[0..len]);
23202316 } else {
23212317 const dir_path_c = try toPosixPath(dir_path);
23222318 return chdirZ(&dir_path_c);
......@@ -2328,7 +2324,10 @@ pub const chdirC = @compileError("deprecated: renamed to chdirZ");
23282324/// Same as `chdir` except the parameter is null-terminated.
23292325pub fn chdirZ(dir_path: [*:0]const u8) ChangeCurDirError!void {
23302326 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]);
23322331 }
23332332 switch (errno(system.chdir(dir_path))) {
23342333 0 => return,
......@@ -2344,6 +2343,14 @@ pub fn chdirZ(dir_path: [*:0]const u8) ChangeCurDirError!void {
23442343 }
23452344}
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
23472354pub const FchdirError = error{
23482355 AccessDenied,
23492356 NotDir,
lib/std/os/test.zig+24
......@@ -25,6 +25,30 @@ const tmpDir = std.testing.tmpDir;
2525const Dir = std.fs.Dir;
2626const 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
2852test "open smoke test" {
2953 if (builtin.os.tag == .wasi) return error.SkipZigTest;
3054