diff options
Diffstat (limited to 'cli/tests/unit')
-rw-r--r-- | cli/tests/unit/read_file_test.ts | 46 | ||||
-rw-r--r-- | cli/tests/unit/read_text_file_test.ts | 46 | ||||
-rw-r--r-- | cli/tests/unit/write_file_test.ts | 83 |
3 files changed, 171 insertions, 4 deletions
diff --git a/cli/tests/unit/read_file_test.ts b/cli/tests/unit/read_file_test.ts index df3161f0b..4b1ebef0b 100644 --- a/cli/tests/unit/read_file_test.ts +++ b/cli/tests/unit/read_file_test.ts @@ -6,6 +6,7 @@ import { assertRejects, assertThrows, pathToAbsoluteFileUrl, + unreachable, } from "./test_util.ts"; Deno.test({ permissions: { read: true } }, function readFileSyncSuccess() { @@ -95,11 +96,52 @@ Deno.test( async function readFileWithAbortSignal() { const ac = new AbortController(); queueMicrotask(() => ac.abort()); - await assertRejects(async () => { + await assertRejects( + async () => { + await Deno.readFile("cli/tests/testdata/fixture.json", { + signal: ac.signal, + }); + }, + (error: Error) => { + assert(error instanceof DOMException); + assertEquals(error.name, "AbortError"); + }, + ); + }, +); + +Deno.test( + { permissions: { read: true } }, + async function readFileWithAbortSignalReason() { + const ac = new AbortController(); + const abortReason = new Error(); + queueMicrotask(() => ac.abort(abortReason)); + await assertRejects( + async () => { + await Deno.readFile("cli/tests/testdata/fixture.json", { + signal: ac.signal, + }); + }, + (error: Error) => { + assertEquals(error, abortReason); + }, + ); + }, +); + +Deno.test( + { permissions: { read: true } }, + async function readFileWithAbortSignalPrimitiveReason() { + const ac = new AbortController(); + queueMicrotask(() => ac.abort("Some string")); + try { await Deno.readFile("cli/tests/testdata/fixture.json", { signal: ac.signal, }); - }); + unreachable(); + } catch (e) { + assertEquals(e, "Some string"); + } }, ); diff --git a/cli/tests/unit/read_text_file_test.ts b/cli/tests/unit/read_text_file_test.ts index 6e7dce73e..169972cb4 100644 --- a/cli/tests/unit/read_text_file_test.ts +++ b/cli/tests/unit/read_text_file_test.ts @@ -4,6 +4,7 @@ import { assertRejects, assertThrows, pathToAbsoluteFileUrl, + unreachable, } from "./test_util.ts"; Deno.test({ permissions: { read: true } }, function readTextFileSyncSuccess() { @@ -88,11 +89,52 @@ Deno.test( async function readTextFileWithAbortSignal() { const ac = new AbortController(); queueMicrotask(() => ac.abort()); - await assertRejects(async () => { + await assertRejects( + async () => { + await Deno.readFile("cli/tests/testdata/fixture.json", { + signal: ac.signal, + }); + }, + (error: Error) => { + assert(error instanceof DOMException); + assertEquals(error.name, "AbortError"); + }, + ); + }, +); + +Deno.test( + { permissions: { read: true } }, + async function readTextFileWithAbortSignalReason() { + const ac = new AbortController(); + const abortReason = new Error(); + queueMicrotask(() => ac.abort(abortReason)); + await assertRejects( + async () => { + await Deno.readFile("cli/tests/testdata/fixture.json", { + signal: ac.signal, + }); + }, + (error: Error) => { + assertEquals(error, abortReason); + }, + ); + }, +); + +Deno.test( + { permissions: { read: true } }, + async function readTextFileWithAbortSignalPrimitiveReason() { + const ac = new AbortController(); + queueMicrotask(() => ac.abort("Some string")); + try { await Deno.readFile("cli/tests/testdata/fixture.json", { signal: ac.signal, }); - }); + unreachable(); + } catch (e) { + assertEquals(e, "Some string"); + } }, ); diff --git a/cli/tests/unit/write_file_test.ts b/cli/tests/unit/write_file_test.ts index 66f07b9b1..b6f7d2cee 100644 --- a/cli/tests/unit/write_file_test.ts +++ b/cli/tests/unit/write_file_test.ts @@ -4,6 +4,7 @@ import { assertEquals, assertRejects, assertThrows, + unreachable, } from "./test_util.ts"; Deno.test( @@ -250,6 +251,7 @@ Deno.test( queueMicrotask(() => ac.abort()); try { await Deno.writeFile(filename, data, { signal: ac.signal }); + unreachable(); } catch (e) { assert(e instanceof Error); assertEquals(e.name, "AbortError"); @@ -261,6 +263,45 @@ Deno.test( Deno.test( { permissions: { read: true, write: true } }, + async function writeFileAbortSignalReason(): Promise<void> { + const ac = new AbortController(); + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + const abortReason = new Error(); + queueMicrotask(() => ac.abort(abortReason)); + try { + await Deno.writeFile(filename, data, { signal: ac.signal }); + unreachable(); + } catch (e) { + assertEquals(e, abortReason); + } + const stat = Deno.statSync(filename); + assertEquals(stat.size, 0); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + async function writeFileAbortSignalPrimitiveReason(): Promise<void> { + const ac = new AbortController(); + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + queueMicrotask(() => ac.abort("Some string")); + try { + await Deno.writeFile(filename, data, { signal: ac.signal }); + unreachable(); + } catch (e) { + assertEquals(e, "Some string"); + } + const stat = Deno.statSync(filename); + assertEquals(stat.size, 0); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, async function writeFileAbortSignalPreAborted(): Promise<void> { const ac = new AbortController(); ac.abort(); @@ -269,6 +310,7 @@ Deno.test( const filename = Deno.makeTempDirSync() + "/test.txt"; try { await Deno.writeFile(filename, data, { signal: ac.signal }); + unreachable(); } catch (e) { assert(e instanceof Error); assertEquals(e.name, "AbortError"); @@ -277,3 +319,44 @@ Deno.test( assertEquals(stat.size, 0); }, ); + +Deno.test( + { permissions: { read: true, write: true } }, + async function writeFileAbortSignalReasonPreAborted(): Promise<void> { + const ac = new AbortController(); + const abortReason = new Error(); + ac.abort(abortReason); + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + try { + await Deno.writeFile(filename, data, { signal: ac.signal }); + unreachable(); + } catch (e) { + assertEquals(e, abortReason); + } + const stat = Deno.statSync(filename); + assertEquals(stat.size, 0); + }, +); + +Deno.test( + { permissions: { read: true, write: true } }, + async function writeFileAbortSignalPrimitiveReasonPreAborted(): Promise< + void + > { + const ac = new AbortController(); + ac.abort("Some string"); + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + try { + await Deno.writeFile(filename, data, { signal: ac.signal }); + unreachable(); + } catch (e) { + assertEquals(e, "Some string"); + } + const stat = Deno.statSync(filename); + assertEquals(stat.size, 0); + }, +); |