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
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, function readFileSyncSuccess() {
|
2021-08-11 10:20:47 -04:00
|
|
|
const data = Deno.readFileSync("cli/tests/testdata/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
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, function readFileSyncUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const data = Deno.readFileSync(
|
2021-08-11 10:20:47 -04:00
|
|
|
pathToAbsoluteFileUrl("cli/tests/testdata/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");
|
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: false } }, function readFileSyncPerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2021-08-11 10:20:47 -04:00
|
|
|
Deno.readFileSync("cli/tests/testdata/fixture.json");
|
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 readFileSyncNotFound() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
|
|
|
Deno.readFileSync("bad_filename");
|
|
|
|
}, Deno.errors.NotFound);
|
2018-09-09 20:25:43 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, async function readFileUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const data = await Deno.readFile(
|
2021-08-11 10:20:47 -04:00
|
|
|
pathToAbsoluteFileUrl("cli/tests/testdata/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");
|
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, async function readFileSuccess() {
|
2021-08-11 10:20:47 -04:00
|
|
|
const data = await Deno.readFile("cli/tests/testdata/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
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: false } }, async function readFilePerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2021-08-11 10:20:47 -04:00
|
|
|
await Deno.readFile("cli/tests/testdata/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
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, function readFileSyncLoop() {
|
2020-04-15 20:43:19 -04:00
|
|
|
for (let i = 0; i < 256; i++) {
|
2021-08-11 10:20:47 -04:00
|
|
|
Deno.readFileSync("cli/tests/testdata/fixture.json");
|
2020-04-15 20:43:19 -04:00
|
|
|
}
|
|
|
|
});
|
2021-04-08 10:36:52 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function readFileDoesNotLeakResources() {
|
2021-04-08 10:36:52 -04:00
|
|
|
const resourcesBefore = Deno.resources();
|
|
|
|
await assertThrowsAsync(async () => await Deno.readFile("cli"));
|
|
|
|
assertEquals(resourcesBefore, Deno.resources());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function readFileSyncDoesNotLeakResources() {
|
2021-04-08 10:36:52 -04:00
|
|
|
const resourcesBefore = Deno.resources();
|
|
|
|
assertThrows(() => Deno.readFileSync("cli"));
|
|
|
|
assertEquals(resourcesBefore, Deno.resources());
|
|
|
|
},
|
|
|
|
);
|
2021-06-22 11:45:26 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function readFileWithAbortSignal() {
|
2021-06-22 11:45:26 -04:00
|
|
|
const ac = new AbortController();
|
|
|
|
queueMicrotask(() => ac.abort());
|
|
|
|
await assertThrowsAsync(async () => {
|
2021-08-11 10:20:47 -04:00
|
|
|
await Deno.readFile("cli/tests/testdata/fixture.json", {
|
|
|
|
signal: ac.signal,
|
|
|
|
});
|
2021-06-22 11:45:26 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function readTextileWithAbortSignal() {
|
2021-06-22 11:45:26 -04:00
|
|
|
const ac = new AbortController();
|
|
|
|
queueMicrotask(() => ac.abort());
|
|
|
|
await assertThrowsAsync(async () => {
|
2021-08-11 10:20:47 -04:00
|
|
|
await Deno.readTextFile("cli/tests/testdata/fixture.json", {
|
|
|
|
signal: ac.signal,
|
|
|
|
});
|
2021-06-22 11:45:26 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|