aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2020-07-15 17:40:39 +0200
committerJakub Konka <kubkon@jakubkonka.com>2020-07-22 08:51:22 +0200
commita8a02dfbfaf4b9bd303712c853e202bd07837371 (patch)
treed415077ae1c6aec8f70ad76f5bd3d575ec88106d /lib/std
parent30f1176a545111e8bdce3362d6bdbf7ef75dce2a (diff)
downloadzig-a8a02dfbfaf4b9bd303712c853e202bd07837371.tar.gz
zig-a8a02dfbfaf4b9bd303712c853e202bd07837371.zip
Add smoke test for dir symlinks
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/os/test.zig52
1 files changed, 28 insertions, 24 deletions
diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig
index 5bb1ef38db..98a2cb7fae 100644
--- a/lib/std/os/test.zig
+++ b/lib/std/os/test.zig
@@ -44,7 +44,7 @@ test "fstatat" {
test "readlink" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;
- // First, try relative paths
+ // First, try relative paths in cwd
{
var cwd = fs.cwd();
try cwd.writeFile("file.txt", "nonsense");
@@ -58,37 +58,41 @@ test "readlink" {
try cwd.deleteFile("symlinked");
}
- // Next, let's try fully-qualified paths
- {
- var tmp = tmpDir(.{});
- // defer tmp.cleanup();
-
- // create file
- try tmp.dir.writeFile("file.txt", "nonsense");
-
- // get paths
- // TODO: use Dir's realpath function once that exists
- var arena = ArenaAllocator.init(testing.allocator);
- defer arena.deinit();
-
- 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 target_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "file.txt" });
- const symlink_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "symlinked" });
+ // Now, let's use tempdir
+ var tmp = tmpDir(.{});
+ // defer tmp.cleanup();
+
+ // Create some targets
+ try tmp.dir.writeFile("file.txt", "nonsense");
+ try tmp.dir.makeDir("subdir");
+
+ // Get base abs path
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+
+ 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);
+ };
+
+ try testReadlink(&arena.allocator, base_path, "file.txt", "symlink1", false);
+ try testReadlink(&arena.allocator, base_path, "subdir", "symlink2", true);
+}
+
+fn testReadlink(allocator: *mem.Allocator, base_path: []const u8, target_name: []const u8, symlink_name: []const u8, is_dir: bool) !void {
+ const target_path = try fs.path.join(allocator, &[_][]const u8{ base_path, target_name });
+ const symlink_path = try fs.path.join(allocator, &[_][]const u8{ base_path, symlink_name });
std.debug.warn("\ntarget_path={}\n", .{target_path});
std.debug.warn("symlink_path={}\n", .{symlink_path});
- // create symbolic link by path
- try os.symlink(target_path, symlink_path, .{});
+ // Create symbolic link by path
+ try os.symlink(target_path, symlink_path, .{ .is_directory = is_dir });
- // now, read the link and verify
+ // Read the link and verify
var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
const given = try os.readlink(symlink_path, buffer[0..]);
std.debug.warn("given={}\n", .{given});
expect(mem.eql(u8, target_path, given));
- }
}
test "readlinkat" {