| ... | ... | @@ -180,6 +180,42 @@ test "Dir.Iterator" { |
| 180 | 180 | try testing.expect(contains(&entries, Dir.Entry{ .name = "some_dir", .kind = Dir.Entry.Kind.Directory })); |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | // TODO Only passes on Windows, see https://github.com/ziglang/zig/issues/10317 |
| 184 | test "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 | |
| 183 | 219 | fn entryEql(lhs: Dir.Entry, rhs: Dir.Entry) bool { |
| 184 | 220 | return mem.eql(u8, lhs.name, rhs.name) and lhs.kind == rhs.kind; |
| 185 | 221 | } |