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

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