fix(std/node): Stop callbacks being called twice when callback throws error (#8867)

This commit is contained in:
Liam Murphy 2021-01-26 23:34:40 +11:00 committed by GitHub
parent f9949a3170
commit 06bd692e5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
46 changed files with 603 additions and 178 deletions

View file

@ -2,6 +2,7 @@
import { assertEquals, assertThrows, fail } from "../../testing/asserts.ts";
import { appendFile, appendFileSync } from "./_fs_appendFile.ts";
import { fromFileUrl } from "../path.ts";
import { assertCallbackErrorUncaught } from "../_utils.ts";
const decoder = new TextDecoder("utf-8");
@ -78,8 +79,7 @@ Deno.test({
.then(async () => {
const data = await Deno.readFile(tempFile);
assertEquals(decoder.decode(data), "hello world");
})
.catch(() => {
}, () => {
fail("No error expected");
})
.finally(async () => {
@ -103,8 +103,7 @@ Deno.test({
assertEquals(Deno.resources(), openResourcesBeforeAppend);
const data = await Deno.readFile("_fs_appendFile_test_file.txt");
assertEquals(decoder.decode(data), "hello world");
})
.catch((err) => {
}, (err) => {
fail("No error was expected: " + err);
})
.finally(async () => {
@ -128,8 +127,7 @@ Deno.test({
assertEquals(Deno.resources(), openResourcesBeforeAppend);
const data = await Deno.readFile(fromFileUrl(fileURL));
assertEquals(decoder.decode(data), "hello world");
})
.catch((err) => {
}, (err) => {
fail("No error was expected: " + err);
})
.finally(async () => {
@ -152,8 +150,7 @@ Deno.test({
})
.then(() => {
fail("Expected error to be thrown");
})
.catch(() => {
}, () => {
assertEquals(Deno.resources(), openResourcesBeforeAppend);
})
.finally(async () => {
@ -235,8 +232,7 @@ Deno.test({
assertEquals(Deno.resources(), openResourcesBeforeAppend);
const data = await Deno.readFile("_fs_appendFile_test_file.txt");
assertEquals(data, testData);
})
.catch((err) => {
}, (err) => {
fail("No error was expected: " + err);
})
.finally(async () => {
@ -244,3 +240,15 @@ Deno.test({
});
},
});
Deno.test("[std/node/fs] appendFile callback isn't called twice if error is thrown", async () => {
const tempFile = await Deno.makeTempFile();
const importUrl = new URL("./_fs_appendFile.ts", import.meta.url);
await assertCallbackErrorUncaught({
prelude: `import { appendFile } from ${JSON.stringify(importUrl)}`,
invocation: `appendFile(${JSON.stringify(tempFile)}, "hello world", `,
async cleanup() {
await Deno.remove(tempFile);
},
});
});