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" {
916916 var tmp = tmpDir(.{});
917917 defer tmp.cleanup();
918918
919 const nb_dirs = 8;
920
921 var i: usize = 0;
922 var sub_dir = tmp.dir;
923 while (i < nb_dirs) : (i += 1) {
924 const dir_name = try std.fmt.allocPrint(allocator, "{}", .{i});
925 try sub_dir.makeDir(dir_name);
926 sub_dir = try sub_dir.openDir(dir_name, .{});
919 // iteration order of walker is undefined, so need lookup maps to check against
920
921 const expected_paths = std.ComptimeStringMap(void, .{
922 .{"dir1"},
923 .{"dir2"},
924 .{"dir3"},
925 .{"dir4"},
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);
927943 }
928944
929945 const tmp_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
......@@ -932,18 +948,19 @@ test "walker" {
932948 var walker = try tmp_dir.walk(testing.allocator);
933949 defer walker.deinit();
934950
935 i = 0;
936 var expected_dir_name: []const u8 = "";
937 while (i < nb_dirs) : (i += 1) {
938 const name = try std.fmt.allocPrint(allocator, "{}", .{i});
939 expected_dir_name = if (expected_dir_name.len == 0)
940 name
941 else
942 try fs.path.join(allocator, &[_][]const u8{ expected_dir_name, name });
943
944 var entry = (try walker.next()).?;
945 try testing.expectEqualStrings(expected_dir_name, entry.path);
951 var num_walked: usize = 0;
952 while (try walker.next()) |entry| {
953 testing.expect(expected_basenames.has(entry.basename)) catch |err| {
954 std.debug.print("found unexpected basename: {s}\n", .{std.fmt.fmtSliceEscapeLower(entry.basename)});
955 return err;
956 };
957 testing.expect(expected_paths.has(entry.path)) catch |err| {
958 std.debug.print("found unexpected path: {s}\n", .{std.fmt.fmtSliceEscapeLower(entry.path)});
959 return err;
960 };
961 num_walked += 1;
946962 }
963 try testing.expectEqual(expected_paths.kvs.len, num_walked);
947964}
948965
949966test ". and .. in fs.Dir functions" {