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.
This commit is contained in:
Ryan Liptak 2022-04-11 14:39:45 -07:00 committed by Andrew Kelley
parent bb4e74103b
commit 17daba1806

View File

@ -461,18 +461,44 @@ test "Dir.rename directories" {
file = try dir.openFile("test_file", .{});
file.close();
dir.close();
}
// 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 });
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 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();
}