authorgravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2023-09-30 22:17:07-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2024-01-04 23:20:35-08:00
log9335529b9be8f7c359e6f2ba79099f263f1a8479
tree255aa8c2aa5ddaf0966ee3262260020b81d8ff4b
parent45836678d76014f94d35a2ffea983c21a57426c4

std.fs: Add several more tests for makePath.


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

lib/std/fs/test.zig+77-7
......@@ -545,7 +545,7 @@ test "Dir.Iterator but dir is deleted during iteration" {
545545 var iterator = subdir.iterate();
546546
547547 // Create something to iterate over within the subdir
548 try tmp.dir.makePath("subdir/b");
548 try tmp.dir.makePath("subdir" ++ fs.path.sep_str ++ "b");
549549
550550 // Then, before iterating, delete the directory that we're iterating.
551551 // This is a contrived reproduction, but this could happen outside of the program, in another thread, etc.
......@@ -754,7 +754,7 @@ test "deleteDir" {
754754 try testWithAllSupportedPathTypes(struct {
755755 fn impl(ctx: *TestContext) !void {
756756 const test_dir_path = try ctx.transformPath("test_dir");
757 const test_file_path = try ctx.transformPath("test_dir" ++ std.fs.path.sep_str ++ "test_file");
757 const test_file_path = try ctx.transformPath("test_dir" ++ fs.path.sep_str ++ "test_file");
758758
759759 // deleting a non-existent directory
760760 try testing.expectError(error.FileNotFound, ctx.dir.deleteDir(test_dir_path));
......@@ -1089,6 +1089,77 @@ test "makePath in a directory that no longer exists" {
10891089 try testing.expectError(error.FileNotFound, tmp.dir.makePath("sub-path"));
10901090}
10911091
1092fn expectDir(dir: Dir, path: []const u8) !void {
1093 var d = try dir.openDir(path, .{});
1094 d.close();
1095}
1096
1097test "makepath existing directories" {
1098 var tmp = tmpDir(.{});
1099 defer tmp.cleanup();
1100
1101 try tmp.dir.makeDir("A");
1102 const tmpA = try tmp.dir.openDir("A", .{});
1103 try tmpA.makeDir("B");
1104
1105 const testPath = "A" ++ fs.path.sep_str ++ "B" ++ fs.path.sep_str ++ "C";
1106 try tmp.dir.makePath(testPath);
1107
1108 try expectDir(tmp.dir, testPath);
1109}
1110
1111test "makepath through existing valid symlink" {
1112 var tmp = tmpDir(.{});
1113 defer tmp.cleanup();
1114
1115 try tmp.dir.makeDir("realfolder");
1116 try tmp.dir.symLink("." ++ fs.path.sep_str ++ "realfolder", "working-symlink", .{});
1117
1118 try tmp.dir.makePath("working-symlink" ++ fs.path.sep_str ++ "in-realfolder");
1119
1120 try expectDir(tmp.dir, "realfolder" ++ fs.path.sep_str ++ "in-realfolder");
1121}
1122
1123test "makepath relative walks" {
1124 var tmp = tmpDir(.{});
1125 defer tmp.cleanup();
1126
1127 const relPath = try fs.path.join(testing.allocator, &.{
1128 "first", "..", "second", "..", "third", "..", "first", "A", "..", "B", "..", "C",
1129 });
1130 defer testing.allocator.free(relPath);
1131
1132 try tmp.dir.makePath(relPath);
1133
1134 // verify created directories exist:
1135 try expectDir(tmp.dir, "first" ++ fs.path.sep_str ++ "A");
1136 try expectDir(tmp.dir, "first" ++ fs.path.sep_str ++ "B");
1137 try expectDir(tmp.dir, "first" ++ fs.path.sep_str ++ "C");
1138 try expectDir(tmp.dir, "second");
1139 try expectDir(tmp.dir, "third");
1140}
1141
1142test "makepath ignores '.'" {
1143 var tmp = tmpDir(.{});
1144 defer tmp.cleanup();
1145
1146 // Path to create, with "." elements:
1147 const dotPath = try fs.path.join(testing.allocator, &.{
1148 "first", ".", "second", ".", "third",
1149 });
1150 defer testing.allocator.free(dotPath);
1151
1152 // Path to expect to find:
1153 const expectedPath = try fs.path.join(testing.allocator, &.{
1154 "first", "second", "third",
1155 });
1156 defer testing.allocator.free(expectedPath);
1157
1158 try tmp.dir.makePath(dotPath);
1159
1160 try expectDir(tmp.dir, expectedPath);
1161}
1162
10921163fn testFilenameLimits(iterable_dir: Dir, maxed_filename: []const u8) !void {
10931164 // setup, create a dir and a nested file both with maxed filenames, and walk the dir
10941165 {
......@@ -1496,6 +1567,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
14961567 .lock = .exclusive,
14971568 .lock_nonblocking = true,
14981569 });
1570 defer fs.deleteFileAbsolute(filename) catch {};
14991571
15001572 const file2 = fs.createFileAbsolute(filename, .{
15011573 .lock = .exclusive,
......@@ -1503,8 +1575,6 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
15031575 });
15041576 file1.close();
15051577 try testing.expectError(error.WouldBlock, file2);
1506
1507 try fs.deleteFileAbsolute(filename);
15081578}
15091579
15101580test "walker" {
......@@ -1520,9 +1590,9 @@ test "walker" {
15201590 .{"dir2"},
15211591 .{"dir3"},
15221592 .{"dir4"},
1523 .{"dir3" ++ std.fs.path.sep_str ++ "sub1"},
1524 .{"dir3" ++ std.fs.path.sep_str ++ "sub2"},
1525 .{"dir3" ++ std.fs.path.sep_str ++ "sub2" ++ std.fs.path.sep_str ++ "subsub1"},
1593 .{"dir3" ++ fs.path.sep_str ++ "sub1"},
1594 .{"dir3" ++ fs.path.sep_str ++ "sub2"},
1595 .{"dir3" ++ fs.path.sep_str ++ "sub2" ++ fs.path.sep_str ++ "subsub1"},
15261596 });
15271597
15281598 const expected_basenames = std.ComptimeStringMap(void, .{