| ... | @@ -1,7 +1,44 @@ | ... | @@ -1,7 +1,44 @@ |
| 1 | const std = @import("../std.zig"); | 1 | const std = @import("../std.zig"); |
| | 2 | const testing = std.testing; |
| 2 | const builtin = std.builtin; | 3 | const builtin = std.builtin; |
| 3 | const fs = std.fs; | 4 | const fs = std.fs; |
| | 5 | const mem = std.mem; |
| | 6 | |
| 4 | const File = std.fs.File; | 7 | const File = std.fs.File; |
| | 8 | const tmpDir = testing.tmpDir; |
| | 9 | |
| | 10 | test "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 | } |
| 5 | | 42 | |
| 6 | test "openSelfExe" { | 43 | test "openSelfExe" { |
| 7 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | 44 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| ... | @@ -116,7 +153,7 @@ test "create file, lock and read from multiple process at once" { | ... | @@ -116,7 +153,7 @@ test "create file, lock and read from multiple process at once" { |
| 116 | test "open file with exclusive nonblocking lock twice (absolute paths)" { | 153 | test "open file with exclusive nonblocking lock twice (absolute paths)" { |
| 117 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | 154 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 118 | | 155 | |
| 119 | const allocator = std.testing.allocator; | 156 | const allocator = testing.allocator; |
| 120 | | 157 | |
| 121 | const file_paths: [1][]const u8 = .{"zig-test-absolute-paths.txt"}; | 158 | const file_paths: [1][]const u8 = .{"zig-test-absolute-paths.txt"}; |
| 122 | const filename = try fs.path.resolve(allocator, &file_paths); | 159 | const filename = try fs.path.resolve(allocator, &file_paths); |
| ... | @@ -126,7 +163,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { | ... | @@ -126,7 +163,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { |
| 126 | | 163 | |
| 127 | const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); | 164 | const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); |
| 128 | file1.close(); | 165 | file1.close(); |
| 129 | std.testing.expectError(error.WouldBlock, file2); | 166 | testing.expectError(error.WouldBlock, file2); |
| 130 | | 167 | |
| 131 | try fs.deleteFileAbsolute(filename); | 168 | try fs.deleteFileAbsolute(filename); |
| 132 | } | 169 | } |
| ... | @@ -187,7 +224,7 @@ const FileLockTestContext = struct { | ... | @@ -187,7 +224,7 @@ const FileLockTestContext = struct { |
| 187 | }; | 224 | }; |
| 188 | | 225 | |
| 189 | fn run_lock_file_test(contexts: []FileLockTestContext) !void { | 226 | fn 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); |
| 191 | defer { | 228 | defer { |
| 192 | for (threads.items) |thread| { | 229 | for (threads.items) |thread| { |
| 193 | thread.wait(); | 230 | thread.wait(); |