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";
|
2018-09-09 20:25:43 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: true } }, function readFileSyncSuccess(): void {
|
2019-10-31 22:33:27 -04:00
|
|
|
const data = Deno.readFileSync("cli/tests/fixture.json");
|
2018-09-09 20:25:43 -04:00
|
|
|
assert(data.byteLength > 0);
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const json = decoder.decode(data);
|
|
|
|
const pkg = JSON.parse(json);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(pkg.name, "deno");
|
2018-09-09 20:25:43 -04:00
|
|
|
});
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
unitTest({ perms: { read: true } }, function readFileSyncUrl(): void {
|
|
|
|
const data = Deno.readFileSync(
|
2020-07-14 15:24:17 -04:00
|
|
|
pathToAbsoluteFileUrl("cli/tests/fixture.json"),
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
assert(data.byteLength > 0);
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const json = decoder.decode(data);
|
|
|
|
const pkg = JSON.parse(json);
|
|
|
|
assertEquals(pkg.name, "deno");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: false } }, function readFileSyncPerm(): void {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2019-10-31 22:33:27 -04:00
|
|
|
Deno.readFileSync("cli/tests/fixture.json");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: true } }, function readFileSyncNotFound(): void {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
|
|
|
Deno.readFileSync("bad_filename");
|
|
|
|
}, Deno.errors.NotFound);
|
2018-09-09 20:25:43 -04:00
|
|
|
});
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
unitTest({ perms: { read: true } }, async function readFileUrl(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const data = await Deno.readFile(
|
2020-07-14 15:24:17 -04:00
|
|
|
pathToAbsoluteFileUrl("cli/tests/fixture.json"),
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
assert(data.byteLength > 0);
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const json = decoder.decode(data);
|
|
|
|
const pkg = JSON.parse(json);
|
|
|
|
assertEquals(pkg.name, "deno");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: true } }, async function readFileSuccess(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-10-31 22:33:27 -04:00
|
|
|
const data = await Deno.readFile("cli/tests/fixture.json");
|
2018-09-09 20:25:43 -04:00
|
|
|
assert(data.byteLength > 0);
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const json = decoder.decode(data);
|
|
|
|
const pkg = JSON.parse(json);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(pkg.name, "deno");
|
2018-09-09 20:25:43 -04:00
|
|
|
});
|
2019-02-08 15:59:38 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: false } }, async function readFilePerm(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2019-10-31 22:33:27 -04:00
|
|
|
await Deno.readFile("cli/tests/fixture.json");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
});
|
2020-04-15 20:43:19 -04:00
|
|
|
|
|
|
|
unitTest({ perms: { read: true } }, function readFileSyncLoop(): void {
|
|
|
|
for (let i = 0; i < 256; i++) {
|
|
|
|
Deno.readFileSync("cli/tests/fixture.json");
|
|
|
|
}
|
|
|
|
});
|
2021-04-08 10:36:52 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
|
|
|
async function readFileDoesNotLeakResources(): Promise<void> {
|
|
|
|
const resourcesBefore = Deno.resources();
|
|
|
|
await assertThrowsAsync(async () => await Deno.readFile("cli"));
|
|
|
|
assertEquals(resourcesBefore, Deno.resources());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
|
|
|
function readFileSyncDoesNotLeakResources(): void {
|
|
|
|
const resourcesBefore = Deno.resources();
|
|
|
|
assertThrows(() => Deno.readFileSync("cli"));
|
|
|
|
assertEquals(resourcesBefore, Deno.resources());
|
|
|
|
},
|
|
|
|
);
|
2021-06-22 11:45:26 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
|
|
|
async function readFileWithAbortSignal(): Promise<void> {
|
|
|
|
const ac = new AbortController();
|
|
|
|
queueMicrotask(() => ac.abort());
|
|
|
|
await assertThrowsAsync(async () => {
|
|
|
|
await Deno.readFile("cli/tests/fixture.json", { signal: ac.signal });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
|
|
|
async function readTextileWithAbortSignal(): Promise<void> {
|
|
|
|
const ac = new AbortController();
|
|
|
|
queueMicrotask(() => ac.abort());
|
|
|
|
await assertThrowsAsync(async () => {
|
|
|
|
await Deno.readTextFile("cli/tests/fixture.json", { signal: ac.signal });
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|