2023-01-13 02:51:32 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
import {
|
2020-07-08 09:38:22 -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,
|
|
|
|
} from "./test_util.ts";
|
2020-04-28 01:35: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
|
|
|
function writeTextFileSyncSuccess() {
|
2020-04-28 01:35:20 -04:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeTextFileSync(filename, "Hello");
|
|
|
|
const dataRead = Deno.readTextFileSync(filename);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(dataRead, "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-28 01:35: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
|
|
|
function writeTextFileSyncByUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
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.writeTextFileSync(fileUrl, "Hello");
|
|
|
|
const dataRead = Deno.readTextFileSync(fileUrl);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(dataRead, "Hello");
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(fileUrl, { 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 writeTextFileSyncFail() {
|
2020-04-28 01:35:20 -04:00
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail because /baddir doesn't exist (hopefully).
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-04-28 01:35:20 -04:00
|
|
|
Deno.writeTextFileSync(filename, "hello");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-04-28 01:35:20 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { write: false } }, function writeTextFileSyncPerm() {
|
2020-04-28 01:35:20 -04:00
|
|
|
const filename = "/baddir/test.txt";
|
|
|
|
// The following should fail due to no write permission
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-04-28 01:35:20 -04:00
|
|
|
Deno.writeTextFileSync(filename, "Hello");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-04-28 01:35: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
|
|
|
function writeTextFileSyncUpdateMode() {
|
2020-07-08 09:38:22 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
|
|
|
const data = "Hello";
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeTextFileSync(filename, data, { mode: 0o755 });
|
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
|
|
|
Deno.writeTextFileSync(filename, data, { mode: 0o666 });
|
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-08 09:38:22 -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 writeTextFileSyncCreate() {
|
2020-07-08 09:38:22 -04:00
|
|
|
const data = "Hello";
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
let caughtError = false;
|
|
|
|
// if create turned off, the file won't be created
|
|
|
|
try {
|
|
|
|
Deno.writeTextFileSync(filename, data, { create: false });
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assert(e instanceof Deno.errors.NotFound);
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
|
|
|
|
// Turn on create, should have no error
|
|
|
|
Deno.writeTextFileSync(filename, data, { create: true });
|
|
|
|
Deno.writeTextFileSync(filename, data, { create: false });
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-08 09:38:22 -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 writeTextFileSyncAppend() {
|
2020-07-08 09:38:22 -04:00
|
|
|
const data = "Hello";
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeTextFileSync(filename, data);
|
|
|
|
Deno.writeTextFileSync(filename, data, { append: true });
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(Deno.readTextFileSync(filename), "HelloHello");
|
2020-07-08 09:38:22 -04:00
|
|
|
// Now attempt overwrite
|
|
|
|
Deno.writeTextFileSync(filename, data, { append: false });
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
2020-07-08 09:38:22 -04:00
|
|
|
// append not set should also overwrite
|
|
|
|
Deno.writeTextFileSync(filename, data);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-08 09:38:22 -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 writeTextFileSuccess() {
|
2020-04-28 01:35:20 -04:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeTextFile(filename, "Hello");
|
|
|
|
const dataRead = Deno.readTextFileSync(filename);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(dataRead, "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-28 01:35: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 writeTextFileByUrl() {
|
2020-06-11 12:36:20 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
await Deno.writeTextFile(fileUrl, "Hello");
|
|
|
|
const dataRead = Deno.readTextFileSync(fileUrl);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(dataRead, "Hello");
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
Deno.removeSync(fileUrl, { 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 writeTextFileNotFound() {
|
2020-04-28 01:35:20 -04:00
|
|
|
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 () => {
|
2020-04-28 01:35:20 -04:00
|
|
|
await Deno.writeTextFile(filename, "Hello");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-28 01:35:20 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { write: false } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function writeTextFilePerm() {
|
2020-04-28 01:35:20 -04:00
|
|
|
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-04-28 01:35:20 -04:00
|
|
|
await Deno.writeTextFile(filename, "Hello");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-28 01:35:20 -04:00
|
|
|
);
|
2020-07-08 09:38:22 -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 writeTextFileUpdateMode() {
|
2020-07-08 09:38:22 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
|
|
|
const data = "Hello";
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeTextFile(filename, data, { mode: 0o755 });
|
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
|
|
|
|
await Deno.writeTextFile(filename, data, { mode: 0o666 });
|
|
|
|
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-08 09:38:22 -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 writeTextFileCreate() {
|
2020-07-08 09:38:22 -04:00
|
|
|
const data = "Hello";
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
let caughtError = false;
|
|
|
|
// if create turned off, the file won't be created
|
|
|
|
try {
|
|
|
|
await Deno.writeTextFile(filename, data, { create: false });
|
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
|
|
|
assert(e instanceof Deno.errors.NotFound);
|
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
|
|
|
|
// Turn on create, should have no error
|
|
|
|
await Deno.writeTextFile(filename, data, { create: true });
|
|
|
|
await Deno.writeTextFile(filename, data, { create: false });
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-08 09:38:22 -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 writeTextFileAppend() {
|
2020-07-08 09:38:22 -04:00
|
|
|
const data = "Hello";
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeTextFile(filename, data);
|
|
|
|
await Deno.writeTextFile(filename, data, { append: true });
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(Deno.readTextFileSync(filename), "HelloHello");
|
2020-07-08 09:38:22 -04:00
|
|
|
// Now attempt overwrite
|
|
|
|
await Deno.writeTextFile(filename, data, { append: false });
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
2020-07-08 09:38:22 -04:00
|
|
|
// append not set should also overwrite
|
|
|
|
await Deno.writeTextFile(filename, data);
|
2022-04-18 18:00:14 -04:00
|
|
|
assertEquals(Deno.readTextFileSync(filename), "Hello");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-08 09:38:22 -04:00
|
|
|
);
|
2023-01-11 21:37:23 -05:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, write: true } },
|
|
|
|
async function writeTextFileStream() {
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
pull(controller) {
|
|
|
|
controller.enqueue("Hello");
|
|
|
|
controller.enqueue("World");
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
await Deno.writeTextFile(filename, stream);
|
|
|
|
assertEquals(Deno.readTextFileSync(filename), "HelloWorld");
|
|
|
|
},
|
|
|
|
);
|