2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-02-12 21:05:10 -05:00
|
|
|
import { assertEquals, assertThrows, fail } from "@std/assert/mod.ts";
|
2023-03-19 12:10:39 -04:00
|
|
|
import { futimes, futimesSync } from "node:fs";
|
|
|
|
|
|
|
|
const randomDate = new Date(Date.now() + 1000);
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"ASYNC: change the file system timestamps of the object referenced by path",
|
|
|
|
async fn() {
|
2024-01-24 09:59:55 -05:00
|
|
|
const filePath = Deno.makeTempFileSync();
|
|
|
|
using file = await Deno.open(filePath, { create: true, write: true });
|
2023-03-19 12:10:39 -04:00
|
|
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
2024-01-24 09:59:55 -05:00
|
|
|
futimes(file.rid, randomDate, randomDate, (err: Error | null) => {
|
2023-03-19 12:10:39 -04:00
|
|
|
if (err !== null) reject();
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
() => {
|
2024-01-24 09:59:55 -05:00
|
|
|
const fileInfo: Deno.FileInfo = Deno.lstatSync(filePath);
|
2023-03-19 12:10:39 -04:00
|
|
|
assertEquals(fileInfo.mtime, randomDate);
|
|
|
|
assertEquals(fileInfo.atime, randomDate);
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
fail("No error expected");
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.finally(() => {
|
2024-01-24 09:59:55 -05:00
|
|
|
Deno.removeSync(filePath);
|
2023-03-19 12:10:39 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "ASYNC: should throw error if atime is infinity",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
futimes(123, Infinity, 0, (_err: Error | null) => {});
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"invalid atime, must not be infinity or NaN",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "ASYNC: should throw error if atime is NaN",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
futimes(123, "some string", 0, (_err: Error | null) => {});
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"invalid atime, must not be infinity or NaN",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"SYNC: change the file system timestamps of the object referenced by path",
|
|
|
|
fn() {
|
2024-01-24 09:59:55 -05:00
|
|
|
const filePath = Deno.makeTempFileSync();
|
|
|
|
using file = Deno.openSync(filePath, { create: true, write: true });
|
2023-03-19 12:10:39 -04:00
|
|
|
|
|
|
|
try {
|
2024-01-24 09:59:55 -05:00
|
|
|
futimesSync(file.rid, randomDate, randomDate);
|
2023-03-19 12:10:39 -04:00
|
|
|
|
2024-01-24 09:59:55 -05:00
|
|
|
const fileInfo: Deno.FileInfo = Deno.lstatSync(filePath);
|
2023-03-19 12:10:39 -04:00
|
|
|
|
|
|
|
assertEquals(fileInfo.mtime, randomDate);
|
|
|
|
assertEquals(fileInfo.atime, randomDate);
|
|
|
|
} finally {
|
2024-01-24 09:59:55 -05:00
|
|
|
Deno.removeSync(filePath);
|
2023-03-19 12:10:39 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "SYNC: should throw error if atime is NaN",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
futimesSync(123, "some string", 0);
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"invalid atime, must not be infinity or NaN",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "SYNC: should throw error if atime is Infinity",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
futimesSync(123, Infinity, 0);
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"invalid atime, must not be infinity or NaN",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|