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