authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2021-08-13 16:22:51-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2021-08-13 16:43:52-07:00
logf6bb56f8c7bd173982d48925d952afb0fd1cd6e5
tree324da5ed7800d901655ceff354b6816f1b36254b
parent7e07df06a4f1507be216c84b034167cdee1817f7

Improve fs.Walker test

- Take into account that iteration order is undefined by checking against a map instead of relying on numerically sorted iteration order - Check both path and basename for each entry instead of just path

1 files changed, 36 insertions(+), 19 deletions(-)

lib/std/fs/test.zig+36-19
...@@ -916,14 +916,30 @@ test "walker" {...@@ -916,14 +916,30 @@ test "walker" {
916 var tmp = tmpDir(.{});916 var tmp = tmpDir(.{});
917 defer tmp.cleanup();917 defer tmp.cleanup();
918918
919 const nb_dirs = 8;919 // iteration order of walker is undefined, so need lookup maps to check against
920920
921 var i: usize = 0;921 const expected_paths = std.ComptimeStringMap(void, .{
922 var sub_dir = tmp.dir;922 .{"dir1"},
923 while (i < nb_dirs) : (i += 1) {923 .{"dir2"},
924 const dir_name = try std.fmt.allocPrint(allocator, "{}", .{i});924 .{"dir3"},
925 try sub_dir.makeDir(dir_name);925 .{"dir4"},
926 sub_dir = try sub_dir.openDir(dir_name, .{});926 .{"dir3" ++ std.fs.path.sep_str ++ "sub1"},
927 .{"dir3" ++ std.fs.path.sep_str ++ "sub2"},
928 .{"dir3" ++ std.fs.path.sep_str ++ "sub2" ++ std.fs.path.sep_str ++ "subsub1"},
929 });
930
931 const expected_basenames = std.ComptimeStringMap(void, .{
932 .{"dir1"},
933 .{"dir2"},
934 .{"dir3"},
935 .{"dir4"},
936 .{"sub1"},
937 .{"sub2"},
938 .{"subsub1"},
939 });
940
941 for (expected_paths.kvs) |kv| {
942 try tmp.dir.makePath(kv.key);
927 }943 }
928944
929 const tmp_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });945 const tmp_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
...@@ -932,18 +948,19 @@ test "walker" {...@@ -932,18 +948,19 @@ test "walker" {
932 var walker = try tmp_dir.walk(testing.allocator);948 var walker = try tmp_dir.walk(testing.allocator);
933 defer walker.deinit();949 defer walker.deinit();
934950
935 i = 0;951 var num_walked: usize = 0;
936 var expected_dir_name: []const u8 = "";952 while (try walker.next()) |entry| {
937 while (i < nb_dirs) : (i += 1) {953 testing.expect(expected_basenames.has(entry.basename)) catch |err| {
938 const name = try std.fmt.allocPrint(allocator, "{}", .{i});954 std.debug.print("found unexpected basename: {s}\n", .{std.fmt.fmtSliceEscapeLower(entry.basename)});
939 expected_dir_name = if (expected_dir_name.len == 0)955 return err;
940 name956 };
941 else957 testing.expect(expected_paths.has(entry.path)) catch |err| {
942 try fs.path.join(allocator, &[_][]const u8{ expected_dir_name, name });958 std.debug.print("found unexpected path: {s}\n", .{std.fmt.fmtSliceEscapeLower(entry.path)});
943959 return err;
944 var entry = (try walker.next()).?;960 };
945 try testing.expectEqualStrings(expected_dir_name, entry.path);961 num_walked += 1;
946 }962 }
963 try testing.expectEqual(expected_paths.kvs.len, num_walked);
947}964}
948965
949test ". and .. in fs.Dir functions" {966test ". and .. in fs.Dir functions" {