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
163163test "openDir" {
164164 try testWithAllSupportedPathTypes(struct {
165165 fn impl(ctx: *TestContext) !void {
166 const allocator = ctx.arena.allocator();
166167 const subdir_path = try ctx.transformPath("subdir");
167168 try ctx.dir.makeDir(subdir_path);
168169
169170 for ([_][]const u8{ "", ".", ".." }) |sub_path| {
170 const dir_path = try fs.path.join(testing.allocator, &[_][]const u8{ subdir_path, sub_path });
171 defer testing.allocator.free(dir_path);
171 const dir_path = try fs.path.join(allocator, &.{ subdir_path, sub_path });
172172 var dir = try ctx.dir.openDir(dir_path, .{});
173173 defer dir.close();
174174 }
......@@ -187,7 +187,7 @@ test "accessAbsolute" {
187187 const allocator = arena.allocator();
188188
189189 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..] });
191191 break :blk try fs.realpathAlloc(allocator, relative_path);
192192 };
193193
......@@ -206,7 +206,7 @@ test "openDirAbsolute" {
206206 const allocator = arena.allocator();
207207
208208 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" });
210210 break :blk try fs.realpathAlloc(allocator, relative_path);
211211 };
212212
......@@ -216,8 +216,7 @@ test "openDirAbsolute" {
216216 }
217217
218218 for ([_][]const u8{ ".", ".." }) |sub_path| {
219 const dir_path = try fs.path.join(allocator, &[_][]const u8{ base_path, sub_path });
220 defer allocator.free(dir_path);
219 const dir_path = try fs.path.join(allocator, &.{ base_path, sub_path });
221220 var dir = try fs.openDirAbsolute(dir_path, .{});
222221 defer dir.close();
223222 }
......@@ -270,13 +269,13 @@ test "readLinkAbsolute" {
270269 const allocator = arena.allocator();
271270
272271 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..] });
274273 break :blk try fs.realpathAlloc(allocator, relative_path);
275274 };
276275
277276 {
278 const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "file.txt" });
279 const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "symlink1" });
277 const target_path = try fs.path.join(allocator, &.{ base_path, "file.txt" });
278 const symlink_path = try fs.path.join(allocator, &.{ base_path, "symlink1" });
280279
281280 // Create symbolic link by path
282281 fs.symLinkAbsolute(target_path, symlink_path, .{}) catch |err| switch (err) {
......@@ -287,8 +286,8 @@ test "readLinkAbsolute" {
287286 try testReadLinkAbsolute(target_path, symlink_path);
288287 }
289288 {
290 const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "subdir" });
291 const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "symlink2" });
289 const target_path = try fs.path.join(allocator, &.{ base_path, "subdir" });
290 const symlink_path = try fs.path.join(allocator, &.{ base_path, "symlink2" });
292291
293292 // Create symbolic link by path
294293 fs.symLinkAbsolute(target_path, symlink_path, .{ .is_directory = true }) catch |err| switch (err) {
......@@ -485,14 +484,15 @@ test "Dir.realpath smoke test" {
485484
486485 try testWithAllSupportedPathTypes(struct {
487486 fn impl(ctx: *TestContext) !void {
487 const allocator = ctx.arena.allocator();
488488 const test_file_path = try ctx.transformPath("test_file");
489489 const test_dir_path = try ctx.transformPath("test_dir");
490490 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
491491
492492 // 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));
494494 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));
496496 try testing.expectError(error.FileNotFound, ctx.dir.realpath(test_dir_path, &buf));
497497
498498 // Now create the file and dir
......@@ -500,18 +500,15 @@ test "Dir.realpath smoke test" {
500500 try ctx.dir.makeDir(test_dir_path);
501501
502502 const base_path = try ctx.transformPath(".");
503 const base_realpath = try ctx.dir.realpathAlloc(testing.allocator, base_path);
504 defer testing.allocator.free(base_realpath);
503 const base_realpath = try ctx.dir.realpathAlloc(allocator, base_path);
505504 const expected_file_path = try fs.path.join(
506 testing.allocator,
507 &[_][]const u8{ base_realpath, "test_file" },
505 allocator,
506 &.{ base_realpath, "test_file" },
508507 );
509 defer testing.allocator.free(expected_file_path);
510508 const expected_dir_path = try fs.path.join(
511 testing.allocator,
512 &[_][]const u8{ base_realpath, "test_dir" },
509 allocator,
510 &.{ base_realpath, "test_dir" },
513511 );
514 defer testing.allocator.free(expected_dir_path);
515512
516513 // First, test non-alloc version
517514 {
......@@ -524,12 +521,10 @@ test "Dir.realpath smoke test" {
524521
525522 // Next, test alloc version
526523 {
527 const file_path = try ctx.dir.realpathAlloc(testing.allocator, test_file_path);
528 defer testing.allocator.free(file_path);
524 const file_path = try ctx.dir.realpathAlloc(allocator, test_file_path);
529525 try testing.expectEqualStrings(expected_file_path, file_path);
530526
531 const dir_path = try ctx.dir.realpathAlloc(testing.allocator, test_dir_path);
532 defer testing.allocator.free(dir_path);
527 const dir_path = try ctx.dir.realpathAlloc(allocator, test_dir_path);
533528 try testing.expectEqualStrings(expected_dir_path, dir_path);
534529 }
535530 }
......@@ -849,13 +844,13 @@ test "renameAbsolute" {
849844 const allocator = arena.allocator();
850845
851846 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..] });
853848 break :blk try fs.realpathAlloc(allocator, relative_path);
854849 };
855850
856851 try testing.expectError(error.FileNotFound, fs.renameAbsolute(
857 try fs.path.join(allocator, &[_][]const u8{ base_path, "missing_file_name" }),
858 try fs.path.join(allocator, &[_][]const u8{ base_path, "something_else" }),
852 try fs.path.join(allocator, &.{ base_path, "missing_file_name" }),
853 try fs.path.join(allocator, &.{ base_path, "something_else" }),
859854 ));
860855
861856 // Renaming files
......@@ -864,8 +859,8 @@ test "renameAbsolute" {
864859 var file = try tmp_dir.dir.createFile(test_file_name, .{ .read = true });
865860 file.close();
866861 try fs.renameAbsolute(
867 try fs.path.join(allocator, &[_][]const u8{ base_path, test_file_name }),
868 try fs.path.join(allocator, &[_][]const u8{ base_path, renamed_test_file_name }),
862 try fs.path.join(allocator, &.{ base_path, test_file_name }),
863 try fs.path.join(allocator, &.{ base_path, renamed_test_file_name }),
869864 );
870865
871866 // ensure the file was renamed
......@@ -880,8 +875,8 @@ test "renameAbsolute" {
880875 const renamed_test_dir_name = "test_dir_renamed";
881876 try tmp_dir.dir.makeDir(test_dir_name);
882877 try fs.renameAbsolute(
883 try fs.path.join(allocator, &[_][]const u8{ base_path, test_dir_name }),
884 try fs.path.join(allocator, &[_][]const u8{ base_path, renamed_test_dir_name }),
878 try fs.path.join(allocator, &.{ base_path, test_dir_name }),
879 try fs.path.join(allocator, &.{ base_path, renamed_test_dir_name }),
885880 );
886881
887882 // ensure the directory was renamed
......@@ -900,11 +895,12 @@ test "openSelfExe" {
900895test "makePath, put some files in it, deleteTree" {
901896 try testWithAllSupportedPathTypes(struct {
902897 fn impl(ctx: *TestContext) !void {
898 const allocator = ctx.arena.allocator();
903899 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");
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");
907 try ctx.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");
901 try ctx.dir.makePath(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c" }));
902 try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c", "file.txt" }), "nonsense");
903 try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "file2.txt" }), "blah");
908904
909905 try ctx.dir.deleteTree(dir_path);
910906 try testing.expectError(error.FileNotFound, ctx.dir.openDir(dir_path, .{}));
......@@ -915,11 +911,12 @@ test "makePath, put some files in it, deleteTree" {
915911test "makePath, put some files in it, deleteTreeMinStackSize" {
916912 try testWithAllSupportedPathTypes(struct {
917913 fn impl(ctx: *TestContext) !void {
914 const allocator = ctx.arena.allocator();
918915 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");
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");
922 try ctx.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");
917 try ctx.dir.makePath(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c" }));
918 try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "c", "file.txt" }), "nonsense");
919 try ctx.dir.writeFile(try fs.path.join(allocator, &.{ "os_test_tmp", "b", "file2.txt" }), "blah");
923920
924921 try ctx.dir.deleteTreeMinStackSize(dir_path);
925922 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 {
12041201test "AtomicFile" {
12051202 try testWithAllSupportedPathTypes(struct {
12061203 fn impl(ctx: *TestContext) !void {
1204 const allocator = ctx.arena.allocator();
12071205 const test_out_file = try ctx.transformPath("tmp_atomic_file_test_dest.txt");
12081206 const test_content =
12091207 \\ hello!
......@@ -1216,8 +1214,7 @@ test "AtomicFile" {
12161214 try af.file.writeAll(test_content);
12171215 try af.finish();
12181216 }
1219 const content = try ctx.dir.readFileAlloc(testing.allocator, test_out_file, 9999);
1220 defer testing.allocator.free(content);
1217 const content = try ctx.dir.readFileAlloc(allocator, test_out_file, 9999);
12211218 try testing.expectEqualStrings(test_content, content);
12221219
12231220 try ctx.dir.deleteFile(test_out_file);
......@@ -1337,7 +1334,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
13371334 const cwd = try std.process.getCwdAlloc(gpa);
13381335 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 });
13411338 defer gpa.free(filename);
13421339
13431340 const file1 = try fs.createFileAbsolute(filename, .{
......@@ -1477,30 +1474,30 @@ test ". and .. in absolute functions" {
14771474 const allocator = arena.allocator();
14781475
14791476 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..] });
14811478 break :blk try fs.realpathAlloc(allocator, relative_path);
14821479 };
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" });
14851482 try fs.makeDirAbsolute(subdir_path);
14861483 try fs.accessAbsolute(subdir_path, .{});
14871484 var created_subdir = try fs.openDirAbsolute(subdir_path, .{});
14881485 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" });
14911488 const created_file = try fs.createFileAbsolute(created_file_path, .{});
14921489 created_file.close();
14931490 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" });
14961493 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" });
14981495 try fs.renameAbsolute(copied_file_path, renamed_file_path);
14991496 const renamed_file = try fs.openFileAbsolute(renamed_file_path, .{});
15001497 renamed_file.close();
15011498 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" });
15041501 const update_file = try fs.createFileAbsolute(update_file_path, .{});
15051502 try update_file.writeAll("something");
15061503 update_file.close();