authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2021-05-23 14:31:52-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2021-05-23 16:22:27-07:00
log1bc0df72504b6839815dd132e513ee582bdf5399
tree1e8a21ffab369c2e9831e702d4ad5200cb63d69c
parente8236551abdb7bb74811e5e9dc9890cb2abbd269

Add . and .. tests for std.fs functions

These tests check that . and .. are able to be handled by the std.fs functions. Before #7664, these tests would have failed on Windows. Closes #4659

1 files changed, 61 insertions(+), 0 deletions(-)

lib/std/fs/test.zig+61
......@@ -932,3 +932,64 @@ test "walker" {
932932 try testing.expectEqualStrings(expected_dir_name, try fs.path.relative(allocator, tmp_path, entry.path));
933933 }
934934}
935
936test ". and .. in fs.Dir functions" {
937 if (builtin.os.tag == .wasi) return error.SkipZigTest;
938
939 var tmp = tmpDir(.{});
940 defer tmp.cleanup();
941
942 try tmp.dir.makeDir("./subdir");
943 try tmp.dir.access("./subdir", .{});
944 var created_subdir = try tmp.dir.openDir("./subdir", .{});
945 created_subdir.close();
946
947 const created_file = try tmp.dir.createFile("./subdir/../file", .{});
948 created_file.close();
949 try tmp.dir.access("./subdir/../file", .{});
950
951 try tmp.dir.copyFile("./subdir/../file", tmp.dir, "./subdir/../copy", .{});
952 try tmp.dir.rename("./subdir/../copy", "./subdir/../rename");
953 const renamed_file = try tmp.dir.openFile("./subdir/../rename", .{});
954 renamed_file.close();
955 try tmp.dir.deleteFile("./subdir/../rename");
956
957 try tmp.dir.deleteDir("./subdir");
958}
959
960test ". and .. in absolute functions" {
961 if (builtin.os.tag == .wasi) return error.SkipZigTest;
962
963 var tmp = tmpDir(.{});
964 defer tmp.cleanup();
965
966 var arena = ArenaAllocator.init(testing.allocator);
967 defer arena.deinit();
968 const allocator = &arena.allocator;
969
970 const base_path = blk: {
971 const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
972 break :blk try fs.realpathAlloc(&arena.allocator, relative_path);
973 };
974
975 const subdir_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "./subdir" });
976 try fs.makeDirAbsolute(subdir_path);
977 try fs.accessAbsolute(subdir_path, .{});
978 var created_subdir = try fs.openDirAbsolute(subdir_path, .{});
979 created_subdir.close();
980
981 const created_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../file" });
982 const created_file = try fs.createFileAbsolute(created_file_path, .{});
983 created_file.close();
984 try fs.accessAbsolute(created_file_path, .{});
985
986 const copied_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../copy" });
987 try fs.copyFileAbsolute(created_file_path, copied_file_path, .{});
988 const renamed_file_path = try fs.path.join(allocator, &[_][]const u8{ subdir_path, "../rename" });
989 try fs.renameAbsolute(copied_file_path, renamed_file_path);
990 const renamed_file = try fs.openFileAbsolute(renamed_file_path, .{});
991 renamed_file.close();
992 try fs.deleteFileAbsolute(renamed_file_path);
993
994 try fs.deleteDirAbsolute(subdir_path);
995}