Skip to content

Commit

Permalink
test(fs): improve ensureSymlink() test (#5087)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Jun 20, 2024
1 parent 572a537 commit b75d42a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
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.
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);
},
);

0 comments on commit b75d42a

Please sign in to comment.