authorgravatar for 37453713+ominitay@users.noreply.github.comominitay <37453713+ominitay@users.noreply.github.com> 2021-12-24 01:21:35+00:00
committergravatar for 37453713+ominitay@users.noreply.github.comominitay <37453713+ominitay@users.noreply.github.com> 2022-01-27 20:54:48+00:00
logc3ef85aa7654af7f6f45ae9d6497d9fff42e9aff
treef7493c0d0b08f4bba15dd923ef9c6521fc1368dd
parent0e6d2184cacf2dd1fad7508b2f9ae99d78763148
signaturelock-open Commit is signed but in an unrecognized format.

Add test for using `fs.Dir.Iterator` twice


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

lib/std/fs/test.zig+36
......@@ -180,6 +180,42 @@ test "Dir.Iterator" {
180180 try testing.expect(contains(&entries, Dir.Entry{ .name = "some_dir", .kind = Dir.Entry.Kind.Directory }));
181181}
182182
183// TODO Only passes on Windows, see https://github.com/ziglang/zig/issues/10317
184test "Dir.Iterator twice" {
185 if (builtin.os.tag != .windows) return error.SkipZigTest;
186
187 var tmp_dir = tmpDir(.{ .iterate = true });
188 defer tmp_dir.cleanup();
189
190 // First, create a couple of entries to iterate over.
191 const file = try tmp_dir.dir.createFile("some_file", .{});
192 file.close();
193
194 try tmp_dir.dir.makeDir("some_dir");
195
196 var arena = ArenaAllocator.init(testing.allocator);
197 defer arena.deinit();
198 const allocator = arena.allocator();
199
200 var i: u8 = 0;
201 while (i < 2) : (i += 1) {
202 var entries = std.ArrayList(Dir.Entry).init(allocator);
203
204 // Create iterator.
205 var iter = tmp_dir.dir.iterate();
206 while (try iter.next()) |entry| {
207 // We cannot just store `entry` as on Windows, we're re-using the name buffer
208 // which means we'll actually share the `name` pointer between entries!
209 const name = try allocator.dupe(u8, entry.name);
210 try entries.append(Dir.Entry{ .name = name, .kind = entry.kind });
211 }
212
213 try testing.expect(entries.items.len == 2); // note that the Iterator skips '.' and '..'
214 try testing.expect(contains(&entries, Dir.Entry{ .name = "some_file", .kind = Dir.Entry.Kind.File }));
215 try testing.expect(contains(&entries, Dir.Entry{ .name = "some_dir", .kind = Dir.Entry.Kind.Directory }));
216 }
217}
218
183219fn entryEql(lhs: Dir.Entry, rhs: Dir.Entry) bool {
184220 return mem.eql(u8, lhs.name, rhs.name) and lhs.kind == rhs.kind;
185221}