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:
parent
ea2c7ac556
commit
330cd6b7ea
3 changed files with 43 additions and 4 deletions
4
cli/dts/lib.deno.unstable.d.ts
vendored
4
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -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>;
|
||||||
|
|
|
@ -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> {
|
||||||
|
|
|
@ -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),
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue