| ... | ... | @@ -1,9 +1,7 @@ |
| 1 | 1 | const std = @import("../std.zig"); |
| 2 | 2 | const builtin = std.builtin; |
| 3 | 3 | const fs = std.fs; |
| 4 | | const Dir = std.fs.Dir; |
| 5 | 4 | const File = std.fs.File; |
| 6 | | const tmpDir = std.testing.tmpDir; |
| 7 | 5 | |
| 8 | 6 | test "openSelfExe" { |
| 9 | 7 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| ... | ... | @@ -17,18 +15,16 @@ const FILE_LOCK_TEST_SLEEP_TIME = 5 * std.time.millisecond; |
| 17 | 15 | test "open file with exclusive nonblocking lock twice" { |
| 18 | 16 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 19 | 17 | |
| 20 | | var tmp = tmpDir(.{}); |
| 21 | | defer tmp.cleanup(); |
| 22 | | |
| 18 | const dir = fs.cwd(); |
| 23 | 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 | 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 | 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 | 28 | error.FileNotFound => {}, |
| 33 | 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 | 39 | } |
| 44 | 40 | |
| 45 | 41 | const filename = "file_lock_test.txt"; |
| 46 | | |
| 47 | | var tmp = tmpDir(.{}); |
| 48 | | defer tmp.cleanup(); |
| 49 | | |
| 50 | 42 | var contexts = [_]FileLockTestContext{ |
| 51 | | .{ .dir = tmp.dir, .filename = filename, .create = true, .lock = .Exclusive }, |
| 52 | | .{ .dir = tmp.dir, .filename = filename, .create = true, .lock = .Exclusive }, |
| 43 | .{ .filename = filename, .create = true, .lock = .Exclusive }, |
| 44 | .{ .filename = filename, .create = true, .lock = .Exclusive }, |
| 53 | 45 | }; |
| 54 | 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 | 57 | |
| 66 | 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 | 61 | error.FileNotFound => {}, |
| 70 | 62 | else => return err, |
| 71 | 63 | }; |
| ... | ... | @@ -87,15 +79,12 @@ test "create file, lock and read from multiple process at once" { |
| 87 | 79 | const filename = "file_read_lock_test.txt"; |
| 88 | 80 | const filedata = "Hello, world!\n"; |
| 89 | 81 | |
| 90 | | var tmp = tmpDir(.{}); |
| 91 | | defer tmp.cleanup(); |
| 92 | | |
| 93 | | try tmp.dir.writeFile(filename, filedata); |
| 82 | try fs.cwd().writeFile(filename, filedata); |
| 94 | 83 | |
| 95 | 84 | var contexts = [_]FileLockTestContext{ |
| 96 | | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Shared }, |
| 97 | | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Shared }, |
| 98 | | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Exclusive }, |
| 85 | .{ .filename = filename, .create = false, .lock = .Shared }, |
| 86 | .{ .filename = filename, .create = false, .lock = .Shared }, |
| 87 | .{ .filename = filename, .create = false, .lock = .Exclusive }, |
| 99 | 88 | }; |
| 100 | 89 | |
| 101 | 90 | try run_lock_file_test(&contexts); |
| ... | ... | @@ -118,7 +107,7 @@ test "create file, lock and read from multiple process at once" { |
| 118 | 107 | std.debug.assert(contexts[0].bytes_read.? == filedata.len); |
| 119 | 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 | 111 | error.FileNotFound => {}, |
| 123 | 112 | else => return err, |
| 124 | 113 | }; |
| ... | ... | @@ -143,7 +132,6 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { |
| 143 | 132 | } |
| 144 | 133 | |
| 145 | 134 | const FileLockTestContext = struct { |
| 146 | | dir: Dir, |
| 147 | 135 | filename: []const u8, |
| 148 | 136 | pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null, |
| 149 | 137 | |
| ... | ... | @@ -165,12 +153,12 @@ const FileLockTestContext = struct { |
| 165 | 153 | fn run(ctx: *@This()) void { |
| 166 | 154 | var file: File = undefined; |
| 167 | 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 | 157 | ctx.err = err; |
| 170 | 158 | return; |
| 171 | 159 | }; |
| 172 | 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 | 162 | ctx.err = err; |
| 175 | 163 | return; |
| 176 | 164 | }; |