2020-03-08 19:14:53 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-12 10:12:27 -04:00
|
|
|
import { assertEquals, assertThrows, fail } from "../../testing/asserts.ts";
|
2020-03-08 19:14:53 -04:00
|
|
|
import { appendFile, appendFileSync } from "./_fs_appendFile.ts";
|
2020-05-03 15:14:38 -04:00
|
|
|
import { fromFileUrl } from "../path.ts";
|
2020-03-08 19:14:53 -04:00
|
|
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-08 19:14:53 -04:00
|
|
|
name: "No callback Fn results in Error",
|
2020-03-12 10:12:27 -04:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
appendFile("some/path", "some data", "utf8");
|
2020-03-08 19:14:53 -04:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"No callback function supplied",
|
2020-03-08 19:14:53 -04:00
|
|
|
);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-08 19:14:53 -04:00
|
|
|
name: "Unsupported encoding results in error()",
|
2020-03-12 10:12:27 -04:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2020-06-23 22:32:43 -04:00
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
2020-03-12 10:12:27 -04:00
|
|
|
appendFile("some/path", "some data", "made-up-encoding", () => {});
|
2020-03-08 19:14:53 -04:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Only 'utf8' encoding is currently supported",
|
2020-03-08 19:14:53 -04:00
|
|
|
);
|
2020-03-12 10:12:27 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
appendFile(
|
2020-03-08 19:14:53 -04:00
|
|
|
"some/path",
|
|
|
|
"some data",
|
2020-06-23 22:32:43 -04:00
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
2020-03-08 19:14:53 -04:00
|
|
|
{ encoding: "made-up-encoding" },
|
2020-07-14 15:24:17 -04:00
|
|
|
() => {},
|
2020-03-08 19:14:53 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Only 'utf8' encoding is currently supported",
|
2020-03-08 19:14:53 -04:00
|
|
|
);
|
|
|
|
assertThrows(
|
2020-06-23 22:32:43 -04:00
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
2020-03-08 19:14:53 -04:00
|
|
|
() => appendFileSync("some/path", "some data", "made-up-encoding"),
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Only 'utf8' encoding is currently supported",
|
2020-03-08 19:14:53 -04:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() =>
|
|
|
|
appendFileSync("some/path", "some data", {
|
2020-06-23 22:32:43 -04:00
|
|
|
// @ts-expect-error Type '"made-up-encoding"' is not assignable to type
|
2020-03-28 13:03:49 -04:00
|
|
|
encoding: "made-up-encoding",
|
2020-03-08 19:14:53 -04:00
|
|
|
}),
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"Only 'utf8' encoding is currently supported",
|
2020-03-08 19:14:53 -04:00
|
|
|
);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-08 19:14:53 -04:00
|
|
|
name: "Async: Data is written to passed in rid",
|
|
|
|
async fn() {
|
|
|
|
const tempFile: string = await Deno.makeTempFile();
|
|
|
|
const file: Deno.File = await Deno.open(tempFile, {
|
|
|
|
create: true,
|
|
|
|
write: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
read: true,
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
2020-03-12 10:12:27 -04:00
|
|
|
await new Promise((resolve, reject) => {
|
2020-03-28 13:03:49 -04:00
|
|
|
appendFile(file.rid, "hello world", (err) => {
|
2020-03-12 10:12:27 -04:00
|
|
|
if (err) reject();
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
const data = await Deno.readFile(tempFile);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
fail("No error expected");
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
Deno.close(file.rid);
|
|
|
|
await Deno.remove(tempFile);
|
|
|
|
});
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-08 19:14:53 -04:00
|
|
|
name: "Async: Data is written to passed in file path",
|
|
|
|
async fn() {
|
|
|
|
const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
|
2020-03-12 10:12:27 -04:00
|
|
|
await new Promise((resolve, reject) => {
|
2020-03-28 13:03:49 -04:00
|
|
|
appendFile("_fs_appendFile_test_file.txt", "hello world", (err) => {
|
2020-03-12 10:12:27 -04:00
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
assertEquals(Deno.resources(), openResourcesBeforeAppend);
|
|
|
|
const data = await Deno.readFile("_fs_appendFile_test_file.txt");
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
})
|
2020-03-28 13:03:49 -04:00
|
|
|
.catch((err) => {
|
2020-03-12 10:12:27 -04:00
|
|
|
fail("No error was expected: " + err);
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove("_fs_appendFile_test_file.txt");
|
|
|
|
});
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-05-03 15:14:38 -04:00
|
|
|
name: "Async: Data is written to passed in URL",
|
|
|
|
async fn() {
|
|
|
|
const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
|
|
|
|
const fileURL = new URL("_fs_appendFile_test_file.txt", import.meta.url);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
appendFile(fileURL, "hello world", (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
assertEquals(Deno.resources(), openResourcesBeforeAppend);
|
|
|
|
const data = await Deno.readFile(fromFileUrl(fileURL));
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
fail("No error was expected: " + err);
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove(fromFileUrl(fileURL));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-08 19:14:53 -04:00
|
|
|
name:
|
|
|
|
"Async: Callback is made with error if attempting to append data to an existing file with 'ax' flag",
|
|
|
|
async fn() {
|
|
|
|
const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
|
|
|
|
const tempFile: string = await Deno.makeTempFile();
|
2020-03-12 10:12:27 -04:00
|
|
|
await new Promise((resolve, reject) => {
|
2020-03-28 13:03:49 -04:00
|
|
|
appendFile(tempFile, "hello world", { flag: "ax" }, (err) => {
|
2020-03-12 10:12:27 -04:00
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
fail("Expected error to be thrown");
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
assertEquals(Deno.resources(), openResourcesBeforeAppend);
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove(tempFile);
|
|
|
|
});
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-08 19:14:53 -04:00
|
|
|
name: "Sync: Data is written to passed in rid",
|
|
|
|
fn() {
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
|
|
|
const file: Deno.File = Deno.openSync(tempFile, {
|
|
|
|
create: true,
|
|
|
|
write: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
read: true,
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
|
|
|
appendFileSync(file.rid, "hello world");
|
|
|
|
Deno.close(file.rid);
|
|
|
|
const data = Deno.readFileSync(tempFile);
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
Deno.removeSync(tempFile);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-08 19:14:53 -04:00
|
|
|
name: "Sync: Data is written to passed in file path",
|
|
|
|
fn() {
|
|
|
|
const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
|
|
|
|
appendFileSync("_fs_appendFile_test_file_sync.txt", "hello world");
|
|
|
|
assertEquals(Deno.resources(), openResourcesBeforeAppend);
|
|
|
|
const data = Deno.readFileSync("_fs_appendFile_test_file_sync.txt");
|
|
|
|
assertEquals(decoder.decode(data), "hello world");
|
|
|
|
Deno.removeSync("_fs_appendFile_test_file_sync.txt");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-08 19:14:53 -04:00
|
|
|
name:
|
|
|
|
"Sync: error thrown if attempting to append data to an existing file with 'ax' flag",
|
|
|
|
fn() {
|
|
|
|
const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
|
|
|
assertThrows(
|
|
|
|
() => appendFileSync(tempFile, "hello world", { flag: "ax" }),
|
|
|
|
Deno.errors.AlreadyExists,
|
2020-07-14 15:24:17 -04:00
|
|
|
"",
|
2020-03-08 19:14:53 -04:00
|
|
|
);
|
|
|
|
assertEquals(Deno.resources(), openResourcesBeforeAppend);
|
|
|
|
Deno.removeSync(tempFile);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-08 19:14:53 -04:00
|
|
|
});
|
2020-10-06 01:26:12 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Sync: Data is written in Uint8Array to passed in file path",
|
|
|
|
fn() {
|
|
|
|
const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
|
2020-10-22 14:36:06 -04:00
|
|
|
const testData = new TextEncoder().encode("hello world");
|
|
|
|
appendFileSync("_fs_appendFile_test_file_sync.txt", testData);
|
2020-10-06 01:26:12 -04:00
|
|
|
assertEquals(Deno.resources(), openResourcesBeforeAppend);
|
|
|
|
const data = Deno.readFileSync("_fs_appendFile_test_file_sync.txt");
|
2020-10-22 14:36:06 -04:00
|
|
|
assertEquals(data, testData);
|
2020-10-06 01:26:12 -04:00
|
|
|
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 openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
|
2020-10-22 14:36:06 -04:00
|
|
|
const testData = new TextEncoder().encode("hello world");
|
2020-10-06 01:26:12 -04:00
|
|
|
await new Promise((resolve, reject) => {
|
2020-10-22 14:36:06 -04:00
|
|
|
appendFile("_fs_appendFile_test_file.txt", testData, (err) => {
|
2020-10-06 01:26:12 -04:00
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(async () => {
|
|
|
|
assertEquals(Deno.resources(), openResourcesBeforeAppend);
|
|
|
|
const data = await Deno.readFile("_fs_appendFile_test_file.txt");
|
2020-10-22 14:36:06 -04:00
|
|
|
assertEquals(data, testData);
|
2020-10-06 01:26:12 -04:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
fail("No error was expected: " + err);
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove("_fs_appendFile_test_file.txt");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|