| author | |
| committer | |
| log | 626b5eccab7264e579ce58f56be5fbc3aa42efc4 |
| tree | 236b95d0b7d2343de199af587e7fe871365855ba |
| parent | bb55889ce0989e120b85a38aec44865a20844ff0 |
The moved tests do not use `std.os` directly and instead use `std.fs` functions, so it makes more sense for them to be in `fs/test.zig`2 files changed, 157 insertions(+), 157 deletions(-)
lib/std/fs/test.zig+157| ... | ... | @@ -80,6 +80,163 @@ test "openSelfExe" { |
| 80 | 80 | self_exe_file.close(); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | test "makePath, put some files in it, deleteTree" { | |
| 84 | var tmp = tmpDir(.{}); | |
| 85 | defer tmp.cleanup(); | |
| 86 | ||
| 87 | try tmp.dir.makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); | |
| 88 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense"); | |
| 89 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah"); | |
| 90 | try tmp.dir.deleteTree("os_test_tmp"); | |
| 91 | if (tmp.dir.openDir("os_test_tmp", .{})) |dir| { | |
| 92 | @panic("expected error"); | |
| 93 | } else |err| { | |
| 94 | testing.expect(err == error.FileNotFound); | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | test "access file" { | |
| 99 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 100 | ||
| 101 | var tmp = tmpDir(.{}); | |
| 102 | defer tmp.cleanup(); | |
| 103 | ||
| 104 | try tmp.dir.makePath("os_test_tmp"); | |
| 105 | if (tmp.dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{})) |ok| { | |
| 106 | @panic("expected error"); | |
| 107 | } else |err| { | |
| 108 | testing.expect(err == error.FileNotFound); | |
| 109 | } | |
| 110 | ||
| 111 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", ""); | |
| 112 | try tmp.dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{}); | |
| 113 | try tmp.dir.deleteTree("os_test_tmp"); | |
| 114 | } | |
| 115 | ||
| 116 | test "sendfile" { | |
| 117 | var tmp = tmpDir(.{}); | |
| 118 | defer tmp.cleanup(); | |
| 119 | ||
| 120 | try tmp.dir.makePath("os_test_tmp"); | |
| 121 | defer tmp.dir.deleteTree("os_test_tmp") catch {}; | |
| 122 | ||
| 123 | var dir = try tmp.dir.openDir("os_test_tmp", .{}); | |
| 124 | defer dir.close(); | |
| 125 | ||
| 126 | const line1 = "line1\n"; | |
| 127 | const line2 = "second line\n"; | |
| 128 | var vecs = [_]std.os.iovec_const{ | |
| 129 | .{ | |
| 130 | .iov_base = line1, | |
| 131 | .iov_len = line1.len, | |
| 132 | }, | |
| 133 | .{ | |
| 134 | .iov_base = line2, | |
| 135 | .iov_len = line2.len, | |
| 136 | }, | |
| 137 | }; | |
| 138 | ||
| 139 | var src_file = try dir.createFile("sendfile1.txt", .{ .read = true }); | |
| 140 | defer src_file.close(); | |
| 141 | ||
| 142 | try src_file.writevAll(&vecs); | |
| 143 | ||
| 144 | var dest_file = try dir.createFile("sendfile2.txt", .{ .read = true }); | |
| 145 | defer dest_file.close(); | |
| 146 | ||
| 147 | const header1 = "header1\n"; | |
| 148 | const header2 = "second header\n"; | |
| 149 | const trailer1 = "trailer1\n"; | |
| 150 | const trailer2 = "second trailer\n"; | |
| 151 | var hdtr = [_]std.os.iovec_const{ | |
| 152 | .{ | |
| 153 | .iov_base = header1, | |
| 154 | .iov_len = header1.len, | |
| 155 | }, | |
| 156 | .{ | |
| 157 | .iov_base = header2, | |
| 158 | .iov_len = header2.len, | |
| 159 | }, | |
| 160 | .{ | |
| 161 | .iov_base = trailer1, | |
| 162 | .iov_len = trailer1.len, | |
| 163 | }, | |
| 164 | .{ | |
| 165 | .iov_base = trailer2, | |
| 166 | .iov_len = trailer2.len, | |
| 167 | }, | |
| 168 | }; | |
| 169 | ||
| 170 | var written_buf: [100]u8 = undefined; | |
| 171 | try dest_file.writeFileAll(src_file, .{ | |
| 172 | .in_offset = 1, | |
| 173 | .in_len = 10, | |
| 174 | .headers_and_trailers = &hdtr, | |
| 175 | .header_count = 2, | |
| 176 | }); | |
| 177 | const amt = try dest_file.preadAll(&written_buf, 0); | |
| 178 | testing.expect(mem.eql(u8, written_buf[0..amt], "header1\nsecond header\nine1\nsecontrailer1\nsecond trailer\n")); | |
| 179 | } | |
| 180 | ||
| 181 | test "fs.copyFile" { | |
| 182 | const data = "u6wj+JmdF3qHsFPE BUlH2g4gJCmEz0PP"; | |
| 183 | const src_file = "tmp_test_copy_file.txt"; | |
| 184 | const dest_file = "tmp_test_copy_file2.txt"; | |
| 185 | const dest_file2 = "tmp_test_copy_file3.txt"; | |
| 186 | ||
| 187 | var tmp = tmpDir(.{}); | |
| 188 | defer tmp.cleanup(); | |
| 189 | ||
| 190 | try tmp.dir.writeFile(src_file, data); | |
| 191 | defer tmp.dir.deleteFile(src_file) catch {}; | |
| 192 | ||
| 193 | try tmp.dir.copyFile(src_file, tmp.dir, dest_file, .{}); | |
| 194 | defer tmp.dir.deleteFile(dest_file) catch {}; | |
| 195 | ||
| 196 | try tmp.dir.copyFile(src_file, tmp.dir, dest_file2, .{ .override_mode = File.default_mode }); | |
| 197 | defer tmp.dir.deleteFile(dest_file2) catch {}; | |
| 198 | ||
| 199 | try expectFileContents(tmp.dir, dest_file, data); | |
| 200 | try expectFileContents(tmp.dir, dest_file2, data); | |
| 201 | } | |
| 202 | ||
| 203 | fn expectFileContents(dir: fs.Dir, file_path: []const u8, data: []const u8) !void { | |
| 204 | const contents = try dir.readFileAlloc(testing.allocator, file_path, 1000); | |
| 205 | defer testing.allocator.free(contents); | |
| 206 | ||
| 207 | testing.expectEqualSlices(u8, data, contents); | |
| 208 | } | |
| 209 | ||
| 210 | test "AtomicFile" { | |
| 211 | const test_out_file = "tmp_atomic_file_test_dest.txt"; | |
| 212 | const test_content = | |
| 213 | \\ hello! | |
| 214 | \\ this is a test file | |
| 215 | ; | |
| 216 | ||
| 217 | var tmp = tmpDir(.{}); | |
| 218 | defer tmp.cleanup(); | |
| 219 | ||
| 220 | { | |
| 221 | var af = try tmp.dir.atomicFile(test_out_file, .{}); | |
| 222 | defer af.deinit(); | |
| 223 | try af.file.writeAll(test_content); | |
| 224 | try af.finish(); | |
| 225 | } | |
| 226 | const content = try tmp.dir.readFileAlloc(testing.allocator, test_out_file, 9999); | |
| 227 | defer testing.allocator.free(content); | |
| 228 | testing.expect(mem.eql(u8, content, test_content)); | |
| 229 | ||
| 230 | try tmp.dir.deleteFile(test_out_file); | |
| 231 | } | |
| 232 | ||
| 233 | test "realpath" { | |
| 234 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 235 | ||
| 236 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; | |
| 237 | testing.expectError(error.FileNotFound, fs.realpath("definitely_bogus_does_not_exist1234", &buf)); | |
| 238 | } | |
| 239 | ||
| 83 | 240 | const FILE_LOCK_TEST_SLEEP_TIME = 5 * std.time.ns_per_ms; |
| 84 | 241 | |
| 85 | 242 | test "open file with exclusive nonblocking lock twice" { |
lib/std/os/test.zig-157| ... | ... | @@ -59,137 +59,10 @@ test "readlinkat" { |
| 59 | 59 | expect(mem.eql(u8, "file.txt", read_link)); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | test "makePath, put some files in it, deleteTree" { | |
| 63 | var tmp = tmpDir(.{}); | |
| 64 | defer tmp.cleanup(); | |
| 65 | ||
| 66 | try tmp.dir.makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); | |
| 67 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense"); | |
| 68 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah"); | |
| 69 | try tmp.dir.deleteTree("os_test_tmp"); | |
| 70 | if (tmp.dir.openDir("os_test_tmp", .{})) |dir| { | |
| 71 | @panic("expected error"); | |
| 72 | } else |err| { | |
| 73 | expect(err == error.FileNotFound); | |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | test "access file" { | |
| 78 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 79 | ||
| 80 | var tmp = tmpDir(.{}); | |
| 81 | defer tmp.cleanup(); | |
| 82 | ||
| 83 | try tmp.dir.makePath("os_test_tmp"); | |
| 84 | if (tmp.dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{})) |ok| { | |
| 85 | @panic("expected error"); | |
| 86 | } else |err| { | |
| 87 | expect(err == error.FileNotFound); | |
| 88 | } | |
| 89 | ||
| 90 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", ""); | |
| 91 | try tmp.dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{}); | |
| 92 | try tmp.dir.deleteTree("os_test_tmp"); | |
| 93 | } | |
| 94 | ||
| 95 | 62 | fn testThreadIdFn(thread_id: *Thread.Id) void { |
| 96 | 63 | thread_id.* = Thread.getCurrentId(); |
| 97 | 64 | } |
| 98 | 65 | |
| 99 | test "sendfile" { | |
| 100 | var tmp = tmpDir(.{}); | |
| 101 | defer tmp.cleanup(); | |
| 102 | ||
| 103 | try tmp.dir.makePath("os_test_tmp"); | |
| 104 | defer tmp.dir.deleteTree("os_test_tmp") catch {}; | |
| 105 | ||
| 106 | var dir = try tmp.dir.openDir("os_test_tmp", .{}); | |
| 107 | defer dir.close(); | |
| 108 | ||
| 109 | const line1 = "line1\n"; | |
| 110 | const line2 = "second line\n"; | |
| 111 | var vecs = [_]os.iovec_const{ | |
| 112 | .{ | |
| 113 | .iov_base = line1, | |
| 114 | .iov_len = line1.len, | |
| 115 | }, | |
| 116 | .{ | |
| 117 | .iov_base = line2, | |
| 118 | .iov_len = line2.len, | |
| 119 | }, | |
| 120 | }; | |
| 121 | ||
| 122 | var src_file = try dir.createFile("sendfile1.txt", .{ .read = true }); | |
| 123 | defer src_file.close(); | |
| 124 | ||
| 125 | try src_file.writevAll(&vecs); | |
| 126 | ||
| 127 | var dest_file = try dir.createFile("sendfile2.txt", .{ .read = true }); | |
| 128 | defer dest_file.close(); | |
| 129 | ||
| 130 | const header1 = "header1\n"; | |
| 131 | const header2 = "second header\n"; | |
| 132 | const trailer1 = "trailer1\n"; | |
| 133 | const trailer2 = "second trailer\n"; | |
| 134 | var hdtr = [_]os.iovec_const{ | |
| 135 | .{ | |
| 136 | .iov_base = header1, | |
| 137 | .iov_len = header1.len, | |
| 138 | }, | |
| 139 | .{ | |
| 140 | .iov_base = header2, | |
| 141 | .iov_len = header2.len, | |
| 142 | }, | |
| 143 | .{ | |
| 144 | .iov_base = trailer1, | |
| 145 | .iov_len = trailer1.len, | |
| 146 | }, | |
| 147 | .{ | |
| 148 | .iov_base = trailer2, | |
| 149 | .iov_len = trailer2.len, | |
| 150 | }, | |
| 151 | }; | |
| 152 | ||
| 153 | var written_buf: [100]u8 = undefined; | |
| 154 | try dest_file.writeFileAll(src_file, .{ | |
| 155 | .in_offset = 1, | |
| 156 | .in_len = 10, | |
| 157 | .headers_and_trailers = &hdtr, | |
| 158 | .header_count = 2, | |
| 159 | }); | |
| 160 | const amt = try dest_file.preadAll(&written_buf, 0); | |
| 161 | expect(mem.eql(u8, written_buf[0..amt], "header1\nsecond header\nine1\nsecontrailer1\nsecond trailer\n")); | |
| 162 | } | |
| 163 | ||
| 164 | test "fs.copyFile" { | |
| 165 | const data = "u6wj+JmdF3qHsFPE BUlH2g4gJCmEz0PP"; | |
| 166 | const src_file = "tmp_test_copy_file.txt"; | |
| 167 | const dest_file = "tmp_test_copy_file2.txt"; | |
| 168 | const dest_file2 = "tmp_test_copy_file3.txt"; | |
| 169 | ||
| 170 | var tmp = tmpDir(.{}); | |
| 171 | defer tmp.cleanup(); | |
| 172 | ||
| 173 | try tmp.dir.writeFile(src_file, data); | |
| 174 | defer tmp.dir.deleteFile(src_file) catch {}; | |
| 175 | ||
| 176 | try tmp.dir.copyFile(src_file, tmp.dir, dest_file, .{}); | |
| 177 | defer tmp.dir.deleteFile(dest_file) catch {}; | |
| 178 | ||
| 179 | try tmp.dir.copyFile(src_file, tmp.dir, dest_file2, .{ .override_mode = File.default_mode }); | |
| 180 | defer tmp.dir.deleteFile(dest_file2) catch {}; | |
| 181 | ||
| 182 | try expectFileContents(tmp.dir, dest_file, data); | |
| 183 | try expectFileContents(tmp.dir, dest_file2, data); | |
| 184 | } | |
| 185 | ||
| 186 | fn expectFileContents(dir: Dir, file_path: []const u8, data: []const u8) !void { | |
| 187 | const contents = try dir.readFileAlloc(testing.allocator, file_path, 1000); | |
| 188 | defer testing.allocator.free(contents); | |
| 189 | ||
| 190 | testing.expectEqualSlices(u8, data, contents); | |
| 191 | } | |
| 192 | ||
| 193 | 66 | test "std.Thread.getCurrentId" { |
| 194 | 67 | if (builtin.single_threaded) return error.SkipZigTest; |
| 195 | 68 | |
| ... | ... | @@ -242,29 +115,6 @@ test "cpu count" { |
| 242 | 115 | expect(cpu_count >= 1); |
| 243 | 116 | } |
| 244 | 117 | |
| 245 | test "AtomicFile" { | |
| 246 | const test_out_file = "tmp_atomic_file_test_dest.txt"; | |
| 247 | const test_content = | |
| 248 | \\ hello! | |
| 249 | \\ this is a test file | |
| 250 | ; | |
| 251 | ||
| 252 | var tmp = tmpDir(.{}); | |
| 253 | defer tmp.cleanup(); | |
| 254 | ||
| 255 | { | |
| 256 | var af = try tmp.dir.atomicFile(test_out_file, .{}); | |
| 257 | defer af.deinit(); | |
| 258 | try af.file.writeAll(test_content); | |
| 259 | try af.finish(); | |
| 260 | } | |
| 261 | const content = try tmp.dir.readFileAlloc(testing.allocator, test_out_file, 9999); | |
| 262 | defer testing.allocator.free(content); | |
| 263 | expect(mem.eql(u8, content, test_content)); | |
| 264 | ||
| 265 | try tmp.dir.deleteFile(test_out_file); | |
| 266 | } | |
| 267 | ||
| 268 | 118 | test "thread local storage" { |
| 269 | 119 | if (builtin.single_threaded) return error.SkipZigTest; |
| 270 | 120 | const thread1 = try Thread.spawn({}, testTls); |
| ... | ... | @@ -299,13 +149,6 @@ test "getcwd" { |
| 299 | 149 | _ = os.getcwd(&buf) catch undefined; |
| 300 | 150 | } |
| 301 | 151 | |
| 302 | test "realpath" { | |
| 303 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 304 | ||
| 305 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; | |
| 306 | testing.expectError(error.FileNotFound, fs.realpath("definitely_bogus_does_not_exist1234", &buf)); | |
| 307 | } | |
| 308 | ||
| 309 | 152 | test "sigaltstack" { |
| 310 | 153 | if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest; |
| 311 | 154 |