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
This commit is contained in:
Ryan Liptak 2021-08-13 16:22:51 -07:00
parent 7e07df06a4
commit f6bb56f8c7

View File

@ -916,14 +916,30 @@ test "walker" {
var tmp = tmpDir(.{});
defer tmp.cleanup();
const nb_dirs = 8;
// iteration order of walker is undefined, so need lookup maps to check against
var i: usize = 0;
var sub_dir = tmp.dir;
while (i < nb_dirs) : (i += 1) {
const dir_name = try std.fmt.allocPrint(allocator, "{}", .{i});
try sub_dir.makeDir(dir_name);
sub_dir = try sub_dir.openDir(dir_name, .{});
const expected_paths = std.ComptimeStringMap(void, .{
.{"dir1"},
.{"dir2"},
.{"dir3"},
.{"dir4"},
.{"dir3" ++ std.fs.path.sep_str ++ "sub1"},
.{"dir3" ++ std.fs.path.sep_str ++ "sub2"},
.{"dir3" ++ std.fs.path.sep_str ++ "sub2" ++ std.fs.path.sep_str ++ "subsub1"},
});
const expected_basenames = std.ComptimeStringMap(void, .{
.{"dir1"},
.{"dir2"},
.{"dir3"},
.{"dir4"},
.{"sub1"},
.{"sub2"},
.{"subsub1"},
});
for (expected_paths.kvs) |kv| {
try tmp.dir.makePath(kv.key);
}
const tmp_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
@ -932,18 +948,19 @@ test "walker" {
var walker = try tmp_dir.walk(testing.allocator);
defer walker.deinit();
i = 0;
var expected_dir_name: []const u8 = "";
while (i < nb_dirs) : (i += 1) {
const name = try std.fmt.allocPrint(allocator, "{}", .{i});
expected_dir_name = if (expected_dir_name.len == 0)
name
else
try fs.path.join(allocator, &[_][]const u8{ expected_dir_name, name });
var entry = (try walker.next()).?;
try testing.expectEqualStrings(expected_dir_name, entry.path);
var num_walked: usize = 0;
while (try walker.next()) |entry| {
testing.expect(expected_basenames.has(entry.basename)) catch |err| {
std.debug.print("found unexpected basename: {s}\n", .{std.fmt.fmtSliceEscapeLower(entry.basename)});
return err;
};
testing.expect(expected_paths.has(entry.path)) catch |err| {
std.debug.print("found unexpected path: {s}\n", .{std.fmt.fmtSliceEscapeLower(entry.path)});
return err;
};
num_walked += 1;
}
try testing.expectEqual(expected_paths.kvs.len, num_walked);
}
test ". and .. in fs.Dir functions" {