2021-01-12 02:13:41 +09:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-06-12 02:36:20 +10:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-22 21:21:11 +08:00
|
|
|
assertRejects,
|
2020-06-25 06:57:08 +08:00
|
|
|
assertThrows,
|
2020-06-12 02:36:20 +10:00
|
|
|
pathToAbsoluteFileUrl,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2020-06-12 02:36:20 +10:00
|
|
|
} from "./test_util.ts";
|
2019-02-13 02:08:56 +11:00
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
function assertSameContent(files: Deno.DirEntry[]) {
|
2018-10-04 06:56:56 +09:00
|
|
|
let counter = 0;
|
|
|
|
|
2020-04-29 22:00:31 +02:00
|
|
|
for (const entry of files) {
|
|
|
|
if (entry.name === "subdir") {
|
|
|
|
assert(entry.isDirectory);
|
2018-10-04 06:56:56 +09:00
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 22:00:31 +02:00
|
|
|
assertEquals(counter, 1);
|
2018-10-04 06:56:56 +09:00
|
|
|
}
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
unitTest({ permissions: { read: true } }, function readDirSyncSuccess() {
|
2021-08-11 10:20:47 -04:00
|
|
|
const files = [...Deno.readDirSync("cli/tests/testdata")];
|
2018-10-04 06:56:56 +09:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
unitTest({ permissions: { read: true } }, function readDirSyncWithUrl() {
|
2021-08-11 10:20:47 -04:00
|
|
|
const files = [
|
|
|
|
...Deno.readDirSync(pathToAbsoluteFileUrl("cli/tests/testdata")),
|
|
|
|
];
|
2020-06-12 02:36:20 +10:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
unitTest({ permissions: { read: false } }, function readDirSyncPerm() {
|
2020-06-25 06:57:08 +08:00
|
|
|
assertThrows(() => {
|
2020-04-29 16:39:37 -04:00
|
|
|
Deno.readDirSync("tests/");
|
2020-06-25 06:57:08 +08:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-02-08 23:59:38 +03:00
|
|
|
});
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
unitTest({ permissions: { read: true } }, function readDirSyncNotDir() {
|
2021-10-11 21:21:18 +08:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.readDirSync("cli/tests/testdata/fixture.json");
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
`readdir 'cli/tests/testdata/fixture.json'`,
|
|
|
|
);
|
2018-10-04 06:56:56 +09:00
|
|
|
});
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
unitTest({ permissions: { read: true } }, function readDirSyncNotFound() {
|
2021-10-11 21:21:18 +08:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.readDirSync("bad_dir_name");
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`readdir 'bad_dir_name'`,
|
|
|
|
);
|
2018-10-04 06:56:56 +09:00
|
|
|
});
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
unitTest({ permissions: { read: true } }, async function readDirSuccess() {
|
2020-04-16 06:40:30 +01:00
|
|
|
const files = [];
|
2021-08-11 10:20:47 -04:00
|
|
|
for await (const dirEntry of Deno.readDir("cli/tests/testdata")) {
|
2020-04-16 06:40:30 +01:00
|
|
|
files.push(dirEntry);
|
|
|
|
}
|
2018-10-04 06:56:56 +09:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
2019-02-08 23:59:38 +03:00
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
unitTest({ permissions: { read: true } }, async function readDirWithUrl() {
|
2020-06-12 02:36:20 +10:00
|
|
|
const files = [];
|
2020-07-14 15:24:17 -04:00
|
|
|
for await (
|
2021-08-11 10:20:47 -04:00
|
|
|
const dirEntry of Deno.readDir(pathToAbsoluteFileUrl("cli/tests/testdata"))
|
2020-07-14 15:24:17 -04:00
|
|
|
) {
|
2020-06-12 02:36:20 +10:00
|
|
|
files.push(dirEntry);
|
|
|
|
}
|
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
unitTest({ permissions: { read: false } }, async function readDirPerm() {
|
2021-09-22 21:21:11 +08:00
|
|
|
await assertRejects(async () => {
|
2020-04-29 16:39:37 -04:00
|
|
|
await Deno.readDir("tests/")[Symbol.asyncIterator]().next();
|
2020-06-25 06:57:08 +08:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-02-08 23:59:38 +03:00
|
|
|
});
|
2021-02-25 18:16:18 +08:00
|
|
|
|
|
|
|
unitTest(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true }, ignore: Deno.build.os == "windows" },
|
2021-02-25 18:16:18 +08:00
|
|
|
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(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true }, ignore: Deno.build.os == "windows" },
|
2021-08-05 13:08:58 +02:00
|
|
|
function readDirDevFdSync() {
|
2021-02-25 18:16:18 +08: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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-10-11 21:21:18 +08:00
|
|
|
|
|
|
|
unitTest({ permissions: { read: true } }, async function readDirNotFound() {
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.readDir("bad_dir_name")[Symbol.asyncIterator]().next();
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`readdir 'bad_dir_name'`,
|
|
|
|
);
|
|
|
|
});
|