From 399c428cb0dfa45fe840315a04526d3794622662 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Mon, 28 Dec 2020 17:50:29 +0100 Subject: [PATCH] fs: add a test for the walker Written like this it triggers the segfault reported in #7560 --- lib/std/fs/test.zig | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index f4d50ca958..3f83f7886b 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -794,3 +794,42 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { try fs.deleteFileAbsolute(filename); } + +test "walker" { + if (builtin.os.tag == .wasi) return error.SkipZigTest; + + var arena = ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + var allocator = &arena.allocator; + + var tmp = tmpDir(.{}); + defer tmp.cleanup(); + + const nb_dirs = 8; + + 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 tmp_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] }); + + var walker = try fs.walkPath(testing.allocator, tmp_path); + 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()).?; + testing.expectEqualStrings(expected_dir_name, try fs.path.relative(allocator, tmp_path, entry.path)); + } +}