authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-25 19:08:30-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-06-25 19:08:30-04:00
log061c8be049508368fba8f9c466fad38ae826c77e
tree522ed379334a0afbd34a7c0dfcfcf03e3cd756ea
parent77bb2dc094bfe9fff9208a8af94d0da617bdae13
parentdcdbb7006ca63a20c003a9928d1cb5e3d0d1cbdb
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5684 from squeek502/fs-file-dir-ops

Add tests for using directory operations on files

2 files changed, 34 insertions(+), 0 deletions(-)

lib/std/fs.zig+1
......@@ -1099,6 +1099,7 @@ pub const Dir = struct {
10991099 .OBJECT_NAME_INVALID => unreachable,
11001100 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
11011101 .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
1102 .NOT_A_DIRECTORY => return error.NotDir,
11021103 .INVALID_PARAMETER => unreachable,
11031104 else => return w.unexpectedStatus(rc),
11041105 }
lib/std/fs/test.zig+33
......@@ -40,6 +40,39 @@ test "readAllAlloc" {
4040 testing.expectError(error.FileTooBig, file.readAllAlloc(testing.allocator, file_size, write_buf.len - 1));
4141}
4242
43test "directory operations on files" {
44 var tmp_dir = tmpDir(.{});
45 defer tmp_dir.cleanup();
46
47 const test_file_name = "test_file";
48
49 var file = try tmp_dir.dir.createFile(test_file_name, .{ .read = true });
50 file.close();
51
52 testing.expectError(error.PathAlreadyExists, tmp_dir.dir.makeDir(test_file_name));
53 testing.expectError(error.NotDir, tmp_dir.dir.openDir(test_file_name, .{}));
54 testing.expectError(error.NotDir, tmp_dir.dir.deleteDir(test_file_name));
55
56 if (builtin.os.tag != .wasi) {
57 // TODO: use Dir's realpath function once that exists
58 const absolute_path = blk: {
59 const relative_path = try fs.path.join(testing.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp_dir.sub_path[0..], test_file_name });
60 defer testing.allocator.free(relative_path);
61 break :blk try fs.realpathAlloc(testing.allocator, relative_path);
62 };
63 defer testing.allocator.free(absolute_path);
64
65 testing.expectError(error.PathAlreadyExists, fs.makeDirAbsolute(absolute_path));
66 testing.expectError(error.NotDir, fs.deleteDirAbsolute(absolute_path));
67 }
68
69 // ensure the file still exists and is a file as a sanity check
70 file = try tmp_dir.dir.openFile(test_file_name, .{});
71 const stat = try file.stat();
72 testing.expect(stat.kind == .File);
73 file.close();
74}
75
4376test "openSelfExe" {
4477 if (builtin.os.tag == .wasi) return error.SkipZigTest;
4578