0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor(cli/tests/unit) to use assertThrows (#6459)

This commit is contained in:
Casper Beyer 2020-06-25 06:57:08 +08:00 committed by GitHub
parent 6bbe52fba3
commit 87f8f99c49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 404 additions and 913 deletions

View file

@ -6,7 +6,6 @@
import { import {
assertEquals, assertEquals,
assert, assert,
assertStringContains,
assertThrows, assertThrows,
assertThrowsAsync, assertThrowsAsync,
unitTest, unitTest,
@ -159,15 +158,13 @@ unitTest(async function bufferTooLargeByteWrites(): Promise<void> {
const buf = new Deno.Buffer(xBytes.buffer as ArrayBuffer); const buf = new Deno.Buffer(xBytes.buffer as ArrayBuffer);
await buf.read(tmp); await buf.read(tmp);
let err; assertThrows(
try { () => {
buf.grow(growLen); buf.grow(growLen);
} catch (e) { },
err = e; Error,
} "grown beyond the maximum size"
);
assert(err instanceof Error);
assertStringContains(err.message, "grown beyond the maximum size");
}); });
unitTest(async function bufferLargeByteReads(): Promise<void> { unitTest(async function bufferLargeByteReads(): Promise<void> {

View file

@ -1,5 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import {
unitTest,
assert,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
unitTest( unitTest(
{ ignore: Deno.build.os === "windows", perms: { read: true, write: true } }, { ignore: Deno.build.os === "windows", perms: { read: true, write: true } },
@ -70,25 +76,16 @@ unitTest(
); );
unitTest({ perms: { write: true } }, function chmodSyncFailure(): void { unitTest({ perms: { write: true } }, function chmodSyncFailure(): void {
let err; assertThrows(() => {
try {
const filename = "/badfile.txt"; const filename = "/badfile.txt";
Deno.chmodSync(filename, 0o777); Deno.chmodSync(filename, 0o777);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
}); });
unitTest({ perms: { write: false } }, function chmodSyncPerm(): void { unitTest({ perms: { write: false } }, function chmodSyncPerm(): void {
let err; assertThrows(() => {
try {
Deno.chmodSync("/somefile.txt", 0o777); Deno.chmodSync("/somefile.txt", 0o777);
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest( unitTest(
@ -163,25 +160,16 @@ unitTest(
unitTest({ perms: { write: true } }, async function chmodFailure(): Promise< unitTest({ perms: { write: true } }, async function chmodFailure(): Promise<
void void
> { > {
let err; await assertThrowsAsync(async () => {
try {
const filename = "/badfile.txt"; const filename = "/badfile.txt";
await Deno.chmod(filename, 0o777); await Deno.chmod(filename, 0o777);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
}); });
unitTest({ perms: { write: false } }, async function chmodPerm(): Promise< unitTest({ perms: { write: false } }, async function chmodPerm(): Promise<
void void
> { > {
let err; await assertThrowsAsync(async () => {
try {
await Deno.chmod("/somefile.txt", 0o777); await Deno.chmod("/somefile.txt", 0o777);
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });

View file

@ -1,5 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import {
unitTest,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
function readFileString(filename: string | URL): string { function readFileString(filename: string | URL): string {
const dataRead = Deno.readFileSync(filename); const dataRead = Deno.readFileSync(filename);
@ -67,14 +72,9 @@ unitTest(
const fromFilename = tempDir + "/from.txt"; const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt"; const toFilename = tempDir + "/to.txt";
// We skip initial writing here, from.txt does not exist // We skip initial writing here, from.txt does not exist
let err; assertThrows(() => {
try {
Deno.copyFileSync(fromFilename, toFilename); Deno.copyFileSync(fromFilename, toFilename);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(!!err);
assert(err instanceof Deno.errors.NotFound);
Deno.removeSync(tempDir, { recursive: true }); Deno.removeSync(tempDir, { recursive: true });
} }
@ -83,28 +83,18 @@ unitTest(
unitTest( unitTest(
{ perms: { write: true, read: false } }, { perms: { write: true, read: false } },
function copyFileSyncPerm1(): void { function copyFileSyncPerm1(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.copyFileSync("/from.txt", "/to.txt"); Deno.copyFileSync("/from.txt", "/to.txt");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
} }
); );
unitTest( unitTest(
{ perms: { write: false, read: true } }, { perms: { write: false, read: true } },
function copyFileSyncPerm2(): void { function copyFileSyncPerm2(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.copyFileSync("/from.txt", "/to.txt"); Deno.copyFileSync("/from.txt", "/to.txt");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
} }
); );
@ -172,14 +162,9 @@ unitTest(
const fromFilename = tempDir + "/from.txt"; const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt"; const toFilename = tempDir + "/to.txt";
// We skip initial writing here, from.txt does not exist // We skip initial writing here, from.txt does not exist
let err; await assertThrowsAsync(async () => {
try {
await Deno.copyFile(fromFilename, toFilename); await Deno.copyFile(fromFilename, toFilename);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(!!err);
assert(err instanceof Deno.errors.NotFound);
Deno.removeSync(tempDir, { recursive: true }); Deno.removeSync(tempDir, { recursive: true });
} }
@ -207,27 +192,17 @@ unitTest(
unitTest( unitTest(
{ perms: { read: false, write: true } }, { perms: { read: false, write: true } },
async function copyFilePerm1(): Promise<void> { async function copyFilePerm1(): Promise<void> {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.copyFile("/from.txt", "/to.txt"); await Deno.copyFile("/from.txt", "/to.txt");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
} }
); );
unitTest( unitTest(
{ perms: { read: true, write: false } }, { perms: { read: true, write: false } },
async function copyFilePerm2(): Promise<void> { async function copyFilePerm2(): Promise<void> {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.copyFile("/from.txt", "/to.txt"); await Deno.copyFile("/from.txt", "/to.txt");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
} }
); );

View file

@ -3,35 +3,33 @@ import {
unitTest, unitTest,
assert, assert,
assertEquals, assertEquals,
assertStringContains,
assertThrows, assertThrows,
assertThrowsAsync,
fail, fail,
} from "./test_util.ts"; } from "./test_util.ts";
unitTest({ perms: { net: true } }, async function fetchProtocolError(): Promise< unitTest({ perms: { net: true } }, async function fetchProtocolError(): Promise<
void void
> { > {
let err; await assertThrowsAsync(
try { async (): Promise<void> => {
await fetch("file:///"); await fetch("file:///");
} catch (err_) { },
err = err_; TypeError,
} "not supported"
assert(err instanceof TypeError); );
assertStringContains(err.message, "not supported");
}); });
unitTest( unitTest(
{ perms: { net: true } }, { perms: { net: true } },
async function fetchConnectionError(): Promise<void> { async function fetchConnectionError(): Promise<void> {
let err; await assertThrowsAsync(
try { async (): Promise<void> => {
await fetch("http://localhost:4000"); await fetch("http://localhost:4000");
} catch (err_) { },
err = err_; Deno.errors.Http,
} "error trying to connect"
assert(err instanceof Deno.errors.Http); );
assertStringContains(err.message, "error trying to connect");
} }
); );
@ -44,14 +42,9 @@ unitTest({ perms: { net: true } }, async function fetchJsonSuccess(): Promise<
}); });
unitTest(async function fetchPerm(): Promise<void> { unitTest(async function fetchPerm(): Promise<void> {
let err; await assertThrowsAsync(async () => {
try {
await fetch("http://localhost:4545/cli/tests/fixture.json"); await fetch("http://localhost:4545/cli/tests/fixture.json");
} catch (err_) { }, Deno.errors.PermissionDenied);
err = err_;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest({ perms: { net: true } }, async function fetchUrl(): Promise<void> { unitTest({ perms: { net: true } }, async function fetchUrl(): Promise<void> {
@ -208,13 +201,9 @@ unitTest({ perms: { net: true } }, async function responseClone(): Promise<
unitTest({ perms: { net: true } }, async function fetchEmptyInvalid(): Promise< unitTest({ perms: { net: true } }, async function fetchEmptyInvalid(): Promise<
void void
> { > {
let err; await assertThrowsAsync(async () => {
try {
await fetch(""); await fetch("");
} catch (err_) { }, URIError);
err = err_;
}
assert(err instanceof URIError);
}); });
unitTest( unitTest(

View file

@ -3,7 +3,7 @@ import {
unitTest, unitTest,
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertThrowsAsync,
} from "./test_util.ts"; } from "./test_util.ts";
unitTest(function filesStdioFileDescriptors(): void { unitTest(function filesStdioFileDescriptors(): void {
@ -251,63 +251,44 @@ unitTest(
const filename = "tests/hello.txt"; const filename = "tests/hello.txt";
const openOptions: Deno.OpenOptions[] = [{ write: true }, { append: true }]; const openOptions: Deno.OpenOptions[] = [{ write: true }, { append: true }];
for (const options of openOptions) { for (const options of openOptions) {
let err; await assertThrowsAsync(async () => {
try {
await Deno.open(filename, options); await Deno.open(filename, options);
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(!!err);
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
} }
} }
); );
unitTest(async function openOptions(): Promise<void> { unitTest(async function openOptions(): Promise<void> {
const filename = "cli/tests/fixture.json"; const filename = "cli/tests/fixture.json";
let err; await assertThrowsAsync(
try { async (): Promise<void> => {
await Deno.open(filename, { write: false }); await Deno.open(filename, { write: false });
} catch (e) { },
err = e; Error,
}
assert(!!err);
assertStringContains(
err.message,
"OpenOptions requires at least one option to be true" "OpenOptions requires at least one option to be true"
); );
try { await assertThrowsAsync(
await Deno.open(filename, { truncate: true, write: false }); async (): Promise<void> => {
} catch (e) { await Deno.open(filename, { truncate: true, write: false });
err = e; },
} Error,
assert(!!err);
assertStringContains(
err.message,
"'truncate' option requires 'write' option" "'truncate' option requires 'write' option"
); );
try { await assertThrowsAsync(
await Deno.open(filename, { create: true, write: false }); async (): Promise<void> => {
} catch (e) { await Deno.open(filename, { create: true, write: false });
err = e; },
} Error,
assert(!!err);
assertStringContains(
err.message,
"'create' or 'createNew' options require 'write' or 'append' option" "'create' or 'createNew' options require 'write' or 'append' option"
); );
try { await assertThrowsAsync(
await Deno.open(filename, { createNew: true, append: false }); async (): Promise<void> => {
} catch (e) { await Deno.open(filename, { createNew: true, append: false });
err = e; },
} Error,
assert(!!err);
assertStringContains(
err.message,
"'create' or 'createNew' options require 'write' or 'append' option" "'create' or 'createNew' options require 'write' or 'append' option"
); );
}); });
@ -315,15 +296,9 @@ unitTest(async function openOptions(): Promise<void> {
unitTest({ perms: { read: false } }, async function readPermFailure(): Promise< unitTest({ perms: { read: false } }, async function readPermFailure(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.open("package.json", { read: true }); await Deno.open("package.json", { read: true });
await Deno.open("cli/tests/fixture.json", { read: true }); }, Deno.errors.PermissionDenied);
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest( unitTest(
@ -339,16 +314,12 @@ unitTest(
const file = await Deno.open(filename, w); const file = await Deno.open(filename, w);
// writing null should throw an error // writing null should throw an error
let err; await assertThrowsAsync(
try { async (): Promise<void> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
await file.write(null as any); await file.write(null as any);
} catch (e) { }
err = e; ); // TODO: Check error kind when dispatch_minimal pipes errors properly
}
// TODO: Check error kind when dispatch_minimal pipes errors properly
assert(!!err);
file.close(); file.close();
await Deno.remove(tempDir, { recursive: true }); await Deno.remove(tempDir, { recursive: true });
} }
@ -371,15 +342,11 @@ unitTest(
assert(bytesRead === 0); assert(bytesRead === 0);
// reading file into null buffer should throw an error // reading file into null buffer should throw an error
let err; await assertThrowsAsync(async () => {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
await file.read(null as any); await file.read(null as any);
} catch (e) { }, TypeError);
err = e;
}
// TODO: Check error kind when dispatch_minimal pipes errors properly // TODO: Check error kind when dispatch_minimal pipes errors properly
assert(!!err);
file.close(); file.close();
await Deno.remove(tempDir, { recursive: true }); await Deno.remove(tempDir, { recursive: true });
@ -390,15 +357,9 @@ unitTest(
{ perms: { write: false, read: false } }, { perms: { write: false, read: false } },
async function readWritePermFailure(): Promise<void> { async function readWritePermFailure(): Promise<void> {
const filename = "tests/hello.txt"; const filename = "tests/hello.txt";
let err; await assertThrowsAsync(async () => {
try {
await Deno.open(filename, { read: true }); await Deno.open(filename, { read: true });
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(!!err);
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
} }
); );
@ -668,15 +629,13 @@ unitTest({ perms: { read: true } }, function seekSyncEnd(): void {
unitTest({ perms: { read: true } }, async function seekMode(): Promise<void> { unitTest({ perms: { read: true } }, async function seekMode(): Promise<void> {
const filename = "cli/tests/hello.txt"; const filename = "cli/tests/hello.txt";
const file = await Deno.open(filename); const file = await Deno.open(filename);
let err; await assertThrowsAsync(
try { async (): Promise<void> => {
await file.seek(1, -1); await file.seek(1, -1);
} catch (e) { },
err = e; TypeError,
} "Invalid seek mode"
assert(!!err); );
assert(err instanceof TypeError);
assertStringContains(err.message, "Invalid seek mode");
// We should still be able to read the file // We should still be able to read the file
// since it is still open. // since it is still open.

View file

@ -1,37 +1,28 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert } from "./test_util.ts"; import { unitTest, assert, assertThrows } from "./test_util.ts";
// TODO(ry) Add more tests to specify format. // TODO(ry) Add more tests to specify format.
unitTest({ perms: { read: false } }, function watchFsPermissions() { unitTest({ perms: { read: false } }, function watchFsPermissions() {
let thrown = false; assertThrows(() => {
try {
Deno.watchFs("."); Deno.watchFs(".");
} catch (err) { }, Deno.errors.PermissionDenied);
assert(err instanceof Deno.errors.PermissionDenied);
thrown = true;
}
assert(thrown);
}); });
unitTest({ perms: { read: true } }, function watchFsInvalidPath() { unitTest({ perms: { read: true } }, function watchFsInvalidPath() {
let thrown = false; if (Deno.build.os === "windows") {
try { assertThrows(
Deno.watchFs("non-existant.file"); () => {
} catch (err) { Deno.watchFs("non-existant.file");
console.error(err); },
if (Deno.build.os === "windows") { Error,
assert( "Input watch path is neither a file nor a directory"
err.message.includes( );
"Input watch path is neither a file nor a directory" } else {
) assertThrows(() => {
); Deno.watchFs("non-existant.file");
} else { }, Deno.errors.NotFound);
assert(err instanceof Deno.errors.NotFound);
}
thrown = true;
} }
assert(thrown);
}); });
async function getTwoEvents( async function getTwoEvents(

View file

@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import { unitTest, assert, assertEquals, assertThrows } from "./test_util.ts";
unitTest( unitTest(
{ perms: { read: true, write: true } }, { perms: { read: true, write: true } },
@ -50,14 +50,9 @@ unitTest(
// newname is already created. // newname is already created.
Deno.writeFileSync(newName, new TextEncoder().encode("newName")); Deno.writeFileSync(newName, new TextEncoder().encode("newName"));
let err; assertThrows(() => {
try {
Deno.linkSync(oldName, newName); Deno.linkSync(oldName, newName);
} catch (e) { }, Deno.errors.AlreadyExists);
err = e;
}
assert(!!err);
assert(err instanceof Deno.errors.AlreadyExists);
} }
); );
@ -68,42 +63,27 @@ unitTest(
const oldName = testDir + "/oldname"; const oldName = testDir + "/oldname";
const newName = testDir + "/newname"; const newName = testDir + "/newname";
let err; assertThrows(() => {
try {
Deno.linkSync(oldName, newName); Deno.linkSync(oldName, newName);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(!!err);
assert(err instanceof Deno.errors.NotFound);
} }
); );
unitTest( unitTest(
{ perms: { read: false, write: true } }, { perms: { read: false, write: true } },
function linkSyncReadPerm(): void { function linkSyncReadPerm(): void {
let err; assertThrows(() => {
try {
Deno.linkSync("oldbaddir", "newbaddir"); Deno.linkSync("oldbaddir", "newbaddir");
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
} }
); );
unitTest( unitTest(
{ perms: { read: true, write: false } }, { perms: { read: true, write: false } },
function linkSyncWritePerm(): void { function linkSyncWritePerm(): void {
let err; assertThrows(() => {
try {
Deno.linkSync("oldbaddir", "newbaddir"); Deno.linkSync("oldbaddir", "newbaddir");
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
} }
); );

View file

@ -1,5 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import {
unitTest,
assert,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
unitTest({ perms: { write: true } }, function makeTempDirSyncSuccess(): void { unitTest({ perms: { write: true } }, function makeTempDirSyncSuccess(): void {
const dir1 = Deno.makeTempDirSync({ prefix: "hello", suffix: "world" }); const dir1 = Deno.makeTempDirSync({ prefix: "hello", suffix: "world" });
@ -17,13 +23,9 @@ unitTest({ perms: { write: true } }, function makeTempDirSyncSuccess(): void {
assert(dir3.startsWith(dir1)); assert(dir3.startsWith(dir1));
assert(/^[\\\/]/.test(dir3.slice(dir1.length))); assert(/^[\\\/]/.test(dir3.slice(dir1.length)));
// Check that creating a temp dir inside a nonexisting directory fails. // Check that creating a temp dir inside a nonexisting directory fails.
let err; assertThrows(() => {
try {
Deno.makeTempDirSync({ dir: "/baddir" }); Deno.makeTempDirSync({ dir: "/baddir" });
} catch (err_) { }, Deno.errors.NotFound);
err = err_;
}
assert(err instanceof Deno.errors.NotFound);
}); });
unitTest( unitTest(
@ -39,14 +41,9 @@ unitTest(
unitTest(function makeTempDirSyncPerm(): void { unitTest(function makeTempDirSyncPerm(): void {
// makeTempDirSync should require write permissions (for now). // makeTempDirSync should require write permissions (for now).
let err; assertThrows(() => {
try {
Deno.makeTempDirSync({ dir: "/baddir" }); Deno.makeTempDirSync({ dir: "/baddir" });
} catch (err_) { }, Deno.errors.PermissionDenied);
err = err_;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest( unitTest(
@ -67,13 +64,9 @@ unitTest(
assert(dir3.startsWith(dir1)); assert(dir3.startsWith(dir1));
assert(/^[\\\/]/.test(dir3.slice(dir1.length))); assert(/^[\\\/]/.test(dir3.slice(dir1.length)));
// Check that creating a temp dir inside a nonexisting directory fails. // Check that creating a temp dir inside a nonexisting directory fails.
let err; await assertThrowsAsync(async () => {
try {
await Deno.makeTempDir({ dir: "/baddir" }); await Deno.makeTempDir({ dir: "/baddir" });
} catch (err_) { }, Deno.errors.NotFound);
err = err_;
}
assert(err instanceof Deno.errors.NotFound);
} }
); );
@ -105,13 +98,9 @@ unitTest({ perms: { write: true } }, function makeTempFileSyncSuccess(): void {
assert(file3.startsWith(dir)); assert(file3.startsWith(dir));
assert(/^[\\\/]/.test(file3.slice(dir.length))); assert(/^[\\\/]/.test(file3.slice(dir.length)));
// Check that creating a temp file inside a nonexisting directory fails. // Check that creating a temp file inside a nonexisting directory fails.
let err; assertThrows(() => {
try {
Deno.makeTempFileSync({ dir: "/baddir" }); Deno.makeTempFileSync({ dir: "/baddir" });
} catch (err_) { }, Deno.errors.NotFound);
err = err_;
}
assert(err instanceof Deno.errors.NotFound);
}); });
unitTest( unitTest(
@ -127,14 +116,9 @@ unitTest(
unitTest(function makeTempFileSyncPerm(): void { unitTest(function makeTempFileSyncPerm(): void {
// makeTempFileSync should require write permissions (for now). // makeTempFileSync should require write permissions (for now).
let err; assertThrows(() => {
try {
Deno.makeTempFileSync({ dir: "/baddir" }); Deno.makeTempFileSync({ dir: "/baddir" });
} catch (err_) { }, Deno.errors.PermissionDenied);
err = err_;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest( unitTest(
@ -156,13 +140,9 @@ unitTest(
assert(file3.startsWith(dir)); assert(file3.startsWith(dir));
assert(/^[\\\/]/.test(file3.slice(dir.length))); assert(/^[\\\/]/.test(file3.slice(dir.length)));
// Check that creating a temp file inside a nonexisting directory fails. // Check that creating a temp file inside a nonexisting directory fails.
let err; await assertThrowsAsync(async () => {
try {
await Deno.makeTempFile({ dir: "/baddir" }); await Deno.makeTempFile({ dir: "/baddir" });
} catch (err_) { }, Deno.errors.NotFound);
err = err_;
}
assert(err instanceof Deno.errors.NotFound);
} }
); );

View file

@ -1,5 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals, assertThrows } from "./test_util.ts"; import {
unitTest,
assert,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
function assertDirectory(path: string, mode?: number): void { function assertDirectory(path: string, mode?: number): void {
const info = Deno.lstatSync(path); const info = Deno.lstatSync(path);
@ -28,14 +34,9 @@ unitTest(
); );
unitTest({ perms: { write: false } }, function mkdirSyncPerm(): void { unitTest({ perms: { write: false } }, function mkdirSyncPerm(): void {
let err; assertThrows(() => {
try {
Deno.mkdirSync("/baddir"); Deno.mkdirSync("/baddir");
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest( unitTest(
@ -57,25 +58,17 @@ unitTest(
); );
unitTest({ perms: { write: true } }, function mkdirErrSyncIfExists(): void { unitTest({ perms: { write: true } }, function mkdirErrSyncIfExists(): void {
let err; assertThrows(() => {
try {
Deno.mkdirSync("."); Deno.mkdirSync(".");
} catch (e) { }, Deno.errors.AlreadyExists);
err = e;
}
assert(err instanceof Deno.errors.AlreadyExists);
}); });
unitTest({ perms: { write: true } }, async function mkdirErrIfExists(): Promise< unitTest({ perms: { write: true } }, async function mkdirErrIfExists(): Promise<
void void
> { > {
let err; await assertThrowsAsync(async () => {
try {
await Deno.mkdir("."); await Deno.mkdir(".");
} catch (e) { }, Deno.errors.AlreadyExists);
err = e;
}
assert(err instanceof Deno.errors.AlreadyExists);
}); });
unitTest( unitTest(

View file

@ -3,6 +3,8 @@ import {
unitTest, unitTest,
assert, assert,
assertEquals, assertEquals,
assertThrows,
assertThrowsAsync,
createResolvable, createResolvable,
assertNotEquals, assertNotEquals,
} from "./test_util.ts"; } from "./test_util.ts";
@ -71,15 +73,13 @@ unitTest(
const listener = Deno.listen({ port: 4501 }); const listener = Deno.listen({ port: 4501 });
const p = listener.accept(); const p = listener.accept();
listener.close(); listener.close();
let err; await assertThrowsAsync(
try { async (): Promise<void> => {
await p; await p;
} catch (e) { },
err = e; Deno.errors.BadResource,
} "Listener has been closed"
assert(!!err); );
assert(err instanceof Error);
assertEquals(err.message, "Listener has been closed");
} }
); );
@ -93,15 +93,13 @@ unitTest(
}); });
const p = listener.accept(); const p = listener.accept();
listener.close(); listener.close();
let err; await assertThrowsAsync(
try { async (): Promise<void> => {
await p; await p;
} catch (e) { },
err = e; Deno.errors.BadResource,
} "Listener has been closed"
assert(!!err); );
assert(err instanceof Error);
assertEquals(err.message, "Listener has been closed");
} }
); );
@ -458,14 +456,9 @@ unitTest(
assertEquals(2, buf[1]); assertEquals(2, buf[1]);
assertEquals(3, buf[2]); assertEquals(3, buf[2]);
// Check write should be closed // Check write should be closed
let err; await assertThrowsAsync(async () => {
try {
await conn.write(new Uint8Array([1, 2, 3])); await conn.write(new Uint8Array([1, 2, 3]));
} catch (e) { }, Deno.errors.BrokenPipe);
err = e;
}
assert(!!err);
assert(err instanceof Deno.errors.BrokenPipe);
closeDeferred.resolve(); closeDeferred.resolve();
listener.close(); listener.close();
conn.close(); conn.close();
@ -488,15 +481,10 @@ unitTest(
}); });
const conn = await Deno.connect(addr); const conn = await Deno.connect(addr);
conn.closeWrite(); // closing write conn.closeWrite(); // closing write
let err; assertThrows(() => {
try {
// Duplicated close should throw error // Duplicated close should throw error
conn.closeWrite(); conn.closeWrite();
} catch (e) { }, Deno.errors.NotConnected);
err = e;
}
assert(!!err);
assert(err instanceof Deno.errors.NotConnected);
closeDeferred.resolve(); closeDeferred.resolve();
listener.close(); listener.close();
conn.close(); conn.close();

View file

@ -28,27 +28,15 @@ unitTest({ perms: { env: true } }, function deleteEnv(): void {
}); });
unitTest(function envPermissionDenied1(): void { unitTest(function envPermissionDenied1(): void {
let err; assertThrows(() => {
try {
Deno.env.toObject(); Deno.env.toObject();
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assertNotEquals(err, undefined);
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest(function envPermissionDenied2(): void { unitTest(function envPermissionDenied2(): void {
let err; assertThrows(() => {
try {
Deno.env.get("PATH"); Deno.env.get("PATH");
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assertNotEquals(err, undefined);
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
// This test verifies that on Windows, environment variables are // This test verifies that on Windows, environment variables are
@ -143,15 +131,9 @@ unitTest({ perms: { env: true } }, function loadavgSuccess(): void {
}); });
unitTest({ perms: { env: false } }, function loadavgPerm(): void { unitTest({ perms: { env: false } }, function loadavgPerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.loadavg(); Deno.loadavg();
} catch (err) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}
assert(caughtError);
}); });
unitTest({ perms: { env: true } }, function hostnameDir(): void { unitTest({ perms: { env: true } }, function hostnameDir(): void {
@ -159,15 +141,9 @@ unitTest({ perms: { env: true } }, function hostnameDir(): void {
}); });
unitTest({ perms: { env: false } }, function hostnamePerm(): void { unitTest({ perms: { env: false } }, function hostnamePerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.hostname(); Deno.hostname();
} catch (err) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}
assert(caughtError);
}); });
unitTest({ perms: { env: true } }, function releaseDir(): void { unitTest({ perms: { env: true } }, function releaseDir(): void {
@ -175,13 +151,7 @@ unitTest({ perms: { env: true } }, function releaseDir(): void {
}); });
unitTest({ perms: { env: false } }, function releasePerm(): void { unitTest({ perms: { env: false } }, function releasePerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.osRelease(); Deno.osRelease();
} catch (err) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}
assert(caughtError);
}); });

View file

@ -3,18 +3,14 @@ import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringContains,
assertThrows,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
unitTest(function runPermissions(): void { unitTest(function runPermissions(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.run({ cmd: ["python", "-c", "print('hello world')"] }); Deno.run({ cmd: ["python", "-c", "print('hello world')"] });
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { run: true } }, async function runSuccess(): Promise<void> { unitTest({ perms: { run: true } }, async function runSuccess(): Promise<void> {
@ -325,18 +321,13 @@ unitTest(function signalNumbers(): void {
}); });
unitTest(function killPermissions(): void { unitTest(function killPermissions(): void {
let caughtError = false; assertThrows(() => {
try {
// Unlike the other test cases, we don't have permission to spawn a // Unlike the other test cases, we don't have permission to spawn a
// subprocess we can safely kill. Instead we send SIGCONT to the current // subprocess we can safely kill. Instead we send SIGCONT to the current
// process - assuming that Deno does not have a special handler set for it // process - assuming that Deno does not have a special handler set for it
// and will just continue even if a signal is erroneously sent. // and will just continue even if a signal is erroneously sent.
Deno.kill(Deno.pid, Deno.Signal.SIGCONT); Deno.kill(Deno.pid, Deno.Signal.SIGCONT);
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { run: true } }, async function killSuccess(): Promise<void> { unitTest({ perms: { run: true } }, async function killSuccess(): Promise<void> {
@ -368,14 +359,9 @@ unitTest({ perms: { run: true } }, function killFailed(): void {
assert(!p.stdin); assert(!p.stdin);
assert(!p.stdout); assert(!p.stdout);
let err; assertThrows(() => {
try {
Deno.kill(p.pid, 12345); Deno.kill(p.pid, 12345);
} catch (e) { }, TypeError);
err = e;
}
assert(!!err);
assert(err instanceof TypeError);
p.close(); p.close();
}); });

View file

@ -3,6 +3,8 @@ import {
unitTest, unitTest,
assert, assert,
assertEquals, assertEquals,
assertThrows,
assertThrowsAsync,
pathToAbsoluteFileUrl, pathToAbsoluteFileUrl,
} from "./test_util.ts"; } from "./test_util.ts";
@ -30,42 +32,21 @@ unitTest({ perms: { read: true } }, function readDirSyncWithUrl(): void {
}); });
unitTest({ perms: { read: false } }, function readDirSyncPerm(): void { unitTest({ perms: { read: false } }, function readDirSyncPerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.readDirSync("tests/"); Deno.readDirSync("tests/");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function readDirSyncNotDir(): void { unitTest({ perms: { read: true } }, function readDirSyncNotDir(): void {
let caughtError = false; assertThrows(() => {
let src; Deno.readDirSync("cli/tests/fixture.json");
}, Error);
try {
src = Deno.readDirSync("cli/tests/fixture.json");
} catch (err) {
caughtError = true;
assert(err instanceof Error);
}
assert(caughtError);
assertEquals(src, undefined);
}); });
unitTest({ perms: { read: true } }, function readDirSyncNotFound(): void { unitTest({ perms: { read: true } }, function readDirSyncNotFound(): void {
let caughtError = false; assertThrows(() => {
let src; Deno.readDirSync("bad_dir_name");
}, Deno.errors.NotFound);
try {
src = Deno.readDirSync("bad_dir_name");
} catch (err) {
caughtError = true;
assert(err instanceof Deno.errors.NotFound);
}
assert(caughtError);
assertEquals(src, undefined);
}); });
unitTest({ perms: { read: true } }, async function readDirSuccess(): Promise< unitTest({ perms: { read: true } }, async function readDirSuccess(): Promise<
@ -93,12 +74,7 @@ unitTest({ perms: { read: true } }, async function readDirWithUrl(): Promise<
unitTest({ perms: { read: false } }, async function readDirPerm(): Promise< unitTest({ perms: { read: false } }, async function readDirPerm(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.readDir("tests/")[Symbol.asyncIterator]().next(); await Deno.readDir("tests/")[Symbol.asyncIterator]().next();
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });

View file

@ -3,6 +3,8 @@ import {
unitTest, unitTest,
assert, assert,
assertEquals, assertEquals,
assertThrows,
assertThrowsAsync,
pathToAbsoluteFileUrl, pathToAbsoluteFileUrl,
} from "./test_util.ts"; } from "./test_util.ts";
@ -27,27 +29,15 @@ unitTest({ perms: { read: true } }, function readFileSyncUrl(): void {
}); });
unitTest({ perms: { read: false } }, function readFileSyncPerm(): void { unitTest({ perms: { read: false } }, function readFileSyncPerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.readFileSync("cli/tests/fixture.json"); Deno.readFileSync("cli/tests/fixture.json");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function readFileSyncNotFound(): void { unitTest({ perms: { read: true } }, function readFileSyncNotFound(): void {
let caughtError = false; assertThrows(() => {
let data; Deno.readFileSync("bad_filename");
try { }, Deno.errors.NotFound);
data = Deno.readFileSync("bad_filename");
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
assert(data === undefined);
}); });
unitTest({ perms: { read: true } }, async function readFileUrl(): Promise< unitTest({ perms: { read: true } }, async function readFileUrl(): Promise<
@ -77,14 +67,9 @@ unitTest({ perms: { read: true } }, async function readFileSuccess(): Promise<
unitTest({ perms: { read: false } }, async function readFilePerm(): Promise< unitTest({ perms: { read: false } }, async function readFilePerm(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.readFile("cli/tests/fixture.json"); await Deno.readFile("cli/tests/fixture.json");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function readFileSyncLoop(): void { unitTest({ perms: { read: true } }, function readFileSyncLoop(): void {

View file

@ -1,5 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import {
unitTest,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
unitTest( unitTest(
{ perms: { write: true, read: true } }, { perms: { write: true, read: true } },
@ -19,27 +24,15 @@ unitTest(
); );
unitTest({ perms: { read: false } }, function readLinkSyncPerm(): void { unitTest({ perms: { read: false } }, function readLinkSyncPerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.readLinkSync("/symlink"); Deno.readLinkSync("/symlink");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function readLinkSyncNotFound(): void { unitTest({ perms: { read: true } }, function readLinkSyncNotFound(): void {
let caughtError = false; assertThrows(() => {
let data; Deno.readLinkSync("bad_filename");
try { }, Deno.errors.NotFound);
data = Deno.readLinkSync("bad_filename");
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
assertEquals(data, undefined);
}); });
unitTest( unitTest(
@ -62,12 +55,7 @@ unitTest(
unitTest({ perms: { read: false } }, async function readLinkPerm(): Promise< unitTest({ perms: { read: false } }, async function readLinkPerm(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.readLink("/symlink"); await Deno.readLink("/symlink");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });

View file

@ -2,6 +2,8 @@ import {
unitTest, unitTest,
assert, assert,
assertEquals, assertEquals,
assertThrows,
assertThrowsAsync,
pathToAbsoluteFileUrl, pathToAbsoluteFileUrl,
} from "./test_util.ts"; } from "./test_util.ts";
@ -22,27 +24,15 @@ unitTest({ perms: { read: true } }, function readTextFileSyncByUrl(): void {
}); });
unitTest({ perms: { read: false } }, function readTextFileSyncPerm(): void { unitTest({ perms: { read: false } }, function readTextFileSyncPerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.readTextFileSync("cli/tests/fixture.json"); Deno.readTextFileSync("cli/tests/fixture.json");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function readTextFileSyncNotFound(): void { unitTest({ perms: { read: true } }, function readTextFileSyncNotFound(): void {
let caughtError = false; assertThrows(() => {
let data; Deno.readTextFileSync("bad_filename");
try { }, Deno.errors.NotFound);
data = Deno.readTextFileSync("bad_filename");
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
assert(data === undefined);
}); });
unitTest( unitTest(
@ -69,14 +59,9 @@ unitTest({ perms: { read: true } }, async function readTextFileByUrl(): Promise<
unitTest({ perms: { read: false } }, async function readTextFilePerm(): Promise< unitTest({ perms: { read: false } }, async function readTextFilePerm(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.readTextFile("cli/tests/fixture.json"); await Deno.readTextFile("cli/tests/fixture.json");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function readTextFileSyncLoop(): void { unitTest({ perms: { read: true } }, function readTextFileSyncLoop(): void {

View file

@ -1,5 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert } from "./test_util.ts"; import {
unitTest,
assert,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
unitTest({ perms: { read: true } }, function realPathSyncSuccess(): void { unitTest({ perms: { read: true } }, function realPathSyncSuccess(): void {
const incompletePath = "cli/tests/fixture.json"; const incompletePath = "cli/tests/fixture.json";
@ -30,25 +35,15 @@ unitTest(
); );
unitTest({ perms: { read: false } }, function realPathSyncPerm(): void { unitTest({ perms: { read: false } }, function realPathSyncPerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.realPathSync("some_file"); Deno.realPathSync("some_file");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function realPathSyncNotFound(): void { unitTest({ perms: { read: true } }, function realPathSyncNotFound(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.realPathSync("bad_filename"); Deno.realPathSync("bad_filename");
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, async function realPathSuccess(): Promise< unitTest({ perms: { read: true } }, async function realPathSuccess(): Promise<
@ -84,25 +79,15 @@ unitTest(
unitTest({ perms: { read: false } }, async function realPathPerm(): Promise< unitTest({ perms: { read: false } }, async function realPathPerm(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.realPath("some_file"); await Deno.realPath("some_file");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, async function realPathNotFound(): Promise< unitTest({ perms: { read: true } }, async function realPathNotFound(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.realPath("bad_filename"); await Deno.realPath("bad_filename");
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
}); });

View file

@ -1,5 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import {
unitTest,
assert,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
const REMOVE_METHODS = ["remove", "removeSync"] as const; const REMOVE_METHODS = ["remove", "removeSync"] as const;
@ -14,14 +19,9 @@ unitTest(
assert(pathInfo.isDirectory); // check exist first assert(pathInfo.isDirectory); // check exist first
await Deno[method](path); // remove await Deno[method](path); // remove
// We then check again after remove // We then check again after remove
let err; assertThrows(() => {
try {
Deno.statSync(path); Deno.statSync(path);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
// Directory is gone
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -39,14 +39,9 @@ unitTest(
assert(fileInfo.isFile); // check exist first assert(fileInfo.isFile); // check exist first
await Deno[method](filename); // remove await Deno[method](filename); // remove
// We then check again after remove // We then check again after remove
let err; assertThrows(() => {
try {
Deno.statSync(filename); Deno.statSync(filename);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
// File is gone
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -69,14 +64,9 @@ unitTest(
assert(fileInfo.isFile); // check exist first assert(fileInfo.isFile); // check exist first
await Deno[method](fileUrl); // remove await Deno[method](fileUrl); // remove
// We then check again after remove // We then check again after remove
let err; assertThrows(() => {
try {
Deno.statSync(fileUrl); Deno.statSync(fileUrl);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
// File is gone
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -94,23 +84,16 @@ unitTest(
assert(pathInfo.isDirectory); // check exist first assert(pathInfo.isDirectory); // check exist first
const subPathInfo = Deno.statSync(subPath); const subPathInfo = Deno.statSync(subPath);
assert(subPathInfo.isDirectory); // check exist first assert(subPathInfo.isDirectory); // check exist first
let err;
try { await assertThrowsAsync(async () => {
// Should not be able to recursively remove
await Deno[method](path); await Deno[method](path);
} catch (e) { }, Error);
err = e;
}
// TODO(ry) Is Other really the error we should get here? What would Go do? // TODO(ry) Is Other really the error we should get here? What would Go do?
assert(err instanceof Error);
// NON-EXISTENT DIRECTORY/FILE // NON-EXISTENT DIRECTORY/FILE
try { await assertThrowsAsync(async () => {
// Non-existent
await Deno[method]("/baddir"); await Deno[method]("/baddir");
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -130,13 +113,9 @@ unitTest(
const pathInfo = Deno.lstatSync(danglingSymlinkPath); const pathInfo = Deno.lstatSync(danglingSymlinkPath);
assert(pathInfo.isSymlink); assert(pathInfo.isSymlink);
await Deno[method](danglingSymlinkPath); await Deno[method](danglingSymlinkPath);
let err; assertThrows(() => {
try {
Deno.lstatSync(danglingSymlinkPath); Deno.lstatSync(danglingSymlinkPath);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -159,14 +138,10 @@ unitTest(
const symlinkPathInfo = Deno.statSync(validSymlinkPath); const symlinkPathInfo = Deno.statSync(validSymlinkPath);
assert(symlinkPathInfo.isFile); assert(symlinkPathInfo.isFile);
await Deno[method](validSymlinkPath); await Deno[method](validSymlinkPath);
let err; assertThrows(() => {
try {
Deno.statSync(validSymlinkPath); Deno.statSync(validSymlinkPath);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
await Deno[method](filePath); await Deno[method](filePath);
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -175,14 +150,9 @@ unitTest({ perms: { write: false } }, async function removePerm(): Promise<
void void
> { > {
for (const method of REMOVE_METHODS) { for (const method of REMOVE_METHODS) {
let err; await assertThrowsAsync(async () => {
try {
await Deno[method]("/baddir"); await Deno[method]("/baddir");
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
} }
}); });
@ -197,14 +167,12 @@ unitTest(
assert(pathInfo.isDirectory); // check exist first assert(pathInfo.isDirectory); // check exist first
await Deno[method](path, { recursive: true }); // remove await Deno[method](path, { recursive: true }); // remove
// We then check again after remove // We then check again after remove
let err; assertThrows(
try { () => {
Deno.statSync(path); Deno.statSync(path);
} catch (e) { }, // Directory is gone
err = e; Deno.errors.NotFound
} );
// Directory is gone
assert(err instanceof Deno.errors.NotFound);
// REMOVE NON-EMPTY DIRECTORY // REMOVE NON-EMPTY DIRECTORY
path = Deno.makeTempDirSync() + "/dir/subdir"; path = Deno.makeTempDirSync() + "/dir/subdir";
@ -217,13 +185,10 @@ unitTest(
assert(subPathInfo.isDirectory); // check exist first assert(subPathInfo.isDirectory); // check exist first
await Deno[method](path, { recursive: true }); // remove await Deno[method](path, { recursive: true }); // remove
// We then check parent directory again after remove // We then check parent directory again after remove
try { assertThrows(() => {
Deno.statSync(path); Deno.statSync(path);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
// Directory is gone // Directory is gone
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -241,14 +206,10 @@ unitTest(
assert(fileInfo.isFile); // check exist first assert(fileInfo.isFile); // check exist first
await Deno[method](filename, { recursive: true }); // remove await Deno[method](filename, { recursive: true }); // remove
// We then check again after remove // We then check again after remove
let err; assertThrows(() => {
try {
Deno.statSync(filename); Deno.statSync(filename);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
// File is gone // File is gone
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -258,14 +219,10 @@ unitTest({ perms: { write: true } }, async function removeAllFail(): Promise<
> { > {
for (const method of REMOVE_METHODS) { for (const method of REMOVE_METHODS) {
// NON-EXISTENT DIRECTORY/FILE // NON-EXISTENT DIRECTORY/FILE
let err; await assertThrowsAsync(async () => {
try {
// Non-existent // Non-existent
await Deno[method]("/baddir", { recursive: true }); await Deno[method]("/baddir", { recursive: true });
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
} }
}); });
@ -273,14 +230,9 @@ unitTest({ perms: { write: false } }, async function removeAllPerm(): Promise<
void void
> { > {
for (const method of REMOVE_METHODS) { for (const method of REMOVE_METHODS) {
let err; await assertThrowsAsync(async () => {
try {
await Deno[method]("/baddir", { recursive: true }); await Deno[method]("/baddir", { recursive: true });
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
} }
}); });
@ -298,13 +250,9 @@ unitTest(
Deno.statSync(path); // check if unix socket exists Deno.statSync(path); // check if unix socket exists
await Deno[method](path); await Deno[method](path);
let err; assertThrows(() => {
try {
Deno.statSync(path); Deno.statSync(path);
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
} }
} }
); );
@ -321,13 +269,9 @@ if (Deno.build.os === "windows") {
assert(await symlink.status()); assert(await symlink.status());
symlink.close(); symlink.close();
await Deno.remove("file_link"); await Deno.remove("file_link");
let err; await assertThrowsAsync(async () => {
try {
await Deno.lstat("file_link"); await Deno.lstat("file_link");
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
} }
); );
@ -343,13 +287,9 @@ if (Deno.build.os === "windows") {
symlink.close(); symlink.close();
await Deno.remove("dir_link"); await Deno.remove("dir_link");
let err; await assertThrowsAsync(async () => {
try {
await Deno.lstat("dir_link"); await Deno.lstat("dir_link");
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
} }
); );
} }

View file

@ -43,32 +43,22 @@ unitTest(
unitTest( unitTest(
{ perms: { read: false, write: true } }, { perms: { read: false, write: true } },
function renameSyncReadPerm(): void { function renameSyncReadPerm(): void {
let err; assertThrows(() => {
try {
const oldpath = "/oldbaddir"; const oldpath = "/oldbaddir";
const newpath = "/newbaddir"; const newpath = "/newbaddir";
Deno.renameSync(oldpath, newpath); Deno.renameSync(oldpath, newpath);
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
} }
); );
unitTest( unitTest(
{ perms: { read: true, write: false } }, { perms: { read: true, write: false } },
function renameSyncWritePerm(): void { function renameSyncWritePerm(): void {
let err; assertThrows(() => {
try {
const oldpath = "/oldbaddir"; const oldpath = "/oldbaddir";
const newpath = "/newbaddir"; const newpath = "/newbaddir";
Deno.renameSync(oldpath, newpath); Deno.renameSync(oldpath, newpath);
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
} }
); );

View file

@ -1,14 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assertEquals, assert } from "./test_util.ts"; import { unitTest, assertEquals, assert, assertThrows } from "./test_util.ts";
unitTest(function resourcesCloseBadArgs(): void { unitTest(function resourcesCloseBadArgs(): void {
let err; assertThrows(() => {
try {
Deno.close((null as unknown) as number); Deno.close((null as unknown) as number);
} catch (e) { }, Deno.errors.InvalidData);
err = e;
}
assert(err instanceof Deno.errors.InvalidData);
}); });
unitTest(function resourcesStdio(): void { unitTest(function resourcesStdio(): void {

View file

@ -3,6 +3,8 @@ import {
unitTest, unitTest,
assert, assert,
assertEquals, assertEquals,
assertThrows,
assertThrowsAsync,
pathToAbsoluteFileUrl, pathToAbsoluteFileUrl,
} from "./test_util.ts"; } from "./test_util.ts";
@ -98,29 +100,15 @@ unitTest(
); );
unitTest({ perms: { read: false } }, function statSyncPerm(): void { unitTest({ perms: { read: false } }, function statSyncPerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.statSync("README.md"); Deno.statSync("README.md");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function statSyncNotFound(): void { unitTest({ perms: { read: true } }, function statSyncNotFound(): void {
let caughtError = false; assertThrows(() => {
let badInfo; Deno.statSync("bad_file_name");
}, Deno.errors.NotFound);
try {
badInfo = Deno.statSync("bad_file_name");
} catch (err) {
caughtError = true;
assert(err instanceof Deno.errors.NotFound);
}
assert(caughtError);
assertEquals(badInfo, undefined);
}); });
unitTest({ perms: { read: true } }, function lstatSyncSuccess(): void { unitTest({ perms: { read: true } }, function lstatSyncSuccess(): void {
@ -152,29 +140,15 @@ unitTest({ perms: { read: true } }, function lstatSyncSuccess(): void {
}); });
unitTest({ perms: { read: false } }, function lstatSyncPerm(): void { unitTest({ perms: { read: false } }, function lstatSyncPerm(): void {
let caughtError = false; assertThrows(() => {
try {
Deno.lstatSync("README.md"); Deno.lstatSync("README.md");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, function lstatSyncNotFound(): void { unitTest({ perms: { read: true } }, function lstatSyncNotFound(): void {
let caughtError = false; assertThrows(() => {
let badInfo; Deno.lstatSync("bad_file_name");
}, Deno.errors.NotFound);
try {
badInfo = Deno.lstatSync("bad_file_name");
} catch (err) {
caughtError = true;
assert(err instanceof Deno.errors.NotFound);
}
assert(caughtError);
assertEquals(badInfo, undefined);
}); });
unitTest( unitTest(
@ -242,31 +216,19 @@ unitTest(
); );
unitTest({ perms: { read: false } }, async function statPerm(): Promise<void> { unitTest({ perms: { read: false } }, async function statPerm(): Promise<void> {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.stat("README.md"); await Deno.stat("README.md");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, async function statNotFound(): Promise< unitTest({ perms: { read: true } }, async function statNotFound(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(
let badInfo; async (): Promise<void> => {
await Deno.stat("bad_file_name"), Deno.errors.NotFound;
try { }
badInfo = await Deno.stat("bad_file_name"); );
} catch (err) {
caughtError = true;
assert(err instanceof Deno.errors.NotFound);
}
assert(caughtError);
assertEquals(badInfo, undefined);
}); });
unitTest({ perms: { read: true } }, async function lstatSuccess(): Promise< unitTest({ perms: { read: true } }, async function lstatSuccess(): Promise<
@ -300,31 +262,17 @@ unitTest({ perms: { read: true } }, async function lstatSuccess(): Promise<
}); });
unitTest({ perms: { read: false } }, async function lstatPerm(): Promise<void> { unitTest({ perms: { read: false } }, async function lstatPerm(): Promise<void> {
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.lstat("README.md"); await Deno.lstat("README.md");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest({ perms: { read: true } }, async function lstatNotFound(): Promise< unitTest({ perms: { read: true } }, async function lstatNotFound(): Promise<
void void
> { > {
let caughtError = false; await assertThrowsAsync(async () => {
let badInfo; await Deno.lstat("bad_file_name");
}, Deno.errors.NotFound);
try {
badInfo = await Deno.lstat("bad_file_name");
} catch (err) {
caughtError = true;
assert(err instanceof Deno.errors.NotFound);
}
assert(caughtError);
assertEquals(badInfo, undefined);
}); });
unitTest( unitTest(

View file

@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import { unitTest, assert, assertThrows } from "./test_util.ts";
unitTest( unitTest(
{ perms: { read: true, write: true } }, { perms: { read: true, write: true } },
@ -18,14 +18,9 @@ unitTest(
); );
unitTest(function symlinkSyncPerm(): void { unitTest(function symlinkSyncPerm(): void {
let err; assertThrows(() => {
try {
Deno.symlinkSync("oldbaddir", "newbaddir"); Deno.symlinkSync("oldbaddir", "newbaddir");
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest( unitTest(

View file

@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import { unitTest, assert, assertEquals, assertThrows } from "./test_util.ts";
unitTest(function btoaSuccess(): void { unitTest(function btoaSuccess(): void {
const text = "hello world"; const text = "hello world";
@ -52,14 +52,9 @@ unitTest(function atobThrows2(): void {
unitTest(function btoaFailed(): void { unitTest(function btoaFailed(): void {
const text = "你好"; const text = "你好";
let err; assertThrows(() => {
try {
btoa(text); btoa(text);
} catch (e) { }, TypeError);
err = e;
}
assert(!!err);
assert(err instanceof TypeError);
}); });
unitTest(function textDecoder2(): void { unitTest(function textDecoder2(): void {

View file

@ -2,6 +2,8 @@
import { import {
assert, assert,
assertEquals, assertEquals,
assertThrows,
assertThrowsAsync,
createResolvable, createResolvable,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
@ -12,35 +14,24 @@ const encoder = new TextEncoder();
const decoder = new TextDecoder(); const decoder = new TextDecoder();
unitTest(async function connectTLSNoPerm(): Promise<void> { unitTest(async function connectTLSNoPerm(): Promise<void> {
let err; await assertThrowsAsync(async () => {
try {
await Deno.connectTls({ hostname: "github.com", port: 443 }); await Deno.connectTls({ hostname: "github.com", port: 443 });
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest(async function connectTLSCertFileNoReadPerm(): Promise<void> { unitTest(async function connectTLSCertFileNoReadPerm(): Promise<void> {
let err; await assertThrowsAsync(async () => {
try {
await Deno.connectTls({ await Deno.connectTls({
hostname: "github.com", hostname: "github.com",
port: 443, port: 443,
certFile: "cli/tests/tls/RootCA.crt", certFile: "cli/tests/tls/RootCA.crt",
}); });
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest( unitTest(
{ perms: { read: true, net: true } }, { perms: { read: true, net: true } },
function listenTLSNonExistentCertKeyFiles(): void { function listenTLSNonExistentCertKeyFiles(): void {
let err;
const options = { const options = {
hostname: "localhost", hostname: "localhost",
port: 3500, port: 3500,
@ -48,42 +39,31 @@ unitTest(
keyFile: "cli/tests/tls/localhost.key", keyFile: "cli/tests/tls/localhost.key",
}; };
try { assertThrows(() => {
Deno.listenTls({ Deno.listenTls({
...options, ...options,
certFile: "./non/existent/file", certFile: "./non/existent/file",
}); });
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
try { assertThrows(() => {
Deno.listenTls({ Deno.listenTls({
...options, ...options,
keyFile: "./non/existent/file", keyFile: "./non/existent/file",
}); });
} catch (e) { }, Deno.errors.NotFound);
err = e;
}
assert(err instanceof Deno.errors.NotFound);
} }
); );
unitTest({ perms: { net: true } }, function listenTLSNoReadPerm(): void { unitTest({ perms: { net: true } }, function listenTLSNoReadPerm(): void {
let err; assertThrows(() => {
try {
Deno.listenTls({ Deno.listenTls({
hostname: "localhost", hostname: "localhost",
port: 3500, port: 3500,
certFile: "cli/tests/tls/localhost.crt", certFile: "cli/tests/tls/localhost.crt",
keyFile: "cli/tests/tls/localhost.key", keyFile: "cli/tests/tls/localhost.key",
}); });
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest( unitTest(
@ -91,7 +71,6 @@ unitTest(
perms: { read: true, write: true, net: true }, perms: { read: true, write: true, net: true },
}, },
function listenTLSEmptyKeyFile(): void { function listenTLSEmptyKeyFile(): void {
let err;
const options = { const options = {
hostname: "localhost", hostname: "localhost",
port: 3500, port: 3500,
@ -105,22 +84,18 @@ unitTest(
mode: 0o666, mode: 0o666,
}); });
try { assertThrows(() => {
Deno.listenTls({ Deno.listenTls({
...options, ...options,
keyFile: keyFilename, keyFile: keyFilename,
}); });
} catch (e) { }, Error);
err = e;
}
assert(err instanceof Error);
} }
); );
unitTest( unitTest(
{ perms: { read: true, write: true, net: true } }, { perms: { read: true, write: true, net: true } },
function listenTLSEmptyCertFile(): void { function listenTLSEmptyCertFile(): void {
let err;
const options = { const options = {
hostname: "localhost", hostname: "localhost",
port: 3500, port: 3500,
@ -134,15 +109,12 @@ unitTest(
mode: 0o666, mode: 0o666,
}); });
try { assertThrows(() => {
Deno.listenTls({ Deno.listenTls({
...options, ...options,
certFile: certFilename, certFile: certFilename,
}); });
} catch (e) { }, Error);
err = e;
}
assert(err instanceof Error);
} }
); );

View file

@ -1,5 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assertEquals, assert } from "./test_util.ts"; import {
unitTest,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
unitTest( unitTest(
{ perms: { read: true, write: true } }, { perms: { read: true, write: true } },
@ -76,25 +81,15 @@ unitTest(
); );
unitTest({ perms: { write: false } }, function truncateSyncPerm(): void { unitTest({ perms: { write: false } }, function truncateSyncPerm(): void {
let err; assertThrows(() => {
try {
Deno.truncateSync("/test_truncateSyncPermission.txt"); Deno.truncateSync("/test_truncateSyncPermission.txt");
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });
unitTest({ perms: { write: false } }, async function truncatePerm(): Promise< unitTest({ perms: { write: false } }, async function truncatePerm(): Promise<
void void
> { > {
let err; await assertThrowsAsync(async () => {
try {
await Deno.truncate("/test_truncatePermission.txt"); await Deno.truncate("/test_truncatePermission.txt");
} catch (e) { }, Deno.errors.PermissionDenied);
err = e;
}
assert(err instanceof Deno.errors.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}); });

View file

@ -1,5 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert } from "./test_util.ts"; import {
unitTest,
assert,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
// Allow 10 second difference. // Allow 10 second difference.
// Note this might not be enough for FAT (but we are not testing on such fs). // Note this might not be enough for FAT (but we are not testing on such fs).
@ -99,14 +104,9 @@ unitTest(
const atime = 1000; const atime = 1000;
const mtime = 50000; const mtime = 50000;
let caughtError = false; assertThrows(() => {
try {
Deno.utimeSync("/baddir", atime, mtime); Deno.utimeSync("/baddir", atime, mtime);
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
} }
); );
@ -116,14 +116,9 @@ unitTest(
const atime = 1000; const atime = 1000;
const mtime = 50000; const mtime = 50000;
let caughtError = false; assertThrows(() => {
try {
Deno.utimeSync("/some_dir", atime, mtime); Deno.utimeSync("/some_dir", atime, mtime);
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
} }
); );
@ -201,14 +196,9 @@ unitTest(
const atime = 1000; const atime = 1000;
const mtime = 50000; const mtime = 50000;
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.utime("/baddir", atime, mtime); await Deno.utime("/baddir", atime, mtime);
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
} }
); );
@ -218,13 +208,8 @@ unitTest(
const atime = 1000; const atime = 1000;
const mtime = 50000; const mtime = 50000;
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.utime("/some_dir", atime, mtime); await Deno.utime("/some_dir", atime, mtime);
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
} }
); );

View file

@ -1,5 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import {
unitTest,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
unitTest( unitTest(
{ perms: { read: true, write: true } }, { perms: { read: true, write: true } },
@ -39,14 +44,9 @@ unitTest({ perms: { write: true } }, function writeFileSyncFail(): void {
const data = enc.encode("Hello"); const data = enc.encode("Hello");
const filename = "/baddir/test.txt"; const filename = "/baddir/test.txt";
// The following should fail because /baddir doesn't exist (hopefully). // The following should fail because /baddir doesn't exist (hopefully).
let caughtError = false; assertThrows(() => {
try {
Deno.writeFileSync(filename, data); Deno.writeFileSync(filename, data);
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
}); });
unitTest({ perms: { write: false } }, function writeFileSyncPerm(): void { unitTest({ perms: { write: false } }, function writeFileSyncPerm(): void {
@ -54,14 +54,9 @@ unitTest({ perms: { write: false } }, function writeFileSyncPerm(): void {
const data = enc.encode("Hello"); const data = enc.encode("Hello");
const filename = "/baddir/test.txt"; const filename = "/baddir/test.txt";
// The following should fail due to no write permission // The following should fail due to no write permission
let caughtError = false; assertThrows(() => {
try {
Deno.writeFileSync(filename, data); Deno.writeFileSync(filename, data);
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest( unitTest(
@ -85,15 +80,10 @@ unitTest(
const enc = new TextEncoder(); const enc = new TextEncoder();
const data = enc.encode("Hello"); const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt"; const filename = Deno.makeTempDirSync() + "/test.txt";
let caughtError = false;
// if create turned off, the file won't be created // if create turned off, the file won't be created
try { assertThrows(() => {
Deno.writeFileSync(filename, data, { create: false }); Deno.writeFileSync(filename, data, { create: false });
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
// Turn on create, should have no error // Turn on create, should have no error
Deno.writeFileSync(filename, data, { create: true }); Deno.writeFileSync(filename, data, { create: true });
@ -170,14 +160,9 @@ unitTest(
const data = enc.encode("Hello"); const data = enc.encode("Hello");
const filename = "/baddir/test.txt"; const filename = "/baddir/test.txt";
// The following should fail because /baddir doesn't exist (hopefully). // The following should fail because /baddir doesn't exist (hopefully).
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.writeFile(filename, data); await Deno.writeFile(filename, data);
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
} }
); );
@ -188,14 +173,9 @@ unitTest(
const data = enc.encode("Hello"); const data = enc.encode("Hello");
const filename = "/baddir/test.txt"; const filename = "/baddir/test.txt";
// The following should fail due to no write permission // The following should fail due to no write permission
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.writeFile(filename, data); await Deno.writeFile(filename, data);
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
} }
); );
@ -220,15 +200,10 @@ unitTest(
const enc = new TextEncoder(); const enc = new TextEncoder();
const data = enc.encode("Hello"); const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt"; const filename = Deno.makeTempDirSync() + "/test.txt";
let caughtError = false;
// if create turned off, the file won't be created // if create turned off, the file won't be created
try { await assertThrowsAsync(async () => {
await Deno.writeFile(filename, data, { create: false }); await Deno.writeFile(filename, data, { create: false });
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
// Turn on create, should have no error // Turn on create, should have no error
await Deno.writeFile(filename, data, { create: true }); await Deno.writeFile(filename, data, { create: true });

View file

@ -1,4 +1,9 @@
import { unitTest, assert, assertEquals } from "./test_util.ts"; import {
unitTest,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "./test_util.ts";
unitTest( unitTest(
{ perms: { read: true, write: true } }, { perms: { read: true, write: true } },
@ -28,27 +33,17 @@ unitTest(
unitTest({ perms: { write: true } }, function writeTextFileSyncFail(): void { unitTest({ perms: { write: true } }, function writeTextFileSyncFail(): void {
const filename = "/baddir/test.txt"; const filename = "/baddir/test.txt";
// The following should fail because /baddir doesn't exist (hopefully). // The following should fail because /baddir doesn't exist (hopefully).
let caughtError = false; assertThrows(() => {
try {
Deno.writeTextFileSync(filename, "hello"); Deno.writeTextFileSync(filename, "hello");
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
}); });
unitTest({ perms: { write: false } }, function writeTextFileSyncPerm(): void { unitTest({ perms: { write: false } }, function writeTextFileSyncPerm(): void {
const filename = "/baddir/test.txt"; const filename = "/baddir/test.txt";
// The following should fail due to no write permission // The following should fail due to no write permission
let caughtError = false; assertThrows(() => {
try {
Deno.writeTextFileSync(filename, "Hello"); Deno.writeTextFileSync(filename, "Hello");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
}); });
unitTest( unitTest(
@ -81,14 +76,9 @@ unitTest(
async function writeTextFileNotFound(): Promise<void> { async function writeTextFileNotFound(): Promise<void> {
const filename = "/baddir/test.txt"; const filename = "/baddir/test.txt";
// The following should fail because /baddir doesn't exist (hopefully). // The following should fail because /baddir doesn't exist (hopefully).
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.writeTextFile(filename, "Hello"); await Deno.writeTextFile(filename, "Hello");
} catch (e) { }, Deno.errors.NotFound);
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
} }
); );
@ -97,13 +87,8 @@ unitTest(
async function writeTextFilePerm(): Promise<void> { async function writeTextFilePerm(): Promise<void> {
const filename = "/baddir/test.txt"; const filename = "/baddir/test.txt";
// The following should fail due to no write permission // The following should fail due to no write permission
let caughtError = false; await assertThrowsAsync(async () => {
try {
await Deno.writeTextFile(filename, "Hello"); await Deno.writeTextFile(filename, "Hello");
} catch (e) { }, Deno.errors.PermissionDenied);
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
} }
); );