authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-06-21 15:18:50+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-22 20:03:21-04:00
log923c0feda1e42dcaf65ab80bafd7400996b39861
treeec079017d4f35d31181f496d2ea6e07138a7c845
parent6ff6ac866dee20f4a39fe92d2fa11605d94e2aec

Add std.fs.File.readAllAlloc tests

This commit adds some unit tests for `std.fs.File.readAllAlloc` function. It also updates the docs of `Reader.readNoEof` which were outdated, and swaps `inStream()` for `reader()` in `File.readAllAlloc` with the former being deprecated.

3 files changed, 42 insertions(+), 6 deletions(-)

lib/std/fs/file.zig+1-1
......@@ -364,7 +364,7 @@ pub const File = struct {
364364 const buf = try allocator.allocWithOptions(u8, size, alignment, optional_sentinel);
365365 errdefer allocator.free(buf);
366366
367 try self.inStream().readNoEof(buf);
367 try self.reader().readNoEof(buf);
368368 return buf;
369369 }
370370
lib/std/fs/test.zig+40-3
......@@ -1,7 +1,44 @@
11const std = @import("../std.zig");
2const testing = std.testing;
23const builtin = std.builtin;
34const fs = std.fs;
5const mem = std.mem;
6
47const File = std.fs.File;
8const tmpDir = testing.tmpDir;
9
10test "readAllAlloc" {
11 var tmp_dir = tmpDir(.{});
12 defer tmp_dir.cleanup();
13
14 var file = try tmp_dir.dir.createFile("test_file", .{ .read = true });
15 defer file.close();
16
17 const buf1 = try file.readAllAlloc(testing.allocator, 0, 1024);
18 defer testing.allocator.free(buf1);
19 testing.expect(buf1.len == 0);
20
21 const write_buf: []const u8 = "this is a test.\nthis is a test.\nthis is a test.\nthis is a test.\n";
22 try file.writeAll(write_buf);
23 try file.seekTo(0);
24 const file_size = try file.getEndPos();
25
26 // max_bytes > file_size
27 const buf2 = try file.readAllAlloc(testing.allocator, file_size, 1024);
28 defer testing.allocator.free(buf2);
29 testing.expectEqual(write_buf.len, buf2.len);
30 testing.expect(std.mem.eql(u8, write_buf, buf2));
31 try file.seekTo(0);
32
33 // max_bytes == file_size
34 const buf3 = try file.readAllAlloc(testing.allocator, file_size, write_buf.len);
35 defer testing.allocator.free(buf3);
36 testing.expectEqual(write_buf.len, buf3.len);
37 testing.expect(std.mem.eql(u8, write_buf, buf3));
38
39 // max_bytes < file_size
40 testing.expectError(error.FileTooBig, file.readAllAlloc(testing.allocator, file_size, write_buf.len - 1));
41}
542
643test "openSelfExe" {
744 if (builtin.os.tag == .wasi) return error.SkipZigTest;
......@@ -116,7 +153,7 @@ test "create file, lock and read from multiple process at once" {
116153test "open file with exclusive nonblocking lock twice (absolute paths)" {
117154 if (builtin.os.tag == .wasi) return error.SkipZigTest;
118155
119 const allocator = std.testing.allocator;
156 const allocator = testing.allocator;
120157
121158 const file_paths: [1][]const u8 = .{"zig-test-absolute-paths.txt"};
122159 const filename = try fs.path.resolve(allocator, &file_paths);
......@@ -126,7 +163,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
126163
127164 const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
128165 file1.close();
129 std.testing.expectError(error.WouldBlock, file2);
166 testing.expectError(error.WouldBlock, file2);
130167
131168 try fs.deleteFileAbsolute(filename);
132169}
......@@ -187,7 +224,7 @@ const FileLockTestContext = struct {
187224};
188225
189226fn run_lock_file_test(contexts: []FileLockTestContext) !void {
190 var threads = std.ArrayList(*std.Thread).init(std.testing.allocator);
227 var threads = std.ArrayList(*std.Thread).init(testing.allocator);
191228 defer {
192229 for (threads.items) |thread| {
193230 thread.wait();
lib/std/io/reader.zig+1-2
......@@ -40,8 +40,7 @@ pub fn Reader(
4040 return index;
4141 }
4242
43 /// Returns the number of bytes read. If the number read would be smaller than buf.len,
44 /// error.EndOfStream is returned instead.
43 /// If the number read would be smaller than `buf.len`, `error.EndOfStream` is returned instead.
4544 pub fn readNoEof(self: Self, buf: []u8) !void {
4645 const amt_read = try self.readAll(buf);
4746 if (amt_read < buf.len) return error.EndOfStream;