authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-15 17:40:39+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-22 08:51:22+02:00
loga8a02dfbfaf4b9bd303712c853e202bd07837371
treed415077ae1c6aec8f70ad76f5bd3d575ec88106d
parent30f1176a545111e8bdce3362d6bdbf7ef75dce2a

Add smoke test for dir symlinks


1 files changed, 28 insertions(+), 24 deletions(-)

lib/std/os/test.zig+28-24
......@@ -44,7 +44,7 @@ test "fstatat" {
4444test "readlink" {
4545 if (builtin.os.tag == .wasi) return error.SkipZigTest;
4646
47 // First, try relative paths
47 // First, try relative paths in cwd
4848 {
4949 var cwd = fs.cwd();
5050 try cwd.writeFile("file.txt", "nonsense");
......@@ -58,37 +58,41 @@ test "readlink" {
5858 try cwd.deleteFile("symlinked");
5959 }
6060
61 // Next, let's try fully-qualified paths
62 {
63 var tmp = tmpDir(.{});
64 // defer tmp.cleanup();
65
66 // create file
67 try tmp.dir.writeFile("file.txt", "nonsense");
68
69 // get paths
70 // TODO: use Dir's realpath function once that exists
71 var arena = ArenaAllocator.init(testing.allocator);
72 defer arena.deinit();
73
74 const base_path = blk: {
75 const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
76 break :blk try fs.realpathAlloc(&arena.allocator, relative_path);
77 };
78 const target_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "file.txt" });
79 const symlink_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "symlinked" });
61 // Now, let's use tempdir
62 var tmp = tmpDir(.{});
63 // defer tmp.cleanup();
64
65 // Create some targets
66 try tmp.dir.writeFile("file.txt", "nonsense");
67 try tmp.dir.makeDir("subdir");
68
69 // Get base abs path
70 var arena = ArenaAllocator.init(testing.allocator);
71 defer arena.deinit();
72
73 const base_path = blk: {
74 const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
75 break :blk try fs.realpathAlloc(&arena.allocator, relative_path);
76 };
77
78 try testReadlink(&arena.allocator, base_path, "file.txt", "symlink1", false);
79 try testReadlink(&arena.allocator, base_path, "subdir", "symlink2", true);
80}
81
82fn testReadlink(allocator: *mem.Allocator, base_path: []const u8, target_name: []const u8, symlink_name: []const u8, is_dir: bool) !void {
83 const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, target_name });
84 const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, symlink_name });
8085 std.debug.warn("\ntarget_path={}\n", .{target_path});
8186 std.debug.warn("symlink_path={}\n", .{symlink_path});
8287
83 // create symbolic link by path
84 try os.symlink(target_path, symlink_path, .{});
88 // Create symbolic link by path
89 try os.symlink(target_path, symlink_path, .{ .is_directory = is_dir });
8590
86 // now, read the link and verify
91 // Read the link and verify
8792 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
8893 const given = try os.readlink(symlink_path, buffer[0..]);
8994 std.debug.warn("given={}\n", .{given});
9095 expect(mem.eql(u8, target_path, given));
91 }
9296}
9397
9498test "readlinkat" {