mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
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:
parent
7e07df06a4
commit
f6bb56f8c7
@ -916,14 +916,30 @@ test "walker" {
|
|||||||
var tmp = tmpDir(.{});
|
var tmp = tmpDir(.{});
|
||||||
defer tmp.cleanup();
|
defer tmp.cleanup();
|
||||||
|
|
||||||
const nb_dirs = 8;
|
// iteration order of walker is undefined, so need lookup maps to check against
|
||||||
|
|
||||||
var i: usize = 0;
|
const expected_paths = std.ComptimeStringMap(void, .{
|
||||||
var sub_dir = tmp.dir;
|
.{"dir1"},
|
||||||
while (i < nb_dirs) : (i += 1) {
|
.{"dir2"},
|
||||||
const dir_name = try std.fmt.allocPrint(allocator, "{}", .{i});
|
.{"dir3"},
|
||||||
try sub_dir.makeDir(dir_name);
|
.{"dir4"},
|
||||||
sub_dir = try sub_dir.openDir(dir_name, .{});
|
.{"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..] });
|
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);
|
var walker = try tmp_dir.walk(testing.allocator);
|
||||||
defer walker.deinit();
|
defer walker.deinit();
|
||||||
|
|
||||||
i = 0;
|
var num_walked: usize = 0;
|
||||||
var expected_dir_name: []const u8 = "";
|
while (try walker.next()) |entry| {
|
||||||
while (i < nb_dirs) : (i += 1) {
|
testing.expect(expected_basenames.has(entry.basename)) catch |err| {
|
||||||
const name = try std.fmt.allocPrint(allocator, "{}", .{i});
|
std.debug.print("found unexpected basename: {s}\n", .{std.fmt.fmtSliceEscapeLower(entry.basename)});
|
||||||
expected_dir_name = if (expected_dir_name.len == 0)
|
return err;
|
||||||
name
|
};
|
||||||
else
|
testing.expect(expected_paths.has(entry.path)) catch |err| {
|
||||||
try fs.path.join(allocator, &[_][]const u8{ expected_dir_name, name });
|
std.debug.print("found unexpected path: {s}\n", .{std.fmt.fmtSliceEscapeLower(entry.path)});
|
||||||
|
return err;
|
||||||
var entry = (try walker.next()).?;
|
};
|
||||||
try testing.expectEqualStrings(expected_dir_name, entry.path);
|
num_walked += 1;
|
||||||
}
|
}
|
||||||
|
try testing.expectEqual(expected_paths.kvs.len, num_walked);
|
||||||
}
|
}
|
||||||
|
|
||||||
test ". and .. in fs.Dir functions" {
|
test ". and .. in fs.Dir functions" {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user