2019-01-22 04:03:30 +09:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { testPerm, assert, assertEquals } from "./test_util.ts";
|
2018-09-09 20:25:43 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true }, function readFileSyncSuccess(): void {
|
2019-02-13 02:08:56 +11:00
|
|
|
const data = Deno.readFileSync("package.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-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: false }, function readFileSyncPerm(): void {
|
2019-02-08 23:59:38 +03:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-03-10 04:30:38 +11:00
|
|
|
Deno.readFileSync("package.json");
|
2019-02-08 23:59:38 +03:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(e.name, "PermissionDenied");
|
2019-02-08 23:59:38 +03:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true }, function readFileSyncNotFound(): void {
|
2018-09-09 20:25:43 -04:00
|
|
|
let caughtError = false;
|
|
|
|
let data;
|
|
|
|
try {
|
2019-02-13 02:08:56 +11:00
|
|
|
data = Deno.readFileSync("bad_filename");
|
2018-09-09 20:25:43 -04:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e.kind, Deno.ErrorKind.NotFound);
|
2018-09-09 20:25:43 -04:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
assert(data === undefined);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: true }, async function readFileSuccess(): Promise<void> {
|
2019-02-13 02:08:56 +11:00
|
|
|
const data = await Deno.readFile("package.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 23:59:38 +03:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ read: false }, async function readFilePerm(): Promise<void> {
|
2019-02-08 23:59:38 +03:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-02-13 02:08:56 +11:00
|
|
|
await Deno.readFile("package.json");
|
2019-02-08 23:59:38 +03:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(e.name, "PermissionDenied");
|
2019-02-08 23:59:38 +03:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|