aboutsummaryrefslogtreecommitdiff
path: root/lib/std/fs/test.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-05-23 23:26:05 -0400
committerGitHub <noreply@github.com>2021-05-23 23:26:05 -0400
commit8fa6ca6daaca6f22f09ef22ff496f4dc010138ff (patch)
tree5500f8644b91c11d10b5c3a4b976def6e9fe639a /lib/std/fs/test.zig
parentb3e483224020f2b1fa2eff554946d2ffa216004b (diff)
parent78019452f7caf5badc1fac2fe11fbac19449792f (diff)
downloadzig-8fa6ca6daaca6f22f09ef22ff496f4dc010138ff.tar.gz
zig-8fa6ca6daaca6f22f09ef22ff496f4dc010138ff.zip
Merge pull request #8879 from squeek502/dot-and-dotdot-test
Add . and .. tests for std.fs functions
Diffstat (limited to 'lib/std/fs/test.zig')
-rw-r--r--lib/std/fs/test.zig72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig
index bcf4bf5c97..27dd48240d 100644
--- a/lib/std/fs/test.zig
+++ b/lib/std/fs/test.zig
@@ -932,3 +932,75 @@ test "walker" {
try testing.expectEqualStrings(expected_dir_name, try fs.path.relative(allocator, tmp_path, entry.path));
}
}
+
+test ". and .. in fs.Dir functions" {
+ if (builtin.os.tag == .wasi) return error.SkipZigTest;
+
+ var tmp = tmpDir(.{});
+ defer tmp.cleanup();
+
+ try tmp.dir.makeDir("./subdir");
+ try tmp.dir.access("./subdir", .{});
+ var created_subdir = try tmp.dir.openDir("./subdir", .{});
+ created_subdir.close();
+
+ const created_file = try tmp.dir.createFile("./subdir/../file", .{});
+ created_file.close();
+ try tmp.dir.access("./subdir/../file", .{});
+
+ try tmp.dir.copyFile("./subdir/../file", tmp.dir, "./subdir/../copy", .{});
+ try tmp.dir.rename("./subdir/../copy", "./subdir/../rename");
+ const renamed_file = try tmp.dir.openFile("./subdir/../rename", .{});
+ renamed_file.close();
+ try tmp.dir.deleteFile("./subdir/../rename");
+
+ try tmp.dir.writeFile("./subdir/../update", "something");
+ const prev_status = try tmp.dir.updateFile("./subdir/../file", tmp.dir, "./subdir/../update", .{});
+ try testing.expectEqual(fs.PrevStatus.stale, prev_status);
+
+ try tmp.dir.deleteDir("./subdir");
+}
+
+test ". and .. in absolute functions" {
+ if (builtin.os.tag == .wasi) return error.SkipZigTest;
+
+ var tmp = tmpDir(.{});
+ defer tmp.cleanup();
+
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+ const allocator = &arena.allocator;
+
+ const base_path = blk: {
+ const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
+ break :blk try fs.realpathAlloc(&arena.allocator, relative_path);
+ };
+
+ const subdir_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "./subdir" });
+ try fs.makeDirAbsolute(subdir_path);
+ try fs.accessAbsolute(subdir_path, .{});
+ var created_subdir = try fs.openDirAbsolute(subdir_path, .{});
+ created_subdir.close();
+
+ const created_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../file" });
+ const created_file = try fs.createFileAbsolute(created_file_path, .{});
+ created_file.close();
+ try fs.accessAbsolute(created_file_path, .{});
+
+ const copied_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../copy" });
+ try fs.copyFileAbsolute(created_file_path, copied_file_path, .{});
+ const renamed_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../rename" });
+ try fs.renameAbsolute(copied_file_path, renamed_file_path);
+ const renamed_file = try fs.openFileAbsolute(renamed_file_path, .{});
+ renamed_file.close();
+ try fs.deleteFileAbsolute(renamed_file_path);
+
+ const update_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../update" });
+ const update_file = try fs.createFileAbsolute(update_file_path, .{});
+ try update_file.writeAll("something");
+ update_file.close();
+ const prev_status = try fs.updateFileAbsolute(created_file_path, update_file_path, .{});
+ try testing.expectEqual(fs.PrevStatus.stale, prev_status);
+
+ try fs.deleteDirAbsolute(subdir_path);
+}