| ... | @@ -1,9 +1,7 @@ | ... | @@ -1,9 +1,7 @@ |
| 1 | const std = @import("../std.zig"); | 1 | const std = @import("../std.zig"); |
| 2 | const builtin = std.builtin; | 2 | const builtin = std.builtin; |
| 3 | const fs = std.fs; | 3 | const fs = std.fs; |
| 4 | const Dir = std.fs.Dir; | | |
| 5 | const File = std.fs.File; | 4 | const File = std.fs.File; |
| 6 | const tmpDir = std.testing.tmpDir; | | |
| 7 | | 5 | |
| 8 | test "openSelfExe" { | 6 | test "openSelfExe" { |
| 9 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | 7 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| ... | @@ -17,18 +15,16 @@ const FILE_LOCK_TEST_SLEEP_TIME = 5 * std.time.millisecond; | ... | @@ -17,18 +15,16 @@ const FILE_LOCK_TEST_SLEEP_TIME = 5 * std.time.millisecond; |
| 17 | test "open file with exclusive nonblocking lock twice" { | 15 | test "open file with exclusive nonblocking lock twice" { |
| 18 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | 16 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 19 | | 17 | |
| 20 | var tmp = tmpDir(.{}); | 18 | const dir = fs.cwd(); |
| 21 | defer tmp.cleanup(); | | |
| 22 | | | |
| 23 | const filename = "file_nonblocking_lock_test.txt"; | 19 | const filename = "file_nonblocking_lock_test.txt"; |
| 24 | | 20 | |
| 25 | const file1 = try tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); | 21 | const file1 = try dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); |
| 26 | defer file1.close(); | 22 | defer file1.close(); |
| 27 | | 23 | |
| 28 | const file2 = tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); | 24 | const file2 = dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); |
| 29 | std.debug.assert(std.meta.eql(file2, error.WouldBlock)); | 25 | std.debug.assert(std.meta.eql(file2, error.WouldBlock)); |
| 30 | | 26 | |
| 31 | tmp.dir.deleteFile(filename) catch |err| switch (err) { | 27 | dir.deleteFile(filename) catch |err| switch (err) { |
| 32 | error.FileNotFound => {}, | 28 | error.FileNotFound => {}, |
| 33 | else => return err, | 29 | else => return err, |
| 34 | }; | 30 | }; |
| ... | @@ -43,13 +39,9 @@ test "open file with lock twice, make sure it wasn't open at the same time" { | ... | @@ -43,13 +39,9 @@ test "open file with lock twice, make sure it wasn't open at the same time" { |
| 43 | } | 39 | } |
| 44 | | 40 | |
| 45 | const filename = "file_lock_test.txt"; | 41 | const filename = "file_lock_test.txt"; |
| 46 | | | |
| 47 | var tmp = tmpDir(.{}); | | |
| 48 | defer tmp.cleanup(); | | |
| 49 | | | |
| 50 | var contexts = [_]FileLockTestContext{ | 42 | var contexts = [_]FileLockTestContext{ |
| 51 | .{ .dir = tmp.dir, .filename = filename, .create = true, .lock = .Exclusive }, | 43 | .{ .filename = filename, .create = true, .lock = .Exclusive }, |
| 52 | .{ .dir = tmp.dir, .filename = filename, .create = true, .lock = .Exclusive }, | 44 | .{ .filename = filename, .create = true, .lock = .Exclusive }, |
| 53 | }; | 45 | }; |
| 54 | try run_lock_file_test(&contexts); | 46 | try run_lock_file_test(&contexts); |
| 55 | | 47 | |
| ... | @@ -65,7 +57,7 @@ test "open file with lock twice, make sure it wasn't open at the same time" { | ... | @@ -65,7 +57,7 @@ test "open file with lock twice, make sure it wasn't open at the same time" { |
| 65 | | 57 | |
| 66 | std.debug.assert(!contexts[0].overlaps(&contexts[1])); | 58 | std.debug.assert(!contexts[0].overlaps(&contexts[1])); |
| 67 | | 59 | |
| 68 | tmp.dir.deleteFile(filename) catch |err| switch (err) { | 60 | fs.cwd().deleteFile(filename) catch |err| switch (err) { |
| 69 | error.FileNotFound => {}, | 61 | error.FileNotFound => {}, |
| 70 | else => return err, | 62 | else => return err, |
| 71 | }; | 63 | }; |
| ... | @@ -87,15 +79,12 @@ test "create file, lock and read from multiple process at once" { | ... | @@ -87,15 +79,12 @@ test "create file, lock and read from multiple process at once" { |
| 87 | const filename = "file_read_lock_test.txt"; | 79 | const filename = "file_read_lock_test.txt"; |
| 88 | const filedata = "Hello, world!\n"; | 80 | const filedata = "Hello, world!\n"; |
| 89 | | 81 | |
| 90 | var tmp = tmpDir(.{}); | 82 | try fs.cwd().writeFile(filename, filedata); |
| 91 | defer tmp.cleanup(); | | |
| 92 | | | |
| 93 | try tmp.dir.writeFile(filename, filedata); | | |
| 94 | | 83 | |
| 95 | var contexts = [_]FileLockTestContext{ | 84 | var contexts = [_]FileLockTestContext{ |
| 96 | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Shared }, | 85 | .{ .filename = filename, .create = false, .lock = .Shared }, |
| 97 | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Shared }, | 86 | .{ .filename = filename, .create = false, .lock = .Shared }, |
| 98 | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Exclusive }, | 87 | .{ .filename = filename, .create = false, .lock = .Exclusive }, |
| 99 | }; | 88 | }; |
| 100 | | 89 | |
| 101 | try run_lock_file_test(&contexts); | 90 | try run_lock_file_test(&contexts); |
| ... | @@ -118,7 +107,7 @@ test "create file, lock and read from multiple process at once" { | ... | @@ -118,7 +107,7 @@ test "create file, lock and read from multiple process at once" { |
| 118 | std.debug.assert(contexts[0].bytes_read.? == filedata.len); | 107 | std.debug.assert(contexts[0].bytes_read.? == filedata.len); |
| 119 | std.debug.assert(contexts[1].bytes_read.? == filedata.len); | 108 | std.debug.assert(contexts[1].bytes_read.? == filedata.len); |
| 120 | | 109 | |
| 121 | tmp.dir.deleteFile(filename) catch |err| switch (err) { | 110 | fs.cwd().deleteFile(filename) catch |err| switch (err) { |
| 122 | error.FileNotFound => {}, | 111 | error.FileNotFound => {}, |
| 123 | else => return err, | 112 | else => return err, |
| 124 | }; | 113 | }; |
| ... | @@ -143,7 +132,6 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { | ... | @@ -143,7 +132,6 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { |
| 143 | } | 132 | } |
| 144 | | 133 | |
| 145 | const FileLockTestContext = struct { | 134 | const FileLockTestContext = struct { |
| 146 | dir: Dir, | | |
| 147 | filename: []const u8, | 135 | filename: []const u8, |
| 148 | pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null, | 136 | pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null, |
| 149 | | 137 | |
| ... | @@ -165,12 +153,12 @@ const FileLockTestContext = struct { | ... | @@ -165,12 +153,12 @@ const FileLockTestContext = struct { |
| 165 | fn run(ctx: *@This()) void { | 153 | fn run(ctx: *@This()) void { |
| 166 | var file: File = undefined; | 154 | var file: File = undefined; |
| 167 | if (ctx.create) { | 155 | if (ctx.create) { |
| 168 | file = ctx.dir.createFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| { | 156 | file = fs.cwd().createFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| { |
| 169 | ctx.err = err; | 157 | ctx.err = err; |
| 170 | return; | 158 | return; |
| 171 | }; | 159 | }; |
| 172 | } else { | 160 | } else { |
| 173 | file = ctx.dir.openFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| { | 161 | file = fs.cwd().openFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| { |
| 174 | ctx.err = err; | 162 | ctx.err = err; |
| 175 | return; | 163 | return; |
| 176 | }; | 164 | }; |