2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-06-11 12:36:20 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows,
|
2020-06-11 12:36:20 -04:00
|
|
|
pathToAbsoluteFileUrl,
|
|
|
|
} 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-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, function readDirSyncSuccess() {
|
2021-08-11 10:20:47 -04:00
|
|
|
const files = [...Deno.readDirSync("cli/tests/testdata")];
|
2018-10-03 17:56:56 -04:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, function readDirSyncWithUrl() {
|
2021-08-11 10:20:47 -04:00
|
|
|
const files = [
|
|
|
|
...Deno.readDirSync(pathToAbsoluteFileUrl("cli/tests/testdata")),
|
|
|
|
];
|
2020-06-11 12:36:20 -04:00
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { 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-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, function readDirSyncNotDir() {
|
2021-10-11 09:21:18 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
2022-09-19 10:32:21 -04:00
|
|
|
Deno.readDirSync("cli/tests/testdata/assets/fixture.json");
|
2021-10-11 09:21:18 -04:00
|
|
|
},
|
|
|
|
Error,
|
2022-09-19 10:32:21 -04:00
|
|
|
`readdir 'cli/tests/testdata/assets/fixture.json'`,
|
2021-10-11 09:21:18 -04:00
|
|
|
);
|
2018-10-03 17:56:56 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, function readDirSyncNotFound() {
|
2021-10-11 09:21:18 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.readDirSync("bad_dir_name");
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`readdir 'bad_dir_name'`,
|
|
|
|
);
|
2018-10-03 17:56:56 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function readDirSuccess() {
|
2020-04-16 01:40:30 -04:00
|
|
|
const files = [];
|
2021-08-11 10:20:47 -04:00
|
|
|
for await (const dirEntry of Deno.readDir("cli/tests/testdata")) {
|
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-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { 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 (
|
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-11 12:36:20 -04:00
|
|
|
files.push(dirEntry);
|
|
|
|
}
|
|
|
|
assertSameContent(files);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: false } }, async function readDirPerm() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true }, ignore: Deno.build.os == "windows" },
|
2021-02-25 05:16:18 -05: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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2021-10-11 09:21:18 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { read: true } }, async function readDirNotFound() {
|
2021-10-11 09:21:18 -04:00
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.readDir("bad_dir_name")[Symbol.asyncIterator]().next();
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`readdir 'bad_dir_name'`,
|
|
|
|
);
|
|
|
|
});
|