summaryrefslogtreecommitdiff
path: root/cli/tests/unit/write_file_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests/unit/write_file_test.ts')
-rw-r--r--cli/tests/unit/write_file_test.ts83
1 files changed, 83 insertions, 0 deletions
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);
+ },
+);