authorgravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2023-10-09 06:44:14-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-09 16:44:14+03:00
log57874ce619c098a4affd6804a71e0604ff3874c8
treed54550d2f14df5db035f4f1fdd5a80c0f36e3b0f
parent54e7f58fcb8cd3fbfd1b1a3efd090c296b0e728b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

lib/std/fs/test.zig: cleanup (#17443)

* fs/test.zig: use arena allocator more consistently * fs/test.zig: remove unnecessary type information Zig can (now?) implicitly cast a `&.{ "foo"}` when passed to `fs.path.join()`, so the `[_][]const u8` is unnecessary. * fs/test.zig: Use fs.path.join() for longer paths Replace long path constructions (that use several "++ path_sep ++") with a single call to `fs.path.join`. Seems more readable to me. * fs/test.zig: fmt

1 files changed, 44 insertions(+), 47 deletions(-)

lib/std/fs/test.zig+44-47
...@@ -163,12 +163,12 @@ fn testReadLink(dir: Dir, target_path: []const u8, symlink_path: []const u8) !vo...@@ -163,12 +163,12 @@ fn testReadLink(dir: Dir, target_path: []const u8, symlink_path: []const u8) !vo
163test "openDir" {163test "openDir" {
164 try testWithAllSupportedPathTypes(struct {164 try testWithAllSupportedPathTypes(struct {
165 fn impl(ctx: *TestContext) !void {165 fn impl(ctx: *TestContext) !void {
166 const allocator = ctx.arena.allocator();
166 const subdir_path = try ctx.transformPath("subdir");167 const subdir_path = try ctx.transformPath("subdir");
167 try ctx.dir.makeDir(subdir_path);168 try ctx.dir.makeDir(subdir_path);
168169
169 for ([_][]const u8{ "", ".", ".." }) |sub_path| {170 for ([_][]const u8{ "", ".", ".." }) |sub_path| {
170 const dir_path = try fs.path.join(testing.allocator, &[_][]const u8{ subdir_path, sub_path });171 const dir_path = try fs.path.join(allocator, &.{ subdir_path, sub_path });
171 defer testing.allocator.free(dir_path);
172 var dir = try ctx.dir.openDir(dir_path, .{});172 var dir = try ctx.dir.openDir(dir_path, .{});
173 defer dir.close();173 defer dir.close();
174 }174 }
...@@ -187,7 +187,7 @@ test "accessAbsolute" {...@@ -187,7 +187,7 @@ test "accessAbsolute" {
187 const allocator = arena.allocator();187 const allocator = arena.allocator();
188188
189 const base_path = blk: {189 const base_path = blk: {
190 const relative_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });190 const relative_path = try fs.path.join(allocator, &.{ "zig-cache", "tmp", tmp.sub_path[0..] });
191 break :blk try fs.realpathAlloc(allocator, relative_path);191 break :blk try fs.realpathAlloc(allocator, relative_path);
192 };192 };
193193
...@@ -206,7 +206,7 @@ test "openDirAbsolute" {...@@ -206,7 +206,7 @@ test "openDirAbsolute" {
206 const allocator = arena.allocator();206 const allocator = arena.allocator();
207207
208 const base_path = blk: {208 const base_path = blk: {
209 const relative_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..], "subdir" });209 const relative_path = try fs.path.join(allocator, &.{ "zig-cache", "tmp", tmp.sub_path[0..], "subdir" });
210 break :blk try fs.realpathAlloc(allocator, relative_path);210 break :blk try fs.realpathAlloc(allocator, relative_path);
211 };211 };
212212
...@@ -216,8 +216,7 @@ test "openDirAbsolute" {...@@ -216,8 +216,7 @@ test "openDirAbsolute" {
216 }216 }
217217
218 for ([_][]const u8{ ".", ".." }) |sub_path| {218 for ([_][]const u8{ ".", ".." }) |sub_path| {
219 const dir_path = try fs.path.join(allocator, &[_][]const u8{ base_path, sub_path });219 const dir_path = try fs.path.join(allocator, &.{ base_path, sub_path });
220 defer allocator.free(dir_path);
221 var dir = try fs.openDirAbsolute(dir_path, .{});220 var dir = try fs.openDirAbsolute(dir_path, .{});
222 defer dir.close();221 defer dir.close();
223 }222 }
...@@ -270,13 +269,13 @@ test "readLinkAbsolute" {...@@ -270,13 +269,13 @@ test "readLinkAbsolute" {
270 const allocator = arena.allocator();269 const allocator = arena.allocator();
271270
272 const base_path = blk: {271 const base_path = blk: {
273 const relative_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });272 const relative_path = try fs.path.join(allocator, &.{ "zig-cache", "tmp", tmp.sub_path[0..] });
274 break :blk try fs.realpathAlloc(allocator, relative_path);273 break :blk try fs.realpathAlloc(allocator, relative_path);
275 };274 };
276275
277 {276 {
278 const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "file.txt" });277 const target_path = try fs.path.join(allocator, &.{ base_path, "file.txt" });
279 const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "symlink1" });278 const symlink_path = try fs.path.join(allocator, &.{ base_path, "symlink1" });
280279
281 // Create symbolic link by path280 // Create symbolic link by path
282 fs.symLinkAbsolute(target_path, symlink_path, .{}) catch |err| switch (err) {281 fs.symLinkAbsolute(target_path, symlink_path, .{}) catch |err| switch (err) {
...@@ -287,8 +286,8 @@ test "readLinkAbsolute" {...@@ -287,8 +286,8 @@ test "readLinkAbsolute" {
287 try testReadLinkAbsolute(target_path, symlink_path);286 try testReadLinkAbsolute(target_path, symlink_path);
288 }287 }
289 {288 {
290 const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "subdir" });289 const target_path = try fs.path.join(allocator, &.{ base_path, "subdir" });
291 const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "symlink2" });290 const symlink_path = try fs.path.join(allocator, &.{ base_path, "symlink2" });
292291
293 // Create symbolic link by path292 // Create symbolic link by path
294 fs.symLinkAbsolute(target_path, symlink_path, .{ .is_directory = true }) catch |err| switch (err) {293 fs.symLinkAbsolute(target_path, symlink_path, .{ .is_directory = true }) catch |err| switch (err) {
...@@ -485,14 +484,15 @@ test "Dir.realpath smoke test" {...@@ -485,14 +484,15 @@ test "Dir.realpath smoke test" {
485484
486 try testWithAllSupportedPathTypes(struct {485 try testWithAllSupportedPathTypes(struct {
487 fn impl(ctx: *TestContext) !void {486 fn impl(ctx: *TestContext) !void {
487 const allocator = ctx.arena.allocator();
488 const test_file_path = try ctx.transformPath("test_file");488 const test_file_path = try ctx.transformPath("test_file");
489 const test_dir_path = try ctx.transformPath("test_dir");489 const test_dir_path = try ctx.transformPath("test_dir");
490 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;490 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
491491
492 // FileNotFound if the path doesn't exist492 // FileNotFound if the path doesn't exist
493 try testing.expectError(error.FileNotFound, ctx.dir.realpathAlloc(testing.allocator, test_file_path));493 try testing.expectError(error.FileNotFound, ctx.dir.realpathAlloc(allocator, test_file_path));
494 try testing.expectError(error.FileNotFound, ctx.dir.realpath(test_file_path, &buf));494 try testing.expectError(error.FileNotFound, ctx.dir.realpath(test_file_path, &buf));
495 try testing.expectError(error.FileNotFound, ctx.dir.realpathAlloc(testing.allocator, test_dir_path));495 try testing.expectError(error.FileNotFound, ctx.dir.realpathAlloc(allocator, test_dir_path));
496 try testing.expectError(error.FileNotFound, ctx.dir.realpath(test_dir_path, &buf));496 try testing.expectError(error.FileNotFound, ctx.dir.realpath(test_dir_path, &buf));
497497
498 // Now create the file and dir498 // Now create the file and dir
...@@ -500,18 +500,15 @@ test "Dir.realpath smoke test" {...@@ -500,18 +500,15 @@ test "Dir.realpath smoke test" {
500 try ctx.dir.makeDir(test_dir_path);500 try ctx.dir.makeDir(test_dir_path);
501501
502 const base_path = try ctx.transformPath(".");502 const base_path = try ctx.transformPath(".");
503 const base_realpath = try ctx.dir.realpathAlloc(testing.allocator, base_path);503 const base_realpath = try ctx.dir.realpathAlloc(allocator, base_path);
504 defer testing.allocator.free(base_realpath);
505 const expected_file_path = try fs.path.join(504 const expected_file_path = try fs.path.join(
506 testing.allocator,505 allocator,
507 &[_][]const u8{ base_realpath, "test_file" },506 &.{ base_realpath, "test_file" },
508 );507 );
509 defer testing.allocator.free(expected_file_path);
510 const expected_dir_path = try fs.path.join(508 const expected_dir_path = try fs.path.join(
511 testing.allocator,509 allocator,
512 &[_][]const u8{ base_realpath, "test_dir" },510 &.{ base_realpath, "test_dir" },
513 );511 );
514 defer testing.allocator.free(expected_dir_path);
515512
516 // First, test non-alloc version513 // First, test non-alloc version
517 {514 {
...@@ -524,12 +521,10 @@ test "Dir.realpath smoke test" {...@@ -524,12 +521,10 @@ test "Dir.realpath smoke test" {
524521
525 // Next, test alloc version522 // Next, test alloc version
526 {523 {
527 const file_path = try ctx.dir.realpathAlloc(testing.allocator, test_file_path);524 const file_path = try ctx.dir.realpathAlloc(allocator, test_file_path);
528 defer testing.allocator.free(file_path);
529 try testing.expectEqualStrings(expected_file_path, file_path);525 try testing.expectEqualStrings(expected_file_path, file_path);
530526
531 const dir_path = try ctx.dir.realpathAlloc(testing.allocator, test_dir_path);527 const dir_path = try ctx.dir.realpathAlloc(allocator, test_dir_path);
532 defer testing.allocator.free(dir_path);
533 try testing.expectEqualStrings(expected_dir_path, dir_path);528 try testing.expectEqualStrings(expected_dir_path, dir_path);
534 }529 }
535 }530 }
...@@ -849,13 +844,13 @@ test "renameAbsolute" {...@@ -849,13 +844,13 @@ test "renameAbsolute" {
849 const allocator = arena.allocator();844 const allocator = arena.allocator();
850845
851 const base_path = blk: {846 const base_path = blk: {
852 const relative_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp_dir.sub_path[0..] });847 const relative_path = try fs.path.join(allocator, &.{ "zig-cache", "tmp", tmp_dir.sub_path[0..] });
853 break :blk try fs.realpathAlloc(allocator, relative_path);848 break :blk try fs.realpathAlloc(allocator, relative_path);
854 };849 };
855850
856 try testing.expectError(error.FileNotFound, fs.renameAbsolute(851 try testing.expectError(error.FileNotFound, fs.renameAbsolute(
857 try fs.path.join(allocator, &[_][]const u8{ base_path, "missing_file_name" }),852 try fs.path.join(allocator, &.{ base_path, "missing_file_name" }),
858 try fs.path.join(allocator, &[_][]const u8{ base_path, "something_else" }),853 try fs.path.join(allocator, &.{ base_path, "something_else" }),
859 ));854 ));
860855
861 // Renaming files856 // Renaming files
...@@ -864,8 +859,8 @@ test "renameAbsolute" {...@@ -864,8 +859,8 @@ test "renameAbsolute" {
864 var file = try tmp_dir.dir.createFile(test_file_name, .{ .read = true });859 var file = try tmp_dir.dir.createFile(test_file_name, .{ .read = true });
865 file.close();860 file.close();
866 try fs.renameAbsolute(861 try fs.renameAbsolute(
867 try fs.path.join(allocator, &[_][]const u8{ base_path, test_file_name }),862 try fs.path.join(allocator, &.{ base_path, test_file_name }),
868 try fs.path.join(allocator, &[_][]const u8{ base_path, renamed_test_file_name }),863 try fs.path.join(allocator, &.{ base_path, renamed_test_file_name }),
869 );864 );
870865
871 // ensure the file was renamed866 // ensure the file was renamed
...@@ -880,8 +875,8 @@ test "renameAbsolute" {...@@ -880,8 +875,8 @@ test "renameAbsolute" {
880 const renamed_test_dir_name = "test_dir_renamed";875 const renamed_test_dir_name = "test_dir_renamed";
881 try tmp_dir.dir.makeDir(test_dir_name);876 try tmp_dir.dir.makeDir(test_dir_name);
882 try fs.renameAbsolute(877 try fs.renameAbsolute(
883 try fs.path.join(allocator, &[_][]const u8{ base_path, test_dir_name }),878 try fs.path.join(allocator, &.{ base_path, test_dir_name }),
884 try fs.path.join(allocator, &[_][]const u8{ base_path, renamed_test_dir_name }),879 try fs.path.join(allocator, &.{ base_path, renamed_test_dir_name }),
885 );880 );
886881
887 // ensure the directory was renamed882 // ensure the directory was renamed
...@@ -900,11 +895,12 @@ test "openSelfExe" {...@@ -900,11 +895,12 @@ test "openSelfExe" {
900test "makePath, put some files in it, deleteTree" {895test "makePath, put some files in it, deleteTree" {
901 try testWithAllSupportedPathTypes(struct {896 try testWithAllSupportedPathTypes(struct {
902 fn impl(ctx: *TestContext) !void {897 fn impl(ctx: *TestContext) !void {
898 const allocator = ctx.arena.allocator();
903 const dir_path = try ctx.transformPath("os_test_tmp");899 const dir_path = try ctx.transformPath("os_test_tmp");
904900
905 try ctx.dir.makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");901 try ctx.dir.makePath(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c" }));
906 try ctx.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");902 try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c", "file.txt" }), "nonsense");
907 try ctx.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");903 try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "file2.txt" }), "blah");
908904
909 try ctx.dir.deleteTree(dir_path);905 try ctx.dir.deleteTree(dir_path);
910 try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));906 try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));
...@@ -915,11 +911,12 @@ test "makePath, put some files in it, deleteTree" {...@@ -915,11 +911,12 @@ test "makePath, put some files in it, deleteTree" {
915test "makePath, put some files in it, deleteTreeMinStackSize" {911test "makePath, put some files in it, deleteTreeMinStackSize" {
916 try testWithAllSupportedPathTypes(struct {912 try testWithAllSupportedPathTypes(struct {
917 fn impl(ctx: *TestContext) !void {913 fn impl(ctx: *TestContext) !void {
914 const allocator = ctx.arena.allocator();
918 const dir_path = try ctx.transformPath("os_test_tmp");915 const dir_path = try ctx.transformPath("os_test_tmp");
919916
920 try ctx.dir.makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");917 try ctx.dir.makePath(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c" }));
921 try ctx.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");918 try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c", "file.txt" }), "nonsense");
922 try ctx.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");919 try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "file2.txt" }), "blah");
923920
924 try ctx.dir.deleteTreeMinStackSize(dir_path);921 try ctx.dir.deleteTreeMinStackSize(dir_path);
925 try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));922 try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));
...@@ -1204,6 +1201,7 @@ fn expectFileContents(dir: Dir, file_path: []const u8, data: []const u8) !void {...@@ -1204,6 +1201,7 @@ fn expectFileContents(dir: Dir, file_path: []const u8, data: []const u8) !void {
1204test "AtomicFile" {1201test "AtomicFile" {
1205 try testWithAllSupportedPathTypes(struct {1202 try testWithAllSupportedPathTypes(struct {
1206 fn impl(ctx: *TestContext) !void {1203 fn impl(ctx: *TestContext) !void {
1204 const allocator = ctx.arena.allocator();
1207 const test_out_file = try ctx.transformPath("tmp_atomic_file_test_dest.txt");1205 const test_out_file = try ctx.transformPath("tmp_atomic_file_test_dest.txt");
1208 const test_content =1206 const test_content =
1209 \\ hello!1207 \\ hello!
...@@ -1216,8 +1214,7 @@ test "AtomicFile" {...@@ -1216,8 +1214,7 @@ test "AtomicFile" {
1216 try af.file.writeAll(test_content);1214 try af.file.writeAll(test_content);
1217 try af.finish();1215 try af.finish();
1218 }1216 }
1219 const content = try ctx.dir.readFileAlloc(testing.allocator, test_out_file, 9999);1217 const content = try ctx.dir.readFileAlloc(allocator, test_out_file, 9999);
1220 defer testing.allocator.free(content);
1221 try testing.expectEqualStrings(test_content, content);1218 try testing.expectEqualStrings(test_content, content);
12221219
1223 try ctx.dir.deleteFile(test_out_file);1220 try ctx.dir.deleteFile(test_out_file);
...@@ -1337,7 +1334,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {...@@ -1337,7 +1334,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
1337 const cwd = try std.process.getCwdAlloc(gpa);1334 const cwd = try std.process.getCwdAlloc(gpa);
1338 defer gpa.free(cwd);1335 defer gpa.free(cwd);
13391336
1340 const filename = try fs.path.resolve(gpa, &[_][]const u8{ cwd, sub_path });1337 const filename = try fs.path.resolve(gpa, &.{ cwd, sub_path });
1341 defer gpa.free(filename);1338 defer gpa.free(filename);
13421339
1343 const file1 = try fs.createFileAbsolute(filename, .{1340 const file1 = try fs.createFileAbsolute(filename, .{
...@@ -1477,30 +1474,30 @@ test ". and .. in absolute functions" {...@@ -1477,30 +1474,30 @@ test ". and .. in absolute functions" {
1477 const allocator = arena.allocator();1474 const allocator = arena.allocator();
14781475
1479 const base_path = blk: {1476 const base_path = blk: {
1480 const relative_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });1477 const relative_path = try fs.path.join(allocator, &.{ "zig-cache", "tmp", tmp.sub_path[0..] });
1481 break :blk try fs.realpathAlloc(allocator, relative_path);1478 break :blk try fs.realpathAlloc(allocator, relative_path);
1482 };1479 };
14831480
1484 const subdir_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "./subdir" });1481 const subdir_path = try fs.path.join(allocator, &.{ base_path, "./subdir" });
1485 try fs.makeDirAbsolute(subdir_path);1482 try fs.makeDirAbsolute(subdir_path);
1486 try fs.accessAbsolute(subdir_path, .{});1483 try fs.accessAbsolute(subdir_path, .{});
1487 var created_subdir = try fs.openDirAbsolute(subdir_path, .{});1484 var created_subdir = try fs.openDirAbsolute(subdir_path, .{});
1488 created_subdir.close();1485 created_subdir.close();
14891486
1490 const created_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../file" });1487 const created_file_path = try fs.path.join(allocator, &.{ subdir_path, "../file" });
1491 const created_file = try fs.createFileAbsolute(created_file_path, .{});1488 const created_file = try fs.createFileAbsolute(created_file_path, .{});
1492 created_file.close();1489 created_file.close();
1493 try fs.accessAbsolute(created_file_path, .{});1490 try fs.accessAbsolute(created_file_path, .{});
14941491
1495 const copied_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../copy" });1492 const copied_file_path = try fs.path.join(allocator, &.{ subdir_path, "../copy" });
1496 try fs.copyFileAbsolute(created_file_path, copied_file_path, .{});1493 try fs.copyFileAbsolute(created_file_path, copied_file_path, .{});
1497 const renamed_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../rename" });1494 const renamed_file_path = try fs.path.join(allocator, &.{ subdir_path, "../rename" });
1498 try fs.renameAbsolute(copied_file_path, renamed_file_path);1495 try fs.renameAbsolute(copied_file_path, renamed_file_path);
1499 const renamed_file = try fs.openFileAbsolute(renamed_file_path, .{});1496 const renamed_file = try fs.openFileAbsolute(renamed_file_path, .{});
1500 renamed_file.close();1497 renamed_file.close();
1501 try fs.deleteFileAbsolute(renamed_file_path);1498 try fs.deleteFileAbsolute(renamed_file_path);
15021499
1503 const update_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../update" });1500 const update_file_path = try fs.path.join(allocator, &.{ subdir_path, "../update" });
1504 const update_file = try fs.createFileAbsolute(update_file_path, .{});1501 const update_file = try fs.createFileAbsolute(update_file_path, .{});
1505 try update_file.writeAll("something");1502 try update_file.writeAll("something");
1506 update_file.close();1503 update_file.close();