2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-24 18:57:08 -04:00
|
|
|
import {
|
|
|
|
unitTest,
|
|
|
|
assert,
|
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync,
|
|
|
|
} from "./test_util.ts";
|
2019-05-01 05:08:12 -04:00
|
|
|
|
|
|
|
// Allow 10 second difference.
|
|
|
|
// Note this might not be enough for FAT (but we are not testing on such fs).
|
2020-02-19 15:36:18 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-04-27 14:09:56 -04:00
|
|
|
function assertFuzzyTimestampEquals(t1: Date | null, t2: Date): void {
|
|
|
|
assert(t1 instanceof Date);
|
|
|
|
assert(Math.abs(t1.valueOf() - t2.valueOf()) < 10_000);
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function utimeSyncFileSuccess(): void {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const filename = testDir + "/file.txt";
|
|
|
|
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
|
2020-03-28 13:03:49 -04:00
|
|
|
mode: 0o666,
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const atime = 1000;
|
|
|
|
const mtime = 50000;
|
|
|
|
Deno.utimeSync(filename, atime, mtime);
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const fileInfo = Deno.statSync(filename);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(fileInfo.atime, new Date(atime * 1000));
|
|
|
|
assertFuzzyTimestampEquals(fileInfo.mtime, new Date(mtime * 1000));
|
2020-03-04 11:31:14 -05:00
|
|
|
}
|
|
|
|
);
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-05-01 05:08:12 -04:00
|
|
|
function utimeSyncDirectorySuccess(): void {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
|
|
|
|
const atime = 1000;
|
|
|
|
const mtime = 50000;
|
|
|
|
Deno.utimeSync(testDir, atime, mtime);
|
|
|
|
|
|
|
|
const dirInfo = Deno.statSync(testDir);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(dirInfo.atime, new Date(atime * 1000));
|
|
|
|
assertFuzzyTimestampEquals(dirInfo.mtime, new Date(mtime * 1000));
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function utimeSyncDateSuccess(): void {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2020-04-27 14:09:56 -04:00
|
|
|
const atime = new Date(1000_000);
|
|
|
|
const mtime = new Date(50000_000);
|
|
|
|
Deno.utimeSync(testDir, atime, mtime);
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const dirInfo = Deno.statSync(testDir);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(dirInfo.atime, atime);
|
|
|
|
assertFuzzyTimestampEquals(dirInfo.mtime, mtime);
|
2020-03-04 11:31:14 -05:00
|
|
|
}
|
|
|
|
);
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2020-04-03 15:20:40 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function utimeSyncFileDateSuccess() {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const filename = testDir + "/file.txt";
|
|
|
|
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
|
|
|
|
mode: 0o666,
|
|
|
|
});
|
|
|
|
const atime = new Date();
|
|
|
|
const mtime = new Date();
|
|
|
|
Deno.utimeSync(filename, atime, mtime);
|
|
|
|
|
|
|
|
const fileInfo = Deno.statSync(filename);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(fileInfo.atime, atime);
|
|
|
|
assertFuzzyTimestampEquals(fileInfo.mtime, mtime);
|
2020-04-03 15:20:40 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-05-01 05:08:12 -04:00
|
|
|
function utimeSyncLargeNumberSuccess(): void {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
|
|
|
|
// There are Rust side caps (might be fs relate),
|
|
|
|
// so JUST make them slightly larger than UINT32_MAX.
|
|
|
|
const atime = 0x100000001;
|
|
|
|
const mtime = 0x100000002;
|
|
|
|
Deno.utimeSync(testDir, atime, mtime);
|
|
|
|
|
|
|
|
const dirInfo = Deno.statSync(testDir);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(dirInfo.atime, new Date(atime * 1000));
|
|
|
|
assertFuzzyTimestampEquals(dirInfo.mtime, new Date(mtime * 1000));
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function utimeSyncNotFound(): void {
|
|
|
|
const atime = 1000;
|
|
|
|
const mtime = 50000;
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.utimeSync("/baddir", atime, mtime);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: false } },
|
|
|
|
function utimeSyncPerm(): void {
|
|
|
|
const atime = 1000;
|
|
|
|
const mtime = 50000;
|
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.utimeSync("/some_dir", atime, mtime);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-05-01 05:08:12 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-05-01 05:08:12 -04:00
|
|
|
async function utimeFileSuccess(): Promise<void> {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const filename = testDir + "/file.txt";
|
|
|
|
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
|
2020-03-28 13:03:49 -04:00
|
|
|
mode: 0o666,
|
2019-05-01 05:08:12 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const atime = 1000;
|
|
|
|
const mtime = 50000;
|
|
|
|
await Deno.utime(filename, atime, mtime);
|
|
|
|
|
|
|
|
const fileInfo = Deno.statSync(filename);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(fileInfo.atime, new Date(atime * 1000));
|
|
|
|
assertFuzzyTimestampEquals(fileInfo.mtime, new Date(mtime * 1000));
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-05-01 05:08:12 -04:00
|
|
|
async function utimeDirectorySuccess(): Promise<void> {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
|
|
|
|
const atime = 1000;
|
|
|
|
const mtime = 50000;
|
|
|
|
await Deno.utime(testDir, atime, mtime);
|
|
|
|
|
|
|
|
const dirInfo = Deno.statSync(testDir);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(dirInfo.atime, new Date(atime * 1000));
|
|
|
|
assertFuzzyTimestampEquals(dirInfo.mtime, new Date(mtime * 1000));
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
2019-05-01 05:08:12 -04:00
|
|
|
async function utimeDateSuccess(): Promise<void> {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
|
2020-04-27 14:09:56 -04:00
|
|
|
const atime = new Date(100_000);
|
|
|
|
const mtime = new Date(5000_000);
|
|
|
|
await Deno.utime(testDir, atime, mtime);
|
2019-05-01 05:08:12 -04:00
|
|
|
|
|
|
|
const dirInfo = Deno.statSync(testDir);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(dirInfo.atime, atime);
|
|
|
|
assertFuzzyTimestampEquals(dirInfo.mtime, mtime);
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-04-03 15:20:40 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function utimeFileDateSuccess(): Promise<void> {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const filename = testDir + "/file.txt";
|
|
|
|
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
|
|
|
|
mode: 0o666,
|
|
|
|
});
|
|
|
|
|
|
|
|
const atime = new Date();
|
|
|
|
const mtime = new Date();
|
|
|
|
await Deno.utime(filename, atime, mtime);
|
|
|
|
|
|
|
|
const fileInfo = Deno.statSync(filename);
|
2020-04-27 14:09:56 -04:00
|
|
|
assertFuzzyTimestampEquals(fileInfo.atime, atime);
|
|
|
|
assertFuzzyTimestampEquals(fileInfo.mtime, mtime);
|
2020-04-03 15:20:40 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function utimeNotFound(): Promise<void> {
|
|
|
|
const atime = 1000;
|
|
|
|
const mtime = 50000;
|
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.utime("/baddir", atime, mtime);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: false } },
|
|
|
|
async function utimeSyncPerm(): Promise<void> {
|
|
|
|
const atime = 1000;
|
|
|
|
const mtime = 50000;
|
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.utime("/some_dir", atime, mtime);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-05-01 05:08:12 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|