2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-06-24 18:57:08 -04:00
|
|
|
import {
|
2021-10-27 17:43:40 -04:00
|
|
|
assert,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows,
|
2021-12-16 06:57:26 -05:00
|
|
|
unreachable,
|
2020-06-24 18:57:08 -04:00
|
|
|
} from "./test_util.ts";
|
2018-09-11 12:00:57 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function writeFileSyncSuccess() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeFileSync(filename, data);
|
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-09-11 12:00:57 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function writeFileSyncUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const fileUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
Deno.writeFileSync(fileUrl, data);
|
|
|
|
const dataRead = Deno.readFileSync(fileUrl);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { write: true } }, function writeFileSyncFail() {
|
2018-09-11 12:00:57 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail because /baddir doesn't exist (hopefully).
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2018-09-11 12:00:57 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { write: false } }, function writeFileSyncPerm() {
|
2018-09-11 12:00:57 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail due to no write permission
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data);
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2018-09-11 12:00:57 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function writeFileSyncUpdateMode() {
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o755 });
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
2020-03-07 22:29:12 -05:00
|
|
|
Deno.writeFileSync(filename, data, { mode: 0o666 });
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function writeFileSyncCreate() {
|
2019-02-02 14:26:18 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2020-03-04 11:31:14 -05:00
|
|
|
// if create turned off, the file won't be created
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.writeFileSync(filename, data, { create: false });
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// Turn on create, should have no error
|
|
|
|
Deno.writeFileSync(filename, data, { create: true });
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.writeFileSync(filename, data, { create: false });
|
2020-03-04 11:31:14 -05:00
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2022-12-12 23:12:49 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
function writeFileSyncCreateNew() {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeFileSync(filename, data, { createNew: true });
|
|
|
|
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.writeFileSync(filename, data, { createNew: true });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function writeFileSyncAppend() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeFileSync(filename, data);
|
|
|
|
Deno.writeFileSync(filename, data, { append: true });
|
|
|
|
let dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
let actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "HelloHello");
|
2020-03-04 11:31:14 -05:00
|
|
|
// Now attempt overwrite
|
|
|
|
Deno.writeFileSync(filename, data, { append: false });
|
|
|
|
dataRead = Deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-03-04 11:31:14 -05:00
|
|
|
// append not set should also overwrite
|
|
|
|
Deno.writeFileSync(filename, data);
|
|
|
|
dataRead = Deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeFileSuccess() {
|
2019-04-21 16:40:10 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.writeFile(filename, data);
|
2019-04-21 16:40:10 -04:00
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeFileUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
const fileUrl = new URL(
|
2020-07-14 15:24:17 -04:00
|
|
|
`file://${Deno.build.os === "windows" ? "/" : ""}${tempDir}/test.txt`,
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
await Deno.writeFile(fileUrl, data);
|
|
|
|
const dataRead = Deno.readFileSync(fileUrl);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(tempDir, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-11 12:36:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeFileNotFound() {
|
2019-04-21 16:40:10 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail because /baddir doesn't exist (hopefully).
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2019-04-21 16:40:10 -04:00
|
|
|
await Deno.writeFile(filename, data);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2018-09-11 12:00:57 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: false } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeFilePerm() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail due to no write permission
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.writeFile(filename, data);
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeFileUpdateMode() {
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2019-04-21 16:40:10 -04:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
2020-03-07 22:29:12 -05:00
|
|
|
await Deno.writeFile(filename, data, { mode: 0o755 });
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
2020-03-07 22:29:12 -05:00
|
|
|
await Deno.writeFile(filename, data, { mode: 0o666 });
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
2019-04-21 16:40:10 -04:00
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeFileCreate() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
// if create turned off, the file won't be created
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-03-04 11:31:14 -05:00
|
|
|
await Deno.writeFile(filename, data, { create: false });
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-03-04 11:31:14 -05:00
|
|
|
|
|
|
|
// Turn on create, should have no error
|
|
|
|
await Deno.writeFile(filename, data, { create: true });
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.writeFile(filename, data, { create: false });
|
2020-03-04 11:31:14 -05:00
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-02-02 14:26:18 -05:00
|
|
|
|
2022-12-12 23:12:49 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function writeFileCreateNew() {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeFile(filename, data, { createNew: true });
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.writeFile(filename, data, { createNew: true });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeFileAppend() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeFile(filename, data);
|
|
|
|
await Deno.writeFile(filename, data, { append: true });
|
|
|
|
let dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
let actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "HelloHello");
|
2020-03-04 11:31:14 -05:00
|
|
|
// Now attempt overwrite
|
|
|
|
await Deno.writeFile(filename, data, { append: false });
|
|
|
|
dataRead = Deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-03-04 11:31:14 -05:00
|
|
|
// append not set should also overwrite
|
|
|
|
await Deno.writeFile(filename, data);
|
|
|
|
dataRead = Deno.readFileSync(filename);
|
|
|
|
actual = dec.decode(dataRead);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(actual, "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2021-08-06 13:21:29 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-06 13:21:29 -04:00
|
|
|
async function writeFileAbortSignal(): Promise<void> {
|
|
|
|
const ac = new AbortController();
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
queueMicrotask(() => ac.abort());
|
|
|
|
try {
|
|
|
|
await Deno.writeFile(filename, data, { signal: ac.signal });
|
2021-12-16 06:57:26 -05:00
|
|
|
unreachable();
|
2021-08-06 13:21:29 -04:00
|
|
|
} catch (e) {
|
2021-10-27 17:43:40 -04:00
|
|
|
assert(e instanceof Error);
|
2021-08-06 13:21:29 -04:00
|
|
|
assertEquals(e.name, "AbortError");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-12-16 06:57:26 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-06 13:21:29 -04:00
|
|
|
async function writeFileAbortSignalPreAborted(): Promise<void> {
|
|
|
|
const ac = new AbortController();
|
|
|
|
ac.abort();
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
try {
|
|
|
|
await Deno.writeFile(filename, data, { signal: ac.signal });
|
2021-12-16 06:57:26 -05:00
|
|
|
unreachable();
|
2021-08-06 13:21:29 -04:00
|
|
|
} catch (e) {
|
2021-10-27 17:43:40 -04:00
|
|
|
assert(e instanceof Error);
|
2021-08-06 13:21:29 -04:00
|
|
|
assertEquals(e.name, "AbortError");
|
|
|
|
}
|
2022-04-18 18:00:14 -04:00
|
|
|
assertNotExists(filename);
|
2021-08-06 13:21:29 -04:00
|
|
|
},
|
|
|
|
);
|
2021-12-16 06:57:26 -05:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-04-18 18:00:14 -04:00
|
|
|
assertNotExists(filename);
|
2021-12-16 06:57:26 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2022-04-18 18:00:14 -04:00
|
|
|
assertNotExists(filename);
|
2021-12-16 06:57:26 -05:00
|
|
|
},
|
|
|
|
);
|
2022-04-18 18:00:14 -04:00
|
|
|
|
2023-02-11 07:19:13 -05:00
|
|
|
// Test that AbortController's cancel handle is cleaned-up correctly, and do not leak resources.
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function writeFileWithAbortSignalNotCalled() {
|
|
|
|
const ac = new AbortController();
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeFile(filename, data, { signal: ac.signal });
|
|
|
|
const dataRead = Deno.readFileSync(filename);
|
|
|
|
const dec = new TextDecoder("utf-8");
|
|
|
|
const actual = dec.decode(dataRead);
|
|
|
|
assertEquals(actual, "Hello");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-04-18 18:00:14 -04:00
|
|
|
function assertNotExists(filename: string | URL) {
|
|
|
|
if (pathExists(filename)) {
|
|
|
|
throw new Error(`The file ${filename} exists.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function pathExists(path: string | URL) {
|
|
|
|
try {
|
|
|
|
Deno.statSync(path);
|
|
|
|
return true;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 21:37:23 -05:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function writeFileStream() {
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
pull(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([1]));
|
|
|
|
controller.enqueue(new Uint8Array([2]));
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeFile(filename, stream);
|
|
|
|
assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2]));
|
|
|
|
},
|
|
|
|
);
|
2024-05-27 18:14:35 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function overwriteFileWithStream() {
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeFile(filename, new Uint8Array([1, 2, 3, 4]));
|
|
|
|
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
pull(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([1]));
|
|
|
|
controller.enqueue(new Uint8Array([2]));
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await Deno.writeFile(filename, stream);
|
|
|
|
assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2]));
|
|
|
|
},
|
|
|
|
);
|