2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-06-11 12:36:20 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync,
|
2020-06-11 12:36:20 -04:00
|
|
|
pathToAbsoluteFileUrl,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2020-06-11 12:36:20 -04:00
|
|
|
} from "./test_util.ts";
|
2019-02-12 10:08:56 -05:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
function assertSameContent(files: Deno.DirEntry[]) {
|
2018-10-03 17:56:56 -04:00
|
|
|
let counter = 0;
|
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
for (const entry of files) {
|
|
|
|
if (entry.name === "subdir") {
|
|
|
|
assert(entry.isDirectory);
|
2018-10-03 17:56:56 -04:00
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
assertEquals(counter, 1);
|
2018-10-03 17:56:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, function readDirSyncSuccess() {
|
2020-04-29 16:39:37 -04:00
|
|
|
const files = [...Deno.readDirSync("cli/tests/")];
|
2018-10-03 17:56:56 -04:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, function readDirSyncWithUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const files = [...Deno.readDirSync(pathToAbsoluteFileUrl("cli/tests"))];
|
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: false } }, function readDirSyncPerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-04-29 16:39:37 -04:00
|
|
|
Deno.readDirSync("tests/");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, function readDirSyncNotDir() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
|
|
|
Deno.readDirSync("cli/tests/fixture.json");
|
|
|
|
}, Error);
|
2018-10-03 17:56:56 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, function readDirSyncNotFound() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
|
|
|
Deno.readDirSync("bad_dir_name");
|
|
|
|
}, Deno.errors.NotFound);
|
2018-10-03 17:56:56 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, async function readDirSuccess() {
|
2020-04-16 01:40:30 -04:00
|
|
|
const files = [];
|
2020-04-29 16:39:37 -04:00
|
|
|
for await (const dirEntry of Deno.readDir("cli/tests/")) {
|
2020-04-16 01:40:30 -04:00
|
|
|
files.push(dirEntry);
|
|
|
|
}
|
2018-10-03 17:56:56 -04:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, async function readDirWithUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const files = [];
|
2020-07-14 15:24:17 -04:00
|
|
|
for await (
|
|
|
|
const dirEntry of Deno.readDir(pathToAbsoluteFileUrl("cli/tests"))
|
|
|
|
) {
|
2020-06-11 12:36:20 -04:00
|
|
|
files.push(dirEntry);
|
|
|
|
}
|
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: false } }, async function readDirPerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-04-29 16:39:37 -04:00
|
|
|
await Deno.readDir("tests/")[Symbol.asyncIterator]().next();
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
});
|
2021-02-25 05:16:18 -05:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
async function readDirDevFd(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
for await (const _ of Deno.readDir("/dev/fd")) {
|
|
|
|
// We don't actually care whats in here; just that we don't panic on non regular entries
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true }, ignore: Deno.build.os == "windows" },
|
2021-08-05 07:08:58 -04:00
|
|
|
function readDirDevFdSync() {
|
2021-02-25 05:16:18 -05:00
|
|
|
for (const _ of Deno.readDirSync("/dev/fd")) {
|
|
|
|
// We don't actually care whats in here; just that we don't panic on non regular file entries
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|