| ... | @@ -3,10 +3,53 @@ const testing = std.testing; | ... | @@ -3,10 +3,53 @@ const testing = std.testing; |
| 3 | const builtin = std.builtin; | 3 | const builtin = std.builtin; |
| 4 | const fs = std.fs; | 4 | const fs = std.fs; |
| 5 | const mem = std.mem; | 5 | const mem = std.mem; |
| | 6 | const wasi = std.os.wasi; |
| 6 | | 7 | |
| | 8 | const ArenaAllocator = std.heap.ArenaAllocator; |
| | 9 | const Dir = std.fs.Dir; |
| 7 | const File = std.fs.File; | 10 | const File = std.fs.File; |
| 8 | const tmpDir = testing.tmpDir; | 11 | const tmpDir = testing.tmpDir; |
| 9 | | 12 | |
| | 13 | test "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 | |
| | 42 | fn 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 | |
| | 46 | fn 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 | |
| 10 | test "readAllAlloc" { | 53 | test "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 | } |
| 239 | | 282 | |
| 240 | fn expectFileContents(dir: fs.Dir, file_path: []const u8, data: []const u8) !void { | 283 | fn 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); |
| 243 | | 286 | |