fix(cli): add file URL support for Deno.readLink (#8423)

This commit is contained in:
William Perron 2020-11-23 16:11:56 -05:00 committed by GitHub
parent 46850918e7
commit 266925d772
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 4 deletions

View file

@ -1491,7 +1491,7 @@ declare namespace Deno {
* Throws TypeError if called with a hard link
*
* Requires `allow-read` permission. */
export function readLinkSync(path: string): string;
export function readLinkSync(path: string | URL): string;
/** Resolves to the full path destination of the named symbolic link.
*
@ -1503,7 +1503,7 @@ declare namespace Deno {
* Throws TypeError if called with a hard link
*
* Requires `allow-read` permission. */
export function readLink(path: string): Promise<string>;
export function readLink(path: string | URL): Promise<string>;
/** Resolves to a `Deno.FileInfo` for the specified `path`. If `path` is a
* symlink, information for the symlink will be returned instead of what it

View file

@ -128,11 +128,11 @@
}
function readLinkSync(path) {
return core.jsonOpSync("op_read_link_sync", { path });
return core.jsonOpSync("op_read_link_sync", { path: pathFromURL(path) });
}
function readLink(path) {
return core.jsonOpAsync("op_read_link_async", { path });
return core.jsonOpAsync("op_read_link_async", { path: pathFromURL(path) });
}
function realPathSync(path) {

View file

@ -3,6 +3,7 @@ import {
assertEquals,
assertThrows,
assertThrowsAsync,
pathToAbsoluteFileUrl,
unitTest,
} from "./test_util.ts";
@ -21,6 +22,21 @@ unitTest(
},
);
unitTest(
{ perms: { write: true, read: true } },
function readLinkSyncUrlSuccess(): void {
const testDir = Deno.makeTempDirSync();
const target = testDir +
(Deno.build.os == "windows" ? "\\target" : "/target");
const symlink = testDir +
(Deno.build.os == "windows" ? "\\symlink" : "/symlink");
Deno.mkdirSync(target);
Deno.symlinkSync(target, symlink);
const targetPath = Deno.readLinkSync(pathToAbsoluteFileUrl(symlink));
assertEquals(targetPath, target);
},
);
unitTest({ perms: { read: false } }, function readLinkSyncPerm(): void {
assertThrows(() => {
Deno.readLinkSync("/symlink");
@ -48,6 +64,21 @@ unitTest(
},
);
unitTest(
{ perms: { write: true, read: true } },
async function readLinkUrlSuccess(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const target = testDir +
(Deno.build.os == "windows" ? "\\target" : "/target");
const symlink = testDir +
(Deno.build.os == "windows" ? "\\symlink" : "/symlink");
Deno.mkdirSync(target);
Deno.symlinkSync(target, symlink);
const targetPath = await Deno.readLink(pathToAbsoluteFileUrl(symlink));
assertEquals(targetPath, target);
},
);
unitTest({ perms: { read: false } }, async function readLinkPerm(): Promise<
void
> {