2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-04 11:31:14 -05:00
|
|
|
import { unitTest, assert, assertEquals } 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-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: false } }, function readFileSyncPerm(): void {
|
2019-02-08 15:59:38 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-10-31 22:33:27 -04:00
|
|
|
Deno.readFileSync("cli/tests/fixture.json");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest({ perms: { read: true } }, function readFileSyncNotFound(): void {
|
2018-09-09 20:25:43 -04:00
|
|
|
let caughtError = false;
|
|
|
|
let data;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
data = Deno.readFileSync("bad_filename");
|
2018-09-09 20:25:43 -04:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.NotFound);
|
2018-09-09 20:25:43 -04:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
assert(data === undefined);
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
> {
|
2019-02-08 15:59:38 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-10-31 22:33:27 -04:00
|
|
|
await Deno.readFile("cli/tests/fixture.json");
|
2019-02-08 15:59:38 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-02-08 15:59:38 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|