0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

feat(cli): support URL overloads for Deno.utime and Deno.utimeSync (#10792)

This commit is contained in:
Casper Beyer 2021-06-01 02:05:57 +08:00 committed by GitHub
parent ea2c7ac556
commit 330cd6b7ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 4 deletions

View file

@ -766,7 +766,7 @@ declare namespace Deno {
* *
* Requires `allow-write` permission. */ * Requires `allow-write` permission. */
export function utimeSync( export function utimeSync(
path: string, path: string | URL,
atime: number | Date, atime: number | Date,
mtime: number | Date, mtime: number | Date,
): void; ): void;
@ -783,7 +783,7 @@ declare namespace Deno {
* *
* Requires `allow-write` permission. */ * Requires `allow-write` permission. */
export function utime( export function utime(
path: string, path: string | URL,
atime: number | Date, atime: number | Date,
mtime: number | Date, mtime: number | Date,
): Promise<void>; ): Promise<void>;

View file

@ -3,6 +3,7 @@ import {
assertEquals, assertEquals,
assertThrows, assertThrows,
assertThrowsAsync, assertThrowsAsync,
pathToAbsoluteFileUrl,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
@ -69,6 +70,25 @@ unitTest(
}, },
); );
unitTest(
{ perms: { read: true, write: true } },
function utimeSyncUrlSuccess(): void {
const testDir = Deno.makeTempDirSync();
const filename = testDir + "/file.txt";
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
mode: 0o666,
});
const atime = 1000;
const mtime = 50000;
Deno.utimeSync(pathToAbsoluteFileUrl(filename), atime, mtime);
const fileInfo = Deno.statSync(filename);
assertEquals(fileInfo.atime, new Date(atime * 1000));
assertEquals(fileInfo.mtime, new Date(mtime * 1000));
},
);
unitTest( unitTest(
{ perms: { read: true, write: true } }, { perms: { read: true, write: true } },
function utimeSyncDirectorySuccess(): void { function utimeSyncDirectorySuccess(): void {
@ -177,6 +197,25 @@ unitTest(
}, },
); );
unitTest(
{ perms: { read: true, write: true } },
async function utimeUrlSuccess(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const filename = testDir + "/file.txt";
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
mode: 0o666,
});
const atime = 1000;
const mtime = 50000;
await Deno.utime(pathToAbsoluteFileUrl(filename), atime, mtime);
const fileInfo = Deno.statSync(filename);
assertEquals(fileInfo.atime, new Date(atime * 1000));
assertEquals(fileInfo.mtime, new Date(mtime * 1000));
},
);
unitTest( unitTest(
{ perms: { read: true, write: true } }, { perms: { read: true, write: true } },
async function utimeDirectorySuccess(): Promise<void> { async function utimeDirectorySuccess(): Promise<void> {

View file

@ -315,7 +315,7 @@
mtime, mtime,
) { ) {
core.opSync("op_utime_sync", { core.opSync("op_utime_sync", {
path, path: pathFromURL(path),
atime: toUnixTimeFromEpoch(atime), atime: toUnixTimeFromEpoch(atime),
mtime: toUnixTimeFromEpoch(mtime), mtime: toUnixTimeFromEpoch(mtime),
}); });
@ -327,7 +327,7 @@
mtime, mtime,
) { ) {
await core.opAsync("op_utime_async", { await core.opAsync("op_utime_async", {
path, path: pathFromURL(path),
atime: toUnixTimeFromEpoch(atime), atime: toUnixTimeFromEpoch(atime),
mtime: toUnixTimeFromEpoch(mtime), mtime: toUnixTimeFromEpoch(mtime),
}); });