authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-04-11 14:39:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-12 06:28:16-04:00
log17daba1806896a2e45a2c1b1969a540f44a64d86
treefa6f33e5d4615acc33db6b59f5a680605055e7f2
parentbb4e74103b4ea2019ada1a3d7f6c1a68fff101ce

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.

1 files changed, 33 insertions(+), 7 deletions(-)

lib/std/fs/test.zig+33-7
...@@ -461,18 +461,44 @@ test "Dir.rename directories" {...@@ -461,18 +461,44 @@ test "Dir.rename directories" {
461 file = try dir.openFile("test_file", .{});461 file = try dir.openFile("test_file", .{});
462 file.close();462 file.close();
463 dir.close();463 dir.close();
464}
465
466test "Dir.rename directory onto empty dir" {
467 // TODO: Fix on Windows, see https://github.com/ziglang/zig/issues/6364
468 if (builtin.os.tag == .windows) return error.SkipZigTest;
469
470 var tmp_dir = testing.tmpDir(.{});
471 defer tmp_dir.cleanup();
464472
465 // Try to rename to a non-empty directory now473 try tmp_dir.dir.makeDir("test_dir");
466 var target_dir = try tmp_dir.dir.makeOpenPath("non_empty_target_dir", .{});474 try tmp_dir.dir.makeDir("target_dir");
467 file = try target_dir.createFile("filler", .{ .read = true });475 try tmp_dir.dir.rename("test_dir", "target_dir");
476
477 // Ensure the directory was renamed
478 try testing.expectError(error.FileNotFound, tmp_dir.dir.openDir("test_dir", .{}));
479 var dir = try tmp_dir.dir.openDir("target_dir", .{});
480 dir.close();
481}
482
483test "Dir.rename directory onto non-empty dir" {
484 // TODO: Fix on Windows, see https://github.com/ziglang/zig/issues/6364
485 if (builtin.os.tag == .windows) return error.SkipZigTest;
486
487 var tmp_dir = testing.tmpDir(.{});
488 defer tmp_dir.cleanup();
489
490 try tmp_dir.dir.makeDir("test_dir");
491
492 var target_dir = try tmp_dir.dir.makeOpenPath("target_dir", .{});
493 var file = try target_dir.createFile("test_file", .{ .read = true });
468 file.close();494 file.close();
495 target_dir.close();
469496
470 try testing.expectError(error.PathAlreadyExists, tmp_dir.dir.rename("test_dir_renamed_again", "non_empty_target_dir"));497 // Rename should fail with PathAlreadyExists if target_dir is non-empty
498 try testing.expectError(error.PathAlreadyExists, tmp_dir.dir.rename("test_dir", "target_dir"));
471499
472 // Ensure the directory was not renamed500 // Ensure the directory was not renamed
473 dir = try tmp_dir.dir.openDir("test_dir_renamed_again", .{});501 var dir = try tmp_dir.dir.openDir("test_dir", .{});
474 file = try dir.openFile("test_file", .{});
475 file.close();
476 dir.close();502 dir.close();
477}503}
478504