authorgravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2020-12-28 17:50:29+01:00
committergravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2020-12-28 18:14:27+01:00
log399c428cb0dfa45fe840315a04526d3794622662
tree2da673d381355065bc5c57b2dfc79bf5bc2722a0
parent7ed499ec4549fa7304b822446b23cff993b8fa6f

fs: add a test for the walker

Written like this it triggers the segfault reported in #7560

1 files changed, 39 insertions(+), 0 deletions(-)

lib/std/fs/test.zig+39
...@@ -794,3 +794,42 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {...@@ -794,3 +794,42 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
794794
795 try fs.deleteFileAbsolute(filename);795 try fs.deleteFileAbsolute(filename);
796}796}
797
798test "walker" {
799 if (builtin.os.tag == .wasi) return error.SkipZigTest;
800
801 var arena = ArenaAllocator.init(testing.allocator);
802 defer arena.deinit();
803 var allocator = &arena.allocator;
804
805 var tmp = tmpDir(.{});
806 defer tmp.cleanup();
807
808 const nb_dirs = 8;
809
810 var i: usize = 0;
811 var sub_dir = tmp.dir;
812 while (i < nb_dirs) : (i += 1) {
813 const dir_name = try std.fmt.allocPrint(allocator, "{}", .{i});
814 try sub_dir.makeDir(dir_name);
815 sub_dir = try sub_dir.openDir(dir_name, .{});
816 }
817
818 const tmp_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
819
820 var walker = try fs.walkPath(testing.allocator, tmp_path);
821 defer walker.deinit();
822
823 i = 0;
824 var expected_dir_name: []const u8 = "";
825 while (i < nb_dirs) : (i += 1) {
826 const name = try std.fmt.allocPrint(allocator, "{}", .{i});
827 expected_dir_name = if (expected_dir_name.len == 0)
828 name
829 else
830 try fs.path.join(allocator, &[_][]const u8{ expected_dir_name, name });
831
832 var entry = (try walker.next()).?;
833 testing.expectEqualStrings(expected_dir_name, try fs.path.relative(allocator, tmp_path, entry.path));
834 }
835}