| ... | ... | @@ -15,15 +15,18 @@ const a = std.testing.allocator; |
| 15 | 15 | const builtin = @import("builtin"); |
| 16 | 16 | const AtomicRmwOp = builtin.AtomicRmwOp; |
| 17 | 17 | const AtomicOrder = builtin.AtomicOrder; |
| 18 | | const getTestDir = std.testing.getTestDir; |
| 18 | const tmpDir = std.testing.tmpDir; |
| 19 | const Dir = std.fs.Dir; |
| 19 | 20 | |
| 20 | 21 | test "makePath, put some files in it, deleteTree" { |
| 21 | | var test_dir = getTestDir(); |
| 22 | | try test_dir.makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); |
| 23 | | try test_dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense"); |
| 24 | | try test_dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah"); |
| 25 | | try test_dir.deleteTree("os_test_tmp"); |
| 26 | | if (test_dir.openDir("os_test_tmp", .{})) |dir| { |
| 22 | var tmp = tmpDir(.{}); |
| 23 | defer tmp.cleanup(); |
| 24 | |
| 25 | try tmp.dir.makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); |
| 26 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense"); |
| 27 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah"); |
| 28 | try tmp.dir.deleteTree("os_test_tmp"); |
| 29 | if (tmp.dir.openDir("os_test_tmp", .{})) |dir| { |
| 27 | 30 | @panic("expected error"); |
| 28 | 31 | } else |err| { |
| 29 | 32 | expect(err == error.FileNotFound); |
| ... | ... | @@ -33,17 +36,19 @@ test "makePath, put some files in it, deleteTree" { |
| 33 | 36 | test "access file" { |
| 34 | 37 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 35 | 38 | |
| 36 | | var test_dir = getTestDir(); |
| 37 | | try test_dir.makePath("os_test_tmp"); |
| 38 | | if (test_dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{})) |ok| { |
| 39 | var tmp = tmpDir(.{}); |
| 40 | defer tmp.cleanup(); |
| 41 | |
| 42 | try tmp.dir.makePath("os_test_tmp"); |
| 43 | if (tmp.dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{})) |ok| { |
| 39 | 44 | @panic("expected error"); |
| 40 | 45 | } else |err| { |
| 41 | 46 | expect(err == error.FileNotFound); |
| 42 | 47 | } |
| 43 | 48 | |
| 44 | | try test_dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", ""); |
| 45 | | try test_dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{}); |
| 46 | | try test_dir.deleteTree("os_test_tmp"); |
| 49 | try tmp.dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", ""); |
| 50 | try tmp.dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{}); |
| 51 | try tmp.dir.deleteTree("os_test_tmp"); |
| 47 | 52 | } |
| 48 | 53 | |
| 49 | 54 | fn testThreadIdFn(thread_id: *Thread.Id) void { |
| ... | ... | @@ -51,11 +56,13 @@ fn testThreadIdFn(thread_id: *Thread.Id) void { |
| 51 | 56 | } |
| 52 | 57 | |
| 53 | 58 | test "sendfile" { |
| 54 | | var test_dir = getTestDir(); |
| 55 | | try test_dir.makePath("os_test_tmp"); |
| 56 | | defer test_dir.deleteTree("os_test_tmp") catch {}; |
| 59 | var tmp = tmpDir(.{}); |
| 60 | defer tmp.cleanup(); |
| 61 | |
| 62 | try tmp.dir.makePath("os_test_tmp"); |
| 63 | defer tmp.dir.deleteTree("os_test_tmp") catch {}; |
| 57 | 64 | |
| 58 | | var dir = try test_dir.openDir("os_test_tmp", .{}); |
| 65 | var dir = try tmp.dir.openDir("os_test_tmp", .{}); |
| 59 | 66 | defer dir.close(); |
| 60 | 67 | |
| 61 | 68 | const line1 = "line1\n"; |
| ... | ... | @@ -119,23 +126,24 @@ test "fs.copyFile" { |
| 119 | 126 | const dest_file = "tmp_test_copy_file2.txt"; |
| 120 | 127 | const dest_file2 = "tmp_test_copy_file3.txt"; |
| 121 | 128 | |
| 122 | | const test_dir = getTestDir(); |
| 129 | var tmp = tmpDir(.{}); |
| 130 | defer tmp.cleanup(); |
| 123 | 131 | |
| 124 | | try test_dir.writeFile(src_file, data); |
| 125 | | defer test_dir.deleteFile(src_file) catch {}; |
| 132 | try tmp.dir.writeFile(src_file, data); |
| 133 | defer tmp.dir.deleteFile(src_file) catch {}; |
| 126 | 134 | |
| 127 | | try test_dir.copyFile(src_file, test_dir, dest_file, .{}); |
| 128 | | defer test_dir.deleteFile(dest_file) catch {}; |
| 135 | try tmp.dir.copyFile(src_file, tmp.dir, dest_file, .{}); |
| 136 | defer tmp.dir.deleteFile(dest_file) catch {}; |
| 129 | 137 | |
| 130 | | try test_dir.copyFile(src_file, test_dir, dest_file2, .{ .override_mode = File.default_mode }); |
| 131 | | defer test_dir.deleteFile(dest_file2) catch {}; |
| 138 | try tmp.dir.copyFile(src_file, tmp.dir, dest_file2, .{ .override_mode = File.default_mode }); |
| 139 | defer tmp.dir.deleteFile(dest_file2) catch {}; |
| 132 | 140 | |
| 133 | | try expectFileContents(dest_file, data); |
| 134 | | try expectFileContents(dest_file2, data); |
| 141 | try expectFileContents(tmp.dir, dest_file, data); |
| 142 | try expectFileContents(tmp.dir, dest_file2, data); |
| 135 | 143 | } |
| 136 | 144 | |
| 137 | | fn expectFileContents(file_path: []const u8, data: []const u8) !void { |
| 138 | | const contents = try getTestDir().readFileAlloc(testing.allocator, file_path, 1000); |
| 145 | fn expectFileContents(dir: Dir, file_path: []const u8, data: []const u8) !void { |
| 146 | const contents = try dir.readFileAlloc(testing.allocator, file_path, 1000); |
| 139 | 147 | defer testing.allocator.free(contents); |
| 140 | 148 | |
| 141 | 149 | testing.expectEqualSlices(u8, data, contents); |
| ... | ... | @@ -199,18 +207,21 @@ test "AtomicFile" { |
| 199 | 207 | \\ hello! |
| 200 | 208 | \\ this is a test file |
| 201 | 209 | ; |
| 202 | | var test_dir = getTestDir(); |
| 210 | |
| 211 | var tmp = tmpDir(.{}); |
| 212 | defer tmp.cleanup(); |
| 213 | |
| 203 | 214 | { |
| 204 | | var af = try test_dir.atomicFile(test_out_file, .{}); |
| 215 | var af = try tmp.dir.atomicFile(test_out_file, .{}); |
| 205 | 216 | defer af.deinit(); |
| 206 | 217 | try af.file.writeAll(test_content); |
| 207 | 218 | try af.finish(); |
| 208 | 219 | } |
| 209 | | const content = try test_dir.readFileAlloc(testing.allocator, test_out_file, 9999); |
| 220 | const content = try tmp.dir.readFileAlloc(testing.allocator, test_out_file, 9999); |
| 210 | 221 | defer testing.allocator.free(content); |
| 211 | 222 | expect(mem.eql(u8, content, test_content)); |
| 212 | 223 | |
| 213 | | try test_dir.deleteFile(test_out_file); |
| 224 | try tmp.dir.deleteFile(test_out_file); |
| 214 | 225 | } |
| 215 | 226 | |
| 216 | 227 | test "thread local storage" { |
| ... | ... | @@ -365,6 +376,9 @@ test "mmap" { |
| 365 | 376 | if (builtin.os.tag == .windows or builtin.os.tag == .wasi) |
| 366 | 377 | return error.SkipZigTest; |
| 367 | 378 | |
| 379 | var tmp = tmpDir(.{}); |
| 380 | defer tmp.cleanup(); |
| 381 | |
| 368 | 382 | // Simple mmap() call with non page-aligned size |
| 369 | 383 | { |
| 370 | 384 | const data = try os.mmap( |
| ... | ... | @@ -393,7 +407,7 @@ test "mmap" { |
| 393 | 407 | |
| 394 | 408 | // Create a file used for testing mmap() calls with a file descriptor |
| 395 | 409 | { |
| 396 | | const file = try fs.cwd().createFile(test_out_file, .{}); |
| 410 | const file = try tmp.dir.createFile(test_out_file, .{}); |
| 397 | 411 | defer file.close(); |
| 398 | 412 | |
| 399 | 413 | const stream = file.outStream(); |
| ... | ... | @@ -406,7 +420,7 @@ test "mmap" { |
| 406 | 420 | |
| 407 | 421 | // Map the whole file |
| 408 | 422 | { |
| 409 | | const file = try fs.cwd().openFile(test_out_file, .{}); |
| 423 | const file = try tmp.dir.openFile(test_out_file, .{}); |
| 410 | 424 | defer file.close(); |
| 411 | 425 | |
| 412 | 426 | const data = try os.mmap( |
| ... | ... | @@ -430,7 +444,7 @@ test "mmap" { |
| 430 | 444 | |
| 431 | 445 | // Map the upper half of the file |
| 432 | 446 | { |
| 433 | | const file = try fs.cwd().openFile(test_out_file, .{}); |
| 447 | const file = try tmp.dir.openFile(test_out_file, .{}); |
| 434 | 448 | defer file.close(); |
| 435 | 449 | |
| 436 | 450 | const data = try os.mmap( |
| ... | ... | @@ -452,7 +466,7 @@ test "mmap" { |
| 452 | 466 | } |
| 453 | 467 | } |
| 454 | 468 | |
| 455 | | try fs.cwd().deleteFile(test_out_file); |
| 469 | try tmp.dir.deleteFile(test_out_file); |
| 456 | 470 | } |
| 457 | 471 | |
| 458 | 472 | test "getenv" { |
| ... | ... | @@ -467,12 +481,15 @@ test "fcntl" { |
| 467 | 481 | if (builtin.os.tag == .windows or builtin.os.tag == .wasi) |
| 468 | 482 | return error.SkipZigTest; |
| 469 | 483 | |
| 484 | var tmp = tmpDir(.{}); |
| 485 | defer tmp.cleanup(); |
| 486 | |
| 470 | 487 | const test_out_file = "os_tmp_test"; |
| 471 | 488 | |
| 472 | | const file = try fs.cwd().createFile(test_out_file, .{}); |
| 489 | const file = try tmp.dir.createFile(test_out_file, .{}); |
| 473 | 490 | defer { |
| 474 | 491 | file.close(); |
| 475 | | fs.cwd().deleteFile(test_out_file) catch {}; |
| 492 | tmp.dir.deleteFile(test_out_file) catch {}; |
| 476 | 493 | } |
| 477 | 494 | |
| 478 | 495 | // Note: The test assumes createFile opens the file with O_CLOEXEC |