2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-02-07 11:51:28 -05:00
|
|
|
import { assertEquals, assertThrows, fail } from "@test_util/std/assert/mod.ts";
|
2023-03-19 12:10:39 -04:00
|
|
|
import { appendFile, appendFileSync } from "node:fs";
|
2024-02-07 11:51:28 -05:00
|
|
|
import { fromFileUrl } from "@test_util/std/path/mod.ts";
|
2023-03-19 12:10:39 -04:00
|
|
|
import { assertCallbackErrorUncaught } from "../_test_utils.ts";
|
|
|
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "No callback Fn results in Error",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'NoParamCallback'
|
|
|
|
appendFile("some/path", "some data", "utf8");
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"The \"cb\" argument must be of type function. Received type string ('utf8')",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Unsupported encoding results in error()",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
|
|
|
appendFile("some/path", "some data", "made-up-encoding", () => {});
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"The argument 'made-up-encoding' is invalid encoding. Received 'encoding'",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
appendFile(
|
|
|
|
"some/path",
|
|
|
|
"some data",
|
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
|
|
|
{ encoding: "made-up-encoding" },
|
|
|
|
() => {},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"The argument 'made-up-encoding' is invalid encoding. Received 'encoding'",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
|
|
|
() => appendFileSync("some/path", "some data", "made-up-encoding"),
|
|
|
|
Error,
|
|
|
|
"The argument 'made-up-encoding' is invalid encoding. Received 'encoding'",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() =>
|
|
|
|
appendFileSync("some/path", "some data", {
|
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
|
|
|
encoding: "made-up-encoding",
|
|
|
|
}),
|
|
|
|
Error,
|
|
|
|
"The argument 'made-up-encoding' is invalid encoding. Received 'encoding'",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Async: Data is written to passed in rid",
|
|
|
|
async fn() {
|
|
|
|
const tempFile: string = await Deno.makeTempFile();
|
2024-01-24 09:59:55 -05:00
|
|
|
using file = await Deno.open(tempFile, {
|
2023-03-19 12:10:39 -04:00
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
read: true,
|
|
|
|
});
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
appendFile(file.rid, "hello world", (err) => {
|
|
|
|
if (err) reject();
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
const data = await Deno.readFile(tempFile);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
}, () => {
|
|
|
|
fail("No error expected");
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove(tempFile);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Async: Data is written to passed in file path",
|
|
|
|
async fn() {
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
appendFile("_fs_appendFile_test_file.txt", "hello world", (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
const data = await Deno.readFile("_fs_appendFile_test_file.txt");
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
}, (err) => {
|
|
|
|
fail("No error was expected: " + err);
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove("_fs_appendFile_test_file.txt");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Async: Data is written to passed in URL",
|
|
|
|
async fn() {
|
|
|
|
const fileURL = new URL("_fs_appendFile_test_file.txt", import.meta.url);
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
appendFile(fileURL, "hello world", (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
const data = await Deno.readFile(fromFileUrl(fileURL));
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
}, (err) => {
|
|
|
|
fail("No error was expected: " + err);
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove(fromFileUrl(fileURL));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"Async: Callback is made with error if attempting to append data to an existing file with 'ax' flag",
|
|
|
|
async fn() {
|
|
|
|
const tempFile: string = await Deno.makeTempFile();
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
appendFile(tempFile, "hello world", { flag: "ax" }, (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
2024-01-23 18:27:29 -05:00
|
|
|
.then(() => fail("Expected error to be thrown"))
|
|
|
|
.catch(() => {})
|
2023-03-19 12:10:39 -04:00
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove(tempFile);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Sync: Data is written to passed in rid",
|
|
|
|
fn() {
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
2024-01-24 09:59:55 -05:00
|
|
|
using file = Deno.openSync(tempFile, {
|
2023-03-19 12:10:39 -04:00
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
read: true,
|
|
|
|
});
|
|
|
|
appendFileSync(file.rid, "hello world");
|
|
|
|
const data = Deno.readFileSync(tempFile);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
Deno.removeSync(tempFile);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Sync: Data is written to passed in file path",
|
|
|
|
fn() {
|
|
|
|
appendFileSync("_fs_appendFile_test_file_sync.txt", "hello world");
|
|
|
|
const data = Deno.readFileSync("_fs_appendFile_test_file_sync.txt");
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
Deno.removeSync("_fs_appendFile_test_file_sync.txt");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"Sync: error thrown if attempting to append data to an existing file with 'ax' flag",
|
|
|
|
fn() {
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
|
|
|
assertThrows(
|
|
|
|
() => appendFileSync(tempFile, "hello world", { flag: "ax" }),
|
|
|
|
Error,
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
Deno.removeSync(tempFile);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Sync: Data is written in Uint8Array to passed in file path",
|
|
|
|
fn() {
|
|
|
|
const testData = new TextEncoder().encode("hello world");
|
|
|
|
appendFileSync("_fs_appendFile_test_file_sync.txt", testData);
|
|
|
|
const data = Deno.readFileSync("_fs_appendFile_test_file_sync.txt");
|
|
|
|
assertEquals(data, testData);
|
|
|
|
Deno.removeSync("_fs_appendFile_test_file_sync.txt");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Async: Data is written in Uint8Array to passed in file path",
|
|
|
|
async fn() {
|
|
|
|
const testData = new TextEncoder().encode("hello world");
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
appendFile("_fs_appendFile_test_file.txt", testData, (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
const data = await Deno.readFile("_fs_appendFile_test_file.txt");
|
|
|
|
assertEquals(data, testData);
|
|
|
|
}, (err) => {
|
|
|
|
fail("No error was expected: " + err);
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove("_fs_appendFile_test_file.txt");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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("node:fs", 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);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|