authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-06-26 00:06:23-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-06-26 00:06:23-07:00
log0e3d74df8a8c06bcc0b1e4bff205f4ab8e4c6c13
tree81ee7d0a695f6c61e78cab9af67364cddfd72e7a
parente820678ca1c1a66b561e760fb6aae4df4babf8ba

Add tests for using file operations on directories


1 files changed, 32 insertions(+), 0 deletions(-)

lib/std/fs/test.zig+32
......@@ -73,6 +73,38 @@ test "directory operations on files" {
7373 file.close();
7474}
7575
76test "file operations on directories" {
77 var tmp_dir = tmpDir(.{});
78 defer tmp_dir.cleanup();
79
80 const test_dir_name = "test_dir";
81
82 try tmp_dir.dir.makeDir(test_dir_name);
83
84 testing.expectError(error.IsDir, tmp_dir.dir.createFile(test_dir_name, .{}));
85 testing.expectError(error.IsDir, tmp_dir.dir.deleteFile(test_dir_name));
86 testing.expectError(error.IsDir, tmp_dir.dir.readFileAlloc(testing.allocator, test_dir_name, std.math.maxInt(usize)));
87 // note: the `.write = true` is necessary to ensure the error occurs on all platforms
88 testing.expectError(error.IsDir, tmp_dir.dir.openFile(test_dir_name, .{ .write = true }));
89
90 if (builtin.os.tag != .wasi) {
91 // TODO: use Dir's realpath function once that exists
92 const absolute_path = blk: {
93 const relative_path = try fs.path.join(testing.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp_dir.sub_path[0..], test_dir_name });
94 defer testing.allocator.free(relative_path);
95 break :blk try fs.realpathAlloc(testing.allocator, relative_path);
96 };
97 defer testing.allocator.free(absolute_path);
98
99 testing.expectError(error.IsDir, fs.createFileAbsolute(absolute_path, .{}));
100 testing.expectError(error.IsDir, fs.deleteFileAbsolute(absolute_path));
101 }
102
103 // ensure the directory still exists as a sanity check
104 var dir = try tmp_dir.dir.openDir(test_dir_name, .{});
105 dir.close();
106}
107
76108test "openSelfExe" {
77109 if (builtin.os.tag == .wasi) return error.SkipZigTest;
78110