2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 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");
|
|
|
|
}
|
|
|
|
});
|