2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 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-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") {
|
2020-02-02 16:55:22 -05:00
|
|
|
assertEquals(file.mode!, Deno.statSync(`cli/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 {
|
2020-02-02 16:55:22 -05:00
|
|
|
const files = Deno.readDirSync("cli/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;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.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-10-31 22:33:27 -04:00
|
|
|
src = Deno.readDirSync("cli/tests/fixture.json");
|
2018-10-03 17:56:56 -04:00
|
|
|
} catch (err) {
|
|
|
|
caughtError = true;
|
2020-02-21 10:36:13 -05:00
|
|
|
assert(err instanceof Error);
|
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;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.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> {
|
2020-02-02 16:55:22 -05:00
|
|
|
const files = await Deno.readDir("cli/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;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|