authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-07 23:13:58+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-07-07 23:13:58+00:00
log597a36367305cdb63ffe25af707d708ef9376a7a
treed7e36580dbb19c4843577f841f5b6debf452f549
parent485231deaef9b12e013d423facdef1624f16014b
parent417c92895263250ca399bcf7a1a2abaee6067624
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5755 from kubkon/dir-iter-tests

[libstd]: add Dir.Iterator tests

2 files changed, 46 insertions(+), 1 deletions(-)

lib/std/fs.zig+2
...@@ -453,6 +453,8 @@ pub const Dir = struct {...@@ -453,6 +453,8 @@ pub const Dir = struct {
453453
454 pub const Error = IteratorError;454 pub const Error = IteratorError;
455455
456 /// Memory such as file names referenced in this returned entry becomes invalid
457 /// with subsequent calls to `next`, as well as when this `Dir` is deinitialized.
456 pub fn next(self: *Self) Error!?Entry {458 pub fn next(self: *Self) Error!?Entry {
457 start_over: while (true) {459 start_over: while (true) {
458 const w = os.windows;460 const w = os.windows;
lib/std/fs/test.zig+44-1
...@@ -3,10 +3,53 @@ const testing = std.testing;...@@ -3,10 +3,53 @@ const testing = std.testing;
3const builtin = std.builtin;3const builtin = std.builtin;
4const fs = std.fs;4const fs = std.fs;
5const mem = std.mem;5const mem = std.mem;
6const wasi = std.os.wasi;
67
8const ArenaAllocator = std.heap.ArenaAllocator;
9const Dir = std.fs.Dir;
7const File = std.fs.File;10const File = std.fs.File;
8const tmpDir = testing.tmpDir;11const tmpDir = testing.tmpDir;
912
13test "Dir.Iterator" {
14 var tmp_dir = tmpDir(.{ .iterate = true });
15 defer tmp_dir.cleanup();
16
17 // First, create a couple of entries to iterate over.
18 const file = try tmp_dir.dir.createFile("some_file", .{});
19 file.close();
20
21 try tmp_dir.dir.makeDir("some_dir");
22
23 var arena = ArenaAllocator.init(testing.allocator);
24 defer arena.deinit();
25
26 var entries = std.ArrayList(Dir.Entry).init(&arena.allocator);
27
28 // Create iterator.
29 var iter = tmp_dir.dir.iterate();
30 while (try iter.next()) |entry| {
31 // We cannot just store `entry` as on Windows, we're re-using the name buffer
32 // which means we'll actually share the `name` pointer between entries!
33 const name = try arena.allocator.dupe(u8, entry.name);
34 try entries.append(Dir.Entry{ .name = name, .kind = entry.kind });
35 }
36
37 testing.expect(entries.items.len == 2); // note that the Iterator skips '.' and '..'
38 testing.expect(contains(&entries, Dir.Entry{ .name = "some_file", .kind = Dir.Entry.Kind.File }));
39 testing.expect(contains(&entries, Dir.Entry{ .name = "some_dir", .kind = Dir.Entry.Kind.Directory }));
40}
41
42fn entry_eql(lhs: Dir.Entry, rhs: Dir.Entry) bool {
43 return mem.eql(u8, lhs.name, rhs.name) and lhs.kind == rhs.kind;
44}
45
46fn contains(entries: *const std.ArrayList(Dir.Entry), el: Dir.Entry) bool {
47 for (entries.items) |entry| {
48 if (entry_eql(entry, el)) return true;
49 }
50 return false;
51}
52
10test "readAllAlloc" {53test "readAllAlloc" {
11 var tmp_dir = tmpDir(.{});54 var tmp_dir = tmpDir(.{});
12 defer tmp_dir.cleanup();55 defer tmp_dir.cleanup();
...@@ -237,7 +280,7 @@ test "fs.copyFile" {...@@ -237,7 +280,7 @@ test "fs.copyFile" {
237 try expectFileContents(tmp.dir, dest_file2, data);280 try expectFileContents(tmp.dir, dest_file2, data);
238}281}
239282
240fn expectFileContents(dir: fs.Dir, file_path: []const u8, data: []const u8) !void {283fn expectFileContents(dir: Dir, file_path: []const u8, data: []const u8) !void {
241 const contents = try dir.readFileAlloc(testing.allocator, file_path, 1000);284 const contents = try dir.readFileAlloc(testing.allocator, file_path, 1000);
242 defer testing.allocator.free(contents);285 defer testing.allocator.free(contents);
243286