2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { testPerm, assert, assertEquals } from "./test_util.ts";
|
2019-02-12 10:08:56 -05:00
|
|
|
|
|
|
|
type FileInfo = Deno.FileInfo;
|
2018-10-03 17:56:56 -04:00
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
const isWin = Deno.build.os === "win";
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function assertSameContent(files: FileInfo[]): void {
|
2018-10-03 17:56:56 -04:00
|
|
|
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") {
|
2019-05-08 19:20:30 -04:00
|
|
|
if (isWin) {
|
|
|
|
assert(file.path.endsWith(`tests\\${file.name}`));
|
|
|
|
} else {
|
|
|
|
assert(file.path.endsWith(`tests/${file.name}`));
|
|
|
|
}
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(file.mode!, Deno.statSync(`tests/${file.name}`).mode!);
|
2018-10-03 17:56:56 -04:00
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(counter, 2);
|
2018-10-03 17:56:56 -04:00
|
|
|
}
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true }, function readDirSyncSuccess(): void {
|
2019-02-12 10:08:56 -05:00
|
|
|
const files = Deno.readDirSync("tests/");
|
2018-10-03 17:56:56 -04:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: false }, function readDirSyncPerm(): void {
|
2019-02-08 15:59:38 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-03-09 12:30:38 -05:00
|
|
|
Deno.readDirSync("tests/");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(e.name, "PermissionDenied");
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true }, function readDirSyncNotDir(): void {
|
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-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.Other);
|
2018-10-03 17:56:56 -04:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(src, undefined);
|
2018-10-03 17:56:56 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true }, function readDirSyncNotFound(): void {
|
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-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.kind, Deno.ErrorKind.NotFound);
|
2018-10-03 17:56:56 -04:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(src, undefined);
|
2018-10-03 17:56:56 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true }, async function readDirSuccess(): Promise<void> {
|
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
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: false }, async function readDirPerm(): Promise<void> {
|
2019-02-08 15:59:38 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-03-09 12:30:38 -05:00
|
|
|
await Deno.readDir("tests/");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(e.name, "PermissionDenied");
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|