authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-05-18 22:22:27+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-05-18 22:22:27+02:00
logcd8daa533ad7e53a3d071dcb782bc14c6fc72711
tree6ddb2c9ce9914d88cae3883c45356d2ef1071873
parent57719006bbae3d003f442864339ad7d30a8e05cc

Undo accidentally checked-in changes to fs/test.zig


1 files changed, 14 insertions(+), 26 deletions(-)

lib/std/fs/test.zig+14-26
...@@ -1,9 +1,7 @@...@@ -1,9 +1,7 @@
1const std = @import("../std.zig");1const std = @import("../std.zig");
2const builtin = std.builtin;2const builtin = std.builtin;
3const fs = std.fs;3const fs = std.fs;
4const Dir = std.fs.Dir;
5const File = std.fs.File;4const File = std.fs.File;
6const tmpDir = std.testing.tmpDir;
75
8test "openSelfExe" {6test "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;
17test "open file with exclusive nonblocking lock twice" {15test "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;
1917
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";
2420
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();
2723
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));
3026
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 }
4440
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);
5547
...@@ -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" {
6557
66 std.debug.assert(!contexts[0].overlaps(&contexts[1]));58 std.debug.assert(!contexts[0].overlaps(&contexts[1]));
6759
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";
8981
90 var tmp = tmpDir(.{});82 try fs.cwd().writeFile(filename, filedata);
91 defer tmp.cleanup();
92
93 try tmp.dir.writeFile(filename, filedata);
9483
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 };
10089
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);
120109
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}
144133
145const FileLockTestContext = struct {134const 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,
149137
...@@ -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 };