Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(fs): improve ensureSymlink() test #5087

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions fs/ensure_symlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { dirname } from "@std/path/dirname";
import { resolve } from "@std/path/resolve";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { getFileInfoType } from "./_get_file_info_type.ts";
import { getFileInfoType, type PathType } from "./_get_file_info_type.ts";
import { toPathString } from "./_to_path_string.ts";

const isWindows = Deno.build.os === "windows";
Expand All @@ -16,6 +16,12 @@ function resolveSymlinkTarget(target: string | URL, linkName: string | URL) {
}
}

function getSymlinkOption(
type: PathType | undefined,
): Deno.SymlinkOptions | undefined {
return isWindows ? { type: type === "dir" ? "dir" : "file" } : undefined;
}

/**
* Asynchronously ensures that the link exists, and points to a valid file.
*
Expand Down Expand Up @@ -51,11 +57,7 @@ export async function ensureSymlink(

await ensureDir(dirname(toPathString(linkName)));

const options: Deno.SymlinkOptions | undefined = isWindows
? {
type: srcFilePathType === "dir" ? "dir" : "file",
}
: undefined;
const options = getSymlinkOption(srcFilePathType);

try {
await Deno.symlink(target, linkName, options);
Expand Down Expand Up @@ -114,11 +116,7 @@ export function ensureSymlinkSync(

ensureDirSync(dirname(toPathString(linkName)));

const options: Deno.SymlinkOptions | undefined = isWindows
? {
type: srcFilePathType === "dir" ? "dir" : "file",
}
: undefined;
const options = getSymlinkOption(srcFilePathType);

try {
Deno.symlinkSync(target, linkName, options);
Expand Down
70 changes: 69 additions & 1 deletion fs/ensure_symlink_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// TODO(axetroy): Add test for Windows once symlink is implemented for Windows.
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
import * as path from "@std/path";
import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";

Expand Down Expand Up @@ -237,3 +237,71 @@ Deno.test("ensureSymlinkSync() creates symlink with relative target", function (

Deno.removeSync(testDir, { recursive: true });
});

Deno.test("ensureSymlink() works with URLs", {
// TODO(kt3k): The 2nd test case doesn't pass on Windows. Fix it.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Able to create an issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #5092

ignore: Deno.build.os === "windows",
}, async () => {
const testDir = path.join(testdataDir, "link_file_with_url");
const testFile = path.join(testDir, "test.txt");
const linkFile = path.join(testDir, "link.txt");
{
try {
await Deno.mkdir(testDir, { recursive: true });
await Deno.writeFile(testFile, new Uint8Array());

await ensureSymlink(path.toFileUrl(testFile), path.toFileUrl(linkFile));

const srcStat = await Deno.lstat(testFile);
const linkStat = await Deno.lstat(linkFile);

assert(srcStat.isFile);
assert(linkStat.isSymlink);
} finally {
await Deno.remove(testDir, { recursive: true });
}
}

{
try {
await Deno.mkdir(testDir, { recursive: true });
await Deno.writeFile(testFile, new Uint8Array());

await ensureSymlink(testFile, path.toFileUrl(linkFile));

const srcStat = await Deno.lstat(testFile);
const linkStat = await Deno.lstat(linkFile);

assert(srcStat.isFile);
assert(linkStat.isSymlink);
} finally {
await Deno.remove(testDir, { recursive: true });
}
}
});

Deno.test(
"ensureSymlink() rejects with permission error if it doesn't have write permission",
{ permissions: { read: true } },
async () => {
const testFile = path.join(testdataDir, "0.ts");
const linkFile = path.join(testdataDir, "link.ts");

await assertRejects(async () => {
await ensureSymlink(testFile, linkFile);
}, Deno.errors.PermissionDenied);
},
);

Deno.test(
"ensureSymlinkSync() throws permission error if it doesn't have write permission",
{ permissions: { read: true } },
() => {
const testFile = path.join(testdataDir, "0.ts");
const linkFile = path.join(testdataDir, "link.ts");

assertThrows(() => {
ensureSymlinkSync(testFile, linkFile);
}, Deno.errors.PermissionDenied);
},
);