0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/js/stat_test.ts

173 lines
4.5 KiB
TypeScript
Raw Normal View History

2019-01-21 14:03:30 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assert, assertEqual } from "./test_util.ts";
2018-09-11 15:38:53 -04:00
// TODO Add tests for modified, accessed, and created fields once there is a way
// to create temp files.
testPerm({ read: true }, async function statSyncSuccess() {
const packageInfo = Deno.statSync("package.json");
2018-09-11 15:38:53 -04:00
assert(packageInfo.isFile());
assert(!packageInfo.isSymlink());
const testingInfo = Deno.statSync("testing");
2018-09-11 15:38:53 -04:00
assert(testingInfo.isDirectory());
assert(!testingInfo.isSymlink());
const srcInfo = Deno.statSync("src");
2018-09-11 15:38:53 -04:00
assert(srcInfo.isDirectory());
assert(!srcInfo.isSymlink());
});
testPerm({ read: false }, async function statSyncPerm() {
let caughtError = false;
try {
Deno.statSync("package.json");
} catch (e) {
caughtError = true;
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ read: true }, async function statSyncNotFound() {
2018-09-11 15:38:53 -04:00
let caughtError = false;
let badInfo;
try {
badInfo = Deno.statSync("bad_file_name");
2018-09-11 15:38:53 -04:00
} catch (err) {
caughtError = true;
assertEqual(err.kind, Deno.ErrorKind.NotFound);
2018-09-11 15:38:53 -04:00
assertEqual(err.name, "NotFound");
}
assert(caughtError);
assertEqual(badInfo, undefined);
});
testPerm({ read: true }, async function lstatSyncSuccess() {
const packageInfo = Deno.lstatSync("package.json");
2018-09-11 15:38:53 -04:00
assert(packageInfo.isFile());
assert(!packageInfo.isSymlink());
const testingInfo = Deno.lstatSync("testing");
2018-09-11 15:38:53 -04:00
assert(!testingInfo.isDirectory());
assert(testingInfo.isSymlink());
const srcInfo = Deno.lstatSync("src");
2018-09-11 15:38:53 -04:00
assert(srcInfo.isDirectory());
assert(!srcInfo.isSymlink());
});
testPerm({ read: false }, async function lstatSyncPerm() {
let caughtError = false;
try {
Deno.lstatSync("package.json");
} catch (e) {
caughtError = true;
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ read: true }, async function lstatSyncNotFound() {
2018-09-11 15:38:53 -04:00
let caughtError = false;
let badInfo;
try {
badInfo = Deno.lstatSync("bad_file_name");
2018-09-11 15:38:53 -04:00
} catch (err) {
caughtError = true;
assertEqual(err.kind, Deno.ErrorKind.NotFound);
2018-09-11 15:38:53 -04:00
assertEqual(err.name, "NotFound");
}
assert(caughtError);
assertEqual(badInfo, undefined);
});
testPerm({ read: true }, async function statSuccess() {
const packageInfo = await Deno.stat("package.json");
2018-09-11 15:38:53 -04:00
assert(packageInfo.isFile());
assert(!packageInfo.isSymlink());
const testingInfo = await Deno.stat("testing");
2018-09-11 15:38:53 -04:00
assert(testingInfo.isDirectory());
assert(!testingInfo.isSymlink());
const srcInfo = await Deno.stat("src");
2018-09-11 15:38:53 -04:00
assert(srcInfo.isDirectory());
assert(!srcInfo.isSymlink());
});
testPerm({ read: false }, async function statPerm() {
let caughtError = false;
try {
await Deno.stat("package.json");
} catch (e) {
caughtError = true;
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ read: true }, async function statNotFound() {
2018-09-11 15:38:53 -04:00
let caughtError = false;
let badInfo;
try {
badInfo = await Deno.stat("bad_file_name");
2018-09-11 15:38:53 -04:00
} catch (err) {
caughtError = true;
assertEqual(err.kind, Deno.ErrorKind.NotFound);
2018-09-11 15:38:53 -04:00
assertEqual(err.name, "NotFound");
}
assert(caughtError);
assertEqual(badInfo, undefined);
});
testPerm({ read: true }, async function lstatSuccess() {
const packageInfo = await Deno.lstat("package.json");
2018-09-11 15:38:53 -04:00
assert(packageInfo.isFile());
assert(!packageInfo.isSymlink());
const testingInfo = await Deno.lstat("testing");
2018-09-11 15:38:53 -04:00
assert(!testingInfo.isDirectory());
assert(testingInfo.isSymlink());
const srcInfo = await Deno.lstat("src");
2018-09-11 15:38:53 -04:00
assert(srcInfo.isDirectory());
assert(!srcInfo.isSymlink());
});
testPerm({ read: false }, async function lstatPerm() {
let caughtError = false;
try {
await Deno.lstat("package.json");
} catch (e) {
caughtError = true;
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ read: true }, async function lstatNotFound() {
2018-09-11 15:38:53 -04:00
let caughtError = false;
let badInfo;
try {
badInfo = await Deno.lstat("bad_file_name");
2018-09-11 15:38:53 -04:00
} catch (err) {
caughtError = true;
assertEqual(err.kind, Deno.ErrorKind.NotFound);
2018-09-11 15:38:53 -04:00
assertEqual(err.name, "NotFound");
}
assert(caughtError);
assertEqual(badInfo, undefined);
});