aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorRyan Liptak <squeek502@hotmail.com>2020-06-26 00:06:23 -0700
committerRyan Liptak <squeek502@hotmail.com>2020-06-26 00:06:23 -0700
commit0e3d74df8a8c06bcc0b1e4bff205f4ab8e4c6c13 (patch)
tree81ee7d0a695f6c61e78cab9af67364cddfd72e7a /lib/std
parente820678ca1c1a66b561e760fb6aae4df4babf8ba (diff)
downloadzig-0e3d74df8a8c06bcc0b1e4bff205f4ab8e4c6c13.tar.gz
zig-0e3d74df8a8c06bcc0b1e4bff205f4ab8e4c6c13.zip
Add tests for using file operations on directories
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/fs/test.zig32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig
index b024f0f4f6..87477549d9 100644
--- a/lib/std/fs/test.zig
+++ b/lib/std/fs/test.zig
@@ -73,6 +73,38 @@ test "directory operations on files" {
file.close();
}
+test "file operations on directories" {
+ var tmp_dir = tmpDir(.{});
+ defer tmp_dir.cleanup();
+
+ const test_dir_name = "test_dir";
+
+ try tmp_dir.dir.makeDir(test_dir_name);
+
+ testing.expectError(error.IsDir, tmp_dir.dir.createFile(test_dir_name, .{}));
+ testing.expectError(error.IsDir, tmp_dir.dir.deleteFile(test_dir_name));
+ testing.expectError(error.IsDir, tmp_dir.dir.readFileAlloc(testing.allocator, test_dir_name, std.math.maxInt(usize)));
+ // note: the `.write = true` is necessary to ensure the error occurs on all platforms
+ testing.expectError(error.IsDir, tmp_dir.dir.openFile(test_dir_name, .{ .write = true }));
+
+ if (builtin.os.tag != .wasi) {
+ // TODO: use Dir's realpath function once that exists
+ const absolute_path = blk: {
+ const relative_path = try fs.path.join(testing.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp_dir.sub_path[0..], test_dir_name });
+ defer testing.allocator.free(relative_path);
+ break :blk try fs.realpathAlloc(testing.allocator, relative_path);
+ };
+ defer testing.allocator.free(absolute_path);
+
+ testing.expectError(error.IsDir, fs.createFileAbsolute(absolute_path, .{}));
+ testing.expectError(error.IsDir, fs.deleteFileAbsolute(absolute_path));
+ }
+
+ // ensure the directory still exists as a sanity check
+ var dir = try tmp_dir.dir.openDir(test_dir_name, .{});
+ dir.close();
+}
+
test "openSelfExe" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;