| ... | ... | @@ -70,29 +70,46 @@ test "readlink" { |
| 70 | 70 | var arena = ArenaAllocator.init(testing.allocator); |
| 71 | 71 | defer arena.deinit(); |
| 72 | 72 | |
| 73 | |
| 73 | 74 | const base_path = blk: { |
| 74 | 75 | const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] }); |
| 75 | 76 | break :blk try fs.realpathAlloc(&arena.allocator, relative_path); |
| 76 | 77 | }; |
| 78 | const allocator = &arena.allocator; |
| 77 | 79 | |
| 78 | | try testReadlink(&arena.allocator, base_path, "file.txt", "symlink1", false); |
| 79 | | try testReadlink(&arena.allocator, base_path, "subdir", "symlink2", true); |
| 80 | | } |
| 80 | { |
| 81 | const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "file.txt" }); |
| 82 | const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "symlink1" }); |
| 83 | std.debug.warn("\ntarget_path={}\n", .{target_path}); |
| 84 | std.debug.warn("symlink_path={}\n", .{symlink_path}); |
| 81 | 85 | |
| 82 | | fn 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 }); |
| 86 | // Create symbolic link by path |
| 87 | try os.symlink(target_path, symlink_path, .{ .is_directory = false }); |
| 88 | try testReadlink(target_path, symlink_path); |
| 89 | } |
| 90 | { |
| 91 | const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "subdir" }); |
| 92 | const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "symlink2" }); |
| 85 | 93 | std.debug.warn("\ntarget_path={}\n", .{target_path}); |
| 86 | 94 | std.debug.warn("symlink_path={}\n", .{symlink_path}); |
| 87 | 95 | |
| 88 | 96 | // Create symbolic link by path |
| 89 | | try os.symlink(target_path, symlink_path, .{ .is_directory = is_dir }); |
| 97 | try os.symlink(target_path, symlink_path, .{ .is_directory = true }); |
| 98 | try testReadlink(target_path, symlink_path); |
| 99 | } |
| 90 | 100 | |
| 91 | | // Read the link and verify |
| 92 | | var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; |
| 93 | | const given = try os.readlink(symlink_path, buffer[0..]); |
| 94 | | std.debug.warn("given={}\n", .{given}); |
| 95 | | expect(mem.eql(u8, target_path, given)); |
| 101 | if (builtin.os.tag == .windows) { |
| 102 | try testReadlink("C:\\ProgramData", "C:\\Users\\All Users"); |
| 103 | try testReadlink("C:\\Users\\Default", "C:\\Users\\Default User"); |
| 104 | try testReadlink("C:\\Users", "C:\\Documents and Settings"); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | fn testReadlink(target_path: []const u8, symlink_path: []const u8) !void { |
| 109 | var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; |
| 110 | const given = try os.readlink(symlink_path, buffer[0..]); |
| 111 | std.debug.warn("given={}\n", .{given}); |
| 112 | expect(mem.eql(u8, target_path, given)); |
| 96 | 113 | } |
| 97 | 114 | |
| 98 | 115 | test "readlinkat" { |