1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 08:33:43 -05:00

feat(runtime): support URL overloads for Deno.rename/Deno.renameSync (#10512)

This commit is contained in:
Casper Beyer 2021-06-03 22:14:37 +08:00 committed by GitHub
parent 844a1317ec
commit dc69b03339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 5 deletions

View file

@ -1322,7 +1322,10 @@ declare namespace Deno {
* they are. It's always an error to rename anything to a non-empty directory.
*
* Requires `allow-read` and `allow-write` permissions. */
export function renameSync(oldpath: string, newpath: string): void;
export function renameSync(
oldpath: string | URL,
newpath: string | URL,
): void;
/** Renames (moves) `oldpath` to `newpath`. Paths may be files or directories.
* If `newpath` already exists and is not a directory, `rename()` replaces it.
@ -1339,7 +1342,10 @@ declare namespace Deno {
* they are. It's always an error to rename anything to a non-empty directory.
*
* Requires `allow-read` and `allow-write` permission. */
export function rename(oldpath: string, newpath: string): Promise<void>;
export function rename(
oldpath: string | URL,
newpath: string | URL,
): Promise<void>;
/** Synchronously reads and returns the entire contents of a file as utf8
* encoded string. Reading a directory throws an error.

View file

@ -1,5 +1,11 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows, unitTest } from "./test_util.ts";
import {
assert,
assertEquals,
assertThrows,
pathToAbsoluteFileUrl,
unitTest,
} from "./test_util.ts";
function assertMissing(path: string): void {
let caughtErr = false;
@ -40,6 +46,22 @@ unitTest(
},
);
unitTest(
{ perms: { read: true, write: true } },
function renameSyncWithURL(): void {
const testDir = Deno.makeTempDirSync();
const oldpath = testDir + "/oldpath";
const newpath = testDir + "/newpath";
Deno.mkdirSync(oldpath);
Deno.renameSync(
pathToAbsoluteFileUrl(oldpath),
pathToAbsoluteFileUrl(newpath),
);
assertDirectory(newpath);
assertMissing(oldpath);
},
);
unitTest(
{ perms: { read: false, write: true } },
function renameSyncReadPerm(): void {
@ -75,6 +97,22 @@ unitTest(
},
);
unitTest(
{ perms: { read: true, write: true } },
async function renameWithURL(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const oldpath = testDir + "/oldpath";
const newpath = testDir + "/newpath";
Deno.mkdirSync(oldpath);
await Deno.rename(
pathToAbsoluteFileUrl(oldpath),
pathToAbsoluteFileUrl(newpath),
);
assertDirectory(newpath);
assertMissing(oldpath);
},
);
function readFileString(filename: string): string {
const dataRead = Deno.readFileSync(filename);
const dec = new TextDecoder("utf-8");

View file

@ -156,11 +156,17 @@
}
function renameSync(oldpath, newpath) {
core.opSync("op_rename_sync", { oldpath, newpath });
core.opSync("op_rename_sync", {
oldpath: pathFromURL(oldpath),
newpath: pathFromURL(newpath),
});
}
async function rename(oldpath, newpath) {
await core.opAsync("op_rename_async", { oldpath, newpath });
await core.opAsync("op_rename_async", {
oldpath: pathFromURL(oldpath),
newpath: pathFromURL(newpath),
});
}
function parseFileInfo(response) {