2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-04 11:31:14 -05:00
|
|
|
import { unitTest, assert, assertEquals } from "./test_util.ts";
|
2018-09-11 15:38:53 -04:00
|
|
|
|
2020-04-27 14:09:56 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function statSyncSuccess(): void {
|
|
|
|
const packageInfo = Deno.statSync("README.md");
|
|
|
|
assert(packageInfo.isFile);
|
|
|
|
assert(!packageInfo.isSymlink);
|
|
|
|
|
|
|
|
const modulesInfo = Deno.statSync("cli/tests/symlink_to_subdir");
|
|
|
|
assert(modulesInfo.isDirectory);
|
|
|
|
assert(!modulesInfo.isSymlink);
|
|
|
|
|
|
|
|
const testsInfo = Deno.statSync("cli/tests");
|
|
|
|
assert(testsInfo.isDirectory);
|
|
|
|
assert(!testsInfo.isSymlink);
|
|
|
|
|
|
|
|
const tempFile = Deno.makeTempFileSync();
|
|
|
|
const tempInfo = Deno.statSync(tempFile);
|
|
|
|
const now = Date.now();
|
|
|
|
assert(tempInfo.atime !== null && now - tempInfo.atime.valueOf() < 1000);
|
|
|
|
assert(tempInfo.mtime !== null && now - tempInfo.mtime.valueOf() < 1000);
|
|
|
|
assert(
|
|
|
|
tempInfo.birthtime === null || now - tempInfo.birthtime.valueOf() < 1000
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2018-09-11 15:38:53 -04:00
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
unitTest({ perms: { read: false } }, function statSyncPerm(): void {
|
2019-02-08 15:59:38 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-10-31 22:33:27 -04:00
|
|
|
Deno.statSync("README.md");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
unitTest({ perms: { read: true } }, function statSyncNotFound(): void {
|
2018-09-11 15:38:53 -04:00
|
|
|
let caughtError = false;
|
|
|
|
let badInfo;
|
|
|
|
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
badInfo = Deno.statSync("bad_file_name");
|
2018-09-11 15:38:53 -04:00
|
|
|
} catch (err) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-11 15:38:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(caughtError);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(badInfo, undefined);
|
2018-09-11 15:38:53 -04:00
|
|
|
});
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
unitTest({ perms: { read: true } }, function lstatSyncSuccess(): void {
|
2019-10-31 22:33:27 -04:00
|
|
|
const packageInfo = Deno.lstatSync("README.md");
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(packageInfo.isFile);
|
|
|
|
assert(!packageInfo.isSymlink);
|
2018-09-11 15:38:53 -04:00
|
|
|
|
2019-10-31 22:33:27 -04:00
|
|
|
const modulesInfo = Deno.lstatSync("cli/tests/symlink_to_subdir");
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(!modulesInfo.isDirectory);
|
|
|
|
assert(modulesInfo.isSymlink);
|
2018-09-11 15:38:53 -04:00
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
const coreInfo = Deno.lstatSync("core");
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(coreInfo.isDirectory);
|
|
|
|
assert(!coreInfo.isSymlink);
|
2018-09-11 15:38:53 -04:00
|
|
|
});
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
unitTest({ perms: { read: false } }, function lstatSyncPerm(): void {
|
2019-02-08 15:59:38 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-10-31 22:33:27 -04:00
|
|
|
Deno.lstatSync("README.md");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
unitTest({ perms: { read: true } }, function lstatSyncNotFound(): void {
|
2018-09-11 15:38:53 -04:00
|
|
|
let caughtError = false;
|
|
|
|
let badInfo;
|
|
|
|
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
badInfo = Deno.lstatSync("bad_file_name");
|
2018-09-11 15:38:53 -04:00
|
|
|
} catch (err) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-11 15:38:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(caughtError);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(badInfo, undefined);
|
2018-09-11 15:38:53 -04:00
|
|
|
});
|
|
|
|
|
2020-04-27 14:09:56 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function statSuccess(): Promise<void> {
|
|
|
|
const packageInfo = await Deno.stat("README.md");
|
|
|
|
assert(packageInfo.isFile);
|
|
|
|
assert(!packageInfo.isSymlink);
|
|
|
|
|
|
|
|
const modulesInfo = await Deno.stat("cli/tests/symlink_to_subdir");
|
|
|
|
assert(modulesInfo.isDirectory);
|
|
|
|
assert(!modulesInfo.isSymlink);
|
|
|
|
|
|
|
|
const testsInfo = await Deno.stat("cli/tests");
|
|
|
|
assert(testsInfo.isDirectory);
|
|
|
|
assert(!testsInfo.isSymlink);
|
|
|
|
|
|
|
|
const tempFile = await Deno.makeTempFile();
|
|
|
|
const tempInfo = await Deno.stat(tempFile);
|
|
|
|
const now = Date.now();
|
|
|
|
assert(tempInfo.atime !== null && now - tempInfo.atime.valueOf() < 1000);
|
|
|
|
assert(tempInfo.mtime !== null && now - tempInfo.mtime.valueOf() < 1000);
|
|
|
|
|
|
|
|
assert(
|
|
|
|
tempInfo.birthtime === null || now - tempInfo.birthtime.valueOf() < 1000
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2018-09-11 15:38:53 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: false } }, async function statPerm(): Promise<void> {
|
2019-02-08 15:59:38 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-10-31 22:33:27 -04:00
|
|
|
await Deno.stat("README.md");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: true } }, async function statNotFound(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-09-11 15:38:53 -04:00
|
|
|
let caughtError = false;
|
|
|
|
let badInfo;
|
|
|
|
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
badInfo = await Deno.stat("bad_file_name");
|
2018-09-11 15:38:53 -04:00
|
|
|
} catch (err) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-11 15:38:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(caughtError);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(badInfo, undefined);
|
2018-09-11 15:38:53 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: true } }, async function lstatSuccess(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const packageInfo = await Deno.lstat("README.md");
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(packageInfo.isFile);
|
|
|
|
assert(!packageInfo.isSymlink);
|
2018-09-11 15:38:53 -04:00
|
|
|
|
2019-10-31 22:33:27 -04:00
|
|
|
const modulesInfo = await Deno.lstat("cli/tests/symlink_to_subdir");
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(!modulesInfo.isDirectory);
|
|
|
|
assert(modulesInfo.isSymlink);
|
2018-09-11 15:38:53 -04:00
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
const coreInfo = await Deno.lstat("core");
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(coreInfo.isDirectory);
|
|
|
|
assert(!coreInfo.isSymlink);
|
2018-09-11 15:38:53 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: false } }, async function lstatPerm(): Promise<void> {
|
2019-02-08 15:59:38 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-10-31 22:33:27 -04:00
|
|
|
await Deno.lstat("README.md");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: true } }, async function lstatNotFound(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-09-11 15:38:53 -04:00
|
|
|
let caughtError = false;
|
|
|
|
let badInfo;
|
|
|
|
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
badInfo = await Deno.lstat("bad_file_name");
|
2018-09-11 15:38:53 -04:00
|
|
|
} catch (err) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-11 15:38:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(caughtError);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(badInfo, undefined);
|
2018-09-11 15:38:53 -04:00
|
|
|
});
|
2020-01-16 09:46:32 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os !== "windows", perms: { read: true, write: true } },
|
2020-03-20 09:38:34 -04:00
|
|
|
function statNoUnixFields(): void {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2020-03-04 11:31:14 -05:00
|
|
|
const s = Deno.statSync(filename);
|
|
|
|
assert(s.dev === null);
|
|
|
|
assert(s.ino === null);
|
|
|
|
assert(s.mode === null);
|
|
|
|
assert(s.nlink === null);
|
|
|
|
assert(s.uid === null);
|
|
|
|
assert(s.gid === null);
|
|
|
|
assert(s.rdev === null);
|
|
|
|
assert(s.blksize === null);
|
|
|
|
assert(s.blocks === null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
|
2020-03-20 09:38:34 -04:00
|
|
|
function statUnixFields(): void {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filename = tempDir + "/test.txt";
|
|
|
|
const filename2 = tempDir + "/test2.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2020-03-04 11:31:14 -05:00
|
|
|
// Create a link
|
|
|
|
Deno.linkSync(filename, filename2);
|
|
|
|
const s = Deno.statSync(filename);
|
|
|
|
assert(s.dev !== null);
|
|
|
|
assert(s.ino !== null);
|
|
|
|
assertEquals(s.mode! & 0o666, 0o666);
|
|
|
|
assertEquals(s.nlink, 2);
|
|
|
|
assert(s.uid !== null);
|
|
|
|
assert(s.gid !== null);
|
|
|
|
assert(s.rdev !== null);
|
|
|
|
assert(s.blksize !== null);
|
|
|
|
assert(s.blocks !== null);
|
|
|
|
}
|
|
|
|
);
|