From ece56d9935614dcc755aeeefd3cd6c84b6827f77 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Thu, 3 Jun 2021 22:16:00 +0800 Subject: [PATCH] feat(runtime): support URL overloads for `Deno.symlink` and `Deno.symlinkSync` (#10664) --- cli/dts/lib.deno.ns.d.ts | 8 +++---- cli/tests/unit/symlink_test.ts | 43 +++++++++++++++++++++++++++++++++- runtime/js/30_fs.js | 12 ++++++++-- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index 34f8c2ad51..3db07ba657 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -2379,8 +2379,8 @@ declare namespace Deno { * * Requires `allow-write` permission. */ export function symlinkSync( - oldpath: string, - newpath: string, + oldpath: string | URL, + newpath: string | URL, options?: SymlinkOptions, ): void; @@ -2396,8 +2396,8 @@ declare namespace Deno { * * Requires `allow-write` permission. */ export function symlink( - oldpath: string, - newpath: string, + oldpath: string | URL, + newpath: string | URL, options?: SymlinkOptions, ): Promise; diff --git a/cli/tests/unit/symlink_test.ts b/cli/tests/unit/symlink_test.ts index 19e83660bb..eef993d17b 100644 --- a/cli/tests/unit/symlink_test.ts +++ b/cli/tests/unit/symlink_test.ts @@ -1,5 +1,10 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { assert, assertThrows, unitTest } from "./test_util.ts"; +import { + assert, + assertThrows, + pathToAbsoluteFileUrl, + unitTest, +} from "./test_util.ts"; unitTest( { perms: { read: true, write: true } }, @@ -16,6 +21,24 @@ unitTest( }, ); +unitTest( + { perms: { read: true, write: true } }, + function symlinkSyncURL(): void { + const testDir = Deno.makeTempDirSync(); + const oldname = testDir + "/oldname"; + const newname = testDir + "/newname"; + Deno.mkdirSync(oldname); + Deno.symlinkSync( + pathToAbsoluteFileUrl(oldname), + pathToAbsoluteFileUrl(newname), + ); + const newNameInfoLStat = Deno.lstatSync(newname); + const newNameInfoStat = Deno.statSync(newname); + assert(newNameInfoLStat.isSymlink); + assert(newNameInfoStat.isDirectory); + }, +); + unitTest(function symlinkSyncPerm(): void { assertThrows(() => { Deno.symlinkSync("oldbaddir", "newbaddir"); @@ -36,3 +59,21 @@ unitTest( assert(newNameInfoStat.isDirectory, "NOT DIRECTORY"); }, ); + +unitTest( + { perms: { read: true, write: true } }, + async function symlinkURL(): Promise { + const testDir = Deno.makeTempDirSync(); + const oldname = testDir + "/oldname"; + const newname = testDir + "/newname"; + Deno.mkdirSync(oldname); + await Deno.symlink( + pathToAbsoluteFileUrl(oldname), + pathToAbsoluteFileUrl(newname), + ); + const newNameInfoLStat = Deno.lstatSync(newname); + const newNameInfoStat = Deno.statSync(newname); + assert(newNameInfoLStat.isSymlink, "NOT SYMLINK"); + assert(newNameInfoStat.isDirectory, "NOT DIRECTORY"); + }, +); diff --git a/runtime/js/30_fs.js b/runtime/js/30_fs.js index c9daddacb6..84e1c68195 100644 --- a/runtime/js/30_fs.js +++ b/runtime/js/30_fs.js @@ -344,7 +344,11 @@ newpath, options, ) { - core.opSync("op_symlink_sync", { oldpath, newpath, options }); + core.opSync("op_symlink_sync", { + oldpath: pathFromURL(oldpath), + newpath: pathFromURL(newpath), + options, + }); } async function symlink( @@ -352,7 +356,11 @@ newpath, options, ) { - await core.opAsync("op_symlink_async", { oldpath, newpath, options }); + await core.opAsync("op_symlink_async", { + oldpath: pathFromURL(oldpath), + newpath: pathFromURL(newpath), + options, + }); } function fdatasyncSync(rid) {