Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
path that requires canonicalization for unix and wasi
test "canonicalize path Unix" {
if (builtin.os.tag == .windows) // symlinks are not portable
return error.SkipZigTest;
// TODO testing partial solution to https://github.com/ziglang/zig/issues/4812
const alloc = testing.allocator;
var tmp = testing.tmpDir(.{ .no_follow = true }); // ie zig-cache/tmp/8DLgoSEqz593PAEE
defer tmp.cleanup();
const tmpdirpath = try tmp.getFullPath(alloc);
defer alloc.free(tmpdirpath);
var cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined; // maximum size of a UTF-8 encoded file path
const cwd = try std.process.getCwd(&cwd_buf);
const s_symlink = "br" ++ ("u" ** 98);
try tmp.dir.symLink(cwd, s_symlink, .{ .is_directory = true });
var ultralong_path = try fs.path.join(alloc, &[_][]const u8{ tmpdirpath, s_symlink });
defer alloc.free(ultralong_path);
{
// ensure symlink works
var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
const given = try tmp.dir.readLink(s_symlink, buffer[0..]);
try testing.expect(mem.eql(u8, cwd, given));
}
const append_path = try fs.path.join(alloc, &[_][]const u8{
"zig-cache",
"tmp",
tmp.sub_path[0..],
s_symlink,
});
defer alloc.free(append_path);
std.debug.assert(fs.MAX_PATH_BYTES > ultralong_path.len);
var min_diff = fs.MAX_PATH_BYTES - ultralong_path.len;
var i: usize = min_diff / append_path.len + 1;
std.debug.print("ultralong_path needs {d} concatenations\n", .{i});
while (i > 0) : (i -= 1) {
// debug: add test here
const tmp_var = try fs.path.join(alloc, &[_][]const u8{ ultralong_path, append_path });
alloc.free(ultralong_path);
ultralong_path = tmp_var;
}
std.debug.assert(fs.MAX_PATH_BYTES < ultralong_path.len);
//std.debug.print("ultralong_path: {s}\n", .{ultralong_path});
// TODO add final test here
// const canon_path1 = canonicalize(alloc, absolute_path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment