authorgravatar for evyatar.shafran@gmail.comEJ <evyatar.shafran@gmail.com> 2026-05-19 03:12:32+02:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2026-05-19 03:12:32+02:00
log5b647b792c680a32c44823a050672537424c95c1
tree742f69f6e76fc8b5bffd6c8f829c757973dccf00
parenteae06cf5cce43c32c73c659a9df376a09896b933

std.Io.Dir: doc comment for `renamePreserve` and tests (#35317)

Right now the comment specifies what will happen when "renaming a file _over_ ... or a directory _over_...". But the whole point of `renamePreserve` is not writing over anything. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35317 Reviewed-by: Ryan Liptak <squeek502@noreply.codeberg.org>

2 files changed, 39 insertions(+), 3 deletions(-)

lib/std/Io/Dir.zig-3
...@@ -1124,9 +1124,6 @@ pub const RenamePreserveError = error{...@@ -1124,9 +1124,6 @@ pub const RenamePreserveError = error{
1124///1124///
1125/// If `new_sub_path` already exists, `error.PathAlreadyExists` will be returned.1125/// If `new_sub_path` already exists, `error.PathAlreadyExists` will be returned.
1126///1126///
1127/// Renaming a file over an existing directory or a directory over an existing
1128/// file will fail with `error.IsDir` or `error.NotDir`
1129///
1130/// * On Windows, both paths should be encoded as [WTF-8](https://wtf-8.codeberg.page/).1127/// * On Windows, both paths should be encoded as [WTF-8](https://wtf-8.codeberg.page/).
1131/// * On WASI, both paths should be encoded as valid UTF-8.1128/// * On WASI, both paths should be encoded as valid UTF-8.
1132/// * On other platforms, both paths are an opaque sequence of bytes with no particular encoding.1129/// * On other platforms, both paths are an opaque sequence of bytes with no particular encoding.
lib/std/fs/test.zig+39
...@@ -1074,6 +1074,45 @@ test "Dir.rename file <-> dir" {...@@ -1074,6 +1074,45 @@ test "Dir.rename file <-> dir" {
1074 }.impl);1074 }.impl);
1075}1075}
10761076
1077test "Dir.renamePreserve onto existing" {
1078 try testWithAllSupportedPathTypes(struct {
1079 fn impl(ctx: *TestContext) !void {
1080 const io = ctx.io;
1081
1082 const test_file_path = try ctx.transformPath("test_file");
1083 const target_file_path = try ctx.transformPath("target_file");
1084 const test_dir_path = try ctx.transformPath("test_dir");
1085 const target_dir_path = try ctx.transformPath("target_dir");
1086
1087 try ctx.dir.writeFile(io, .{ .sub_path = test_file_path, .data = "" });
1088 try ctx.dir.writeFile(io, .{ .sub_path = target_file_path, .data = "" });
1089 try ctx.dir.createDir(io, test_dir_path, .default_dir);
1090 try ctx.dir.createDir(io, target_dir_path, .default_dir);
1091
1092 // file -> file
1093 try expectError(error.PathAlreadyExists, ctx.dir.renamePreserve(test_file_path, ctx.dir, target_file_path, io));
1094 // file -> dir
1095 try expectError(error.PathAlreadyExists, ctx.dir.renamePreserve(test_file_path, ctx.dir, target_dir_path, io));
1096
1097 // TODO: fix dir renaming on non-Linux, non-Windows systems, see https://codeberg.org/ziglang/zig/issues/35340
1098 if (native_os != .windows and native_os != .linux) return;
1099
1100 // dir -> file
1101 try expectError(error.PathAlreadyExists, ctx.dir.renamePreserve(test_dir_path, ctx.dir, target_file_path, io));
1102 // dir -> dir
1103 try expectError(error.PathAlreadyExists, ctx.dir.renamePreserve(test_dir_path, ctx.dir, target_dir_path, io));
1104
1105 // dir -> non-empty dir
1106 {
1107 const target_dir = try ctx.dir.openDir(io, target_dir_path, .{});
1108 defer target_dir.close(io);
1109 try target_dir.writeFile(io, .{ .sub_path = "test_file", .data = "" });
1110 }
1111 try expectError(error.PathAlreadyExists, ctx.dir.renamePreserve(test_dir_path, ctx.dir, target_dir_path, io));
1112 }
1113 }.impl);
1114}
1115
1077test "rename" {1116test "rename" {
1078 const io = testing.io;1117 const io = testing.io;
10791118