2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-02-08 15:59:38 -05:00
|
|
|
import { testPerm, assert, assertEqual } from "./test_util.ts";
|
2019-02-12 10:08:56 -05:00
|
|
|
|
|
|
|
type FileInfo = Deno.FileInfo;
|
2018-10-03 17:56:56 -04:00
|
|
|
|
|
|
|
function assertSameContent(files: FileInfo[]) {
|
|
|
|
let counter = 0;
|
|
|
|
|
|
|
|
for (const file of files) {
|
2018-10-05 07:29:55 -04:00
|
|
|
if (file.name === "subdir") {
|
2018-10-03 17:56:56 -04:00
|
|
|
assert(file.isDirectory());
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.name === "002_hello.ts") {
|
|
|
|
assertEqual(file.path, `tests/${file.name}`);
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(file.mode!, Deno.statSync(`tests/${file.name}`).mode!);
|
2018-10-03 17:56:56 -04:00
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEqual(counter, 2);
|
|
|
|
}
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true }, function readDirSyncSuccess() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const files = Deno.readDirSync("tests/");
|
2018-10-03 17:56:56 -04:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: false }, function readDirSyncPerm() {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
const files = Deno.readDirSync("tests/");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
assertEqual(e.name, "PermissionDenied");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ read: true }, function readDirSyncNotDir() {
|
2018-10-03 17:56:56 -04:00
|
|
|
let caughtError = false;
|
|
|
|
let src;
|
|
|
|
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
src = Deno.readDirSync("package.json");
|
2018-10-03 17:56:56 -04:00
|
|
|
} catch (err) {
|
|
|
|
caughtError = true;
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.Other);
|
2018-10-03 17:56:56 -04:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
assertEqual(src, undefined);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true }, function readDirSyncNotFound() {
|
2018-10-03 17:56:56 -04:00
|
|
|
let caughtError = false;
|
|
|
|
let src;
|
|
|
|
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
src = Deno.readDirSync("bad_dir_name");
|
2018-10-03 17:56:56 -04:00
|
|
|
} catch (err) {
|
|
|
|
caughtError = true;
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-10-03 17:56:56 -04:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
assertEqual(src, undefined);
|
|
|
|
});
|
|
|
|
|
2019-02-08 15:59:38 -05:00
|
|
|
testPerm({ read: true }, async function readDirSuccess() {
|
2019-02-12 10:08:56 -05:00
|
|
|
const files = await Deno.readDir("tests/");
|
2018-10-03 17:56:56 -04:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
2019-02-08 15:59:38 -05:00
|
|
|
|
|
|
|
testPerm({ read: false }, async function readDirPerm() {
|
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
const files = await Deno.readDir("tests/");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
assertEqual(e.name, "PermissionDenied");
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|