commit 17daba1806896a2e45a2c1b1969a540f44a64d86 (tree)
parent bb4e74103b4ea2019ada1a3d7f6c1a68fff101ce
Author: Ryan Liptak <squeek502@hotmail.com>
Date: Mon, 11 Apr 2022 14:39:45 -0700
std/fs/test.zig: Add test for renaming a dir onto an empty dir
Also split the Dir.rename on directories test into 3 tests:
- General rename of a directory
- Rename of a directory onto an existing empty directory
- Rename of a directory onto an existing non-empty directory
The only new case is the rename onto an existing empty directory, but splitting the tests this way made them much more understandable.
Diffstat:
1 file changed, 33 insertions(+), 7 deletions(-)
diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig
@@ -461,18 +461,44 @@ test "Dir.rename directories" {
file = try dir.openFile("test_file", .{});
file.close();
dir.close();
+}
+
+test "Dir.rename directory onto empty dir" {
+ // TODO: Fix on Windows, see https://github.com/ziglang/zig/issues/6364
+ if (builtin.os.tag == .windows) return error.SkipZigTest;
+
+ var tmp_dir = testing.tmpDir(.{});
+ defer tmp_dir.cleanup();
- // Try to rename to a non-empty directory now
- var target_dir = try tmp_dir.dir.makeOpenPath("non_empty_target_dir", .{});
- file = try target_dir.createFile("filler", .{ .read = true });
+ try tmp_dir.dir.makeDir("test_dir");
+ try tmp_dir.dir.makeDir("target_dir");
+ try tmp_dir.dir.rename("test_dir", "target_dir");
+
+ // Ensure the directory was renamed
+ try testing.expectError(error.FileNotFound, tmp_dir.dir.openDir("test_dir", .{}));
+ var dir = try tmp_dir.dir.openDir("target_dir", .{});
+ dir.close();
+}
+
+test "Dir.rename directory onto non-empty dir" {
+ // TODO: Fix on Windows, see https://github.com/ziglang/zig/issues/6364
+ if (builtin.os.tag == .windows) return error.SkipZigTest;
+
+ var tmp_dir = testing.tmpDir(.{});
+ defer tmp_dir.cleanup();
+
+ try tmp_dir.dir.makeDir("test_dir");
+
+ var target_dir = try tmp_dir.dir.makeOpenPath("target_dir", .{});
+ var file = try target_dir.createFile("test_file", .{ .read = true });
file.close();
+ target_dir.close();
- try testing.expectError(error.PathAlreadyExists, tmp_dir.dir.rename("test_dir_renamed_again", "non_empty_target_dir"));
+ // Rename should fail with PathAlreadyExists if target_dir is non-empty
+ try testing.expectError(error.PathAlreadyExists, tmp_dir.dir.rename("test_dir", "target_dir"));
// Ensure the directory was not renamed
- dir = try tmp_dir.dir.openDir("test_dir_renamed_again", .{});
- file = try dir.openFile("test_file", .{});
- file.close();
+ var dir = try tmp_dir.dir.openDir("test_dir", .{});
dir.close();
}