2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-14 10:26:12 -04:00
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrowsAsync,
|
2020-03-28 13:03:49 -04:00
|
|
|
assertThrows,
|
2019-03-14 10:26:12 -04:00
|
|
|
} from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-14 10:26:12 -04:00
|
|
|
import { writeJson, writeJsonSync } from "./write_json.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("writeJsonIfNotExists", async function (): Promise<void> {
|
2019-03-14 10:26:12 -04:00
|
|
|
const notExistsJsonFile = path.join(testdataDir, "file_not_exists.json");
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-03-14 10:26:12 -04:00
|
|
|
await writeJson(notExistsJsonFile, { a: "1" });
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = await Deno.readFile(notExistsJsonFile);
|
|
|
|
|
|
|
|
await Deno.remove(notExistsJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("writeJsonIfExists", async function (): Promise<void> {
|
2019-03-14 10:26:12 -04:00
|
|
|
const existsJsonFile = path.join(testdataDir, "file_write_exists.json");
|
|
|
|
|
|
|
|
await Deno.writeFile(existsJsonFile, new Uint8Array());
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-03-14 10:26:12 -04:00
|
|
|
await writeJson(existsJsonFile, { a: "1" });
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = await Deno.readFile(existsJsonFile);
|
|
|
|
|
|
|
|
await Deno.remove(existsJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("writeJsonIfExistsAnInvalidJson", async function (): Promise<void> {
|
2019-03-14 10:26:12 -04:00
|
|
|
const existsInvalidJsonFile = path.join(
|
|
|
|
testdataDir,
|
2020-07-14 15:24:17 -04:00
|
|
|
"file_write_invalid.json",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const invalidJsonContent = new TextEncoder().encode("[123}");
|
|
|
|
await Deno.writeFile(existsInvalidJsonFile, invalidJsonContent);
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-03-14 10:26:12 -04:00
|
|
|
await writeJson(existsInvalidJsonFile, { a: "1" });
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = await Deno.readFile(existsInvalidJsonFile);
|
|
|
|
|
|
|
|
await Deno.remove(existsInvalidJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("writeJsonWithSpaces", async function (): Promise<void> {
|
2019-03-14 10:26:12 -04:00
|
|
|
const existsJsonFile = path.join(testdataDir, "file_write_spaces.json");
|
|
|
|
|
|
|
|
const invalidJsonContent = new TextEncoder().encode();
|
|
|
|
await Deno.writeFile(existsJsonFile, invalidJsonContent);
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-03-14 10:26:12 -04:00
|
|
|
await writeJson(existsJsonFile, { a: "1" }, { spaces: 2 });
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = await Deno.readFile(existsJsonFile);
|
|
|
|
|
|
|
|
await Deno.remove(existsJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("writeJsonWithReplacer", async function (): Promise<void> {
|
2019-03-14 10:26:12 -04:00
|
|
|
const existsJsonFile = path.join(testdataDir, "file_write_replacer.json");
|
|
|
|
|
|
|
|
const invalidJsonContent = new TextEncoder().encode();
|
|
|
|
await Deno.writeFile(existsJsonFile, invalidJsonContent);
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-03-14 10:26:12 -04:00
|
|
|
await writeJson(
|
|
|
|
existsJsonFile,
|
|
|
|
{ a: "1", b: "2", c: "3" },
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
replacer: ["a"],
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = await Deno.readFile(existsJsonFile);
|
|
|
|
|
|
|
|
await Deno.remove(existsJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("writeJsonSyncIfNotExists", function (): void {
|
2019-03-14 10:26:12 -04:00
|
|
|
const notExistsJsonFile = path.join(testdataDir, "file_not_exists_sync.json");
|
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-03-14 10:26:12 -04:00
|
|
|
writeJsonSync(notExistsJsonFile, { a: "1" });
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = Deno.readFileSync(notExistsJsonFile);
|
|
|
|
|
|
|
|
Deno.removeSync(notExistsJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("writeJsonSyncIfExists", function (): void {
|
2019-03-14 10:26:12 -04:00
|
|
|
const existsJsonFile = path.join(testdataDir, "file_write_exists_sync.json");
|
|
|
|
|
|
|
|
Deno.writeFileSync(existsJsonFile, new Uint8Array());
|
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-03-14 10:26:12 -04:00
|
|
|
writeJsonSync(existsJsonFile, { a: "1" });
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = Deno.readFileSync(existsJsonFile);
|
|
|
|
|
|
|
|
Deno.removeSync(existsJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("writeJsonSyncIfExistsAnInvalidJson", function (): void {
|
2019-03-14 10:26:12 -04:00
|
|
|
const existsInvalidJsonFile = path.join(
|
|
|
|
testdataDir,
|
2020-07-14 15:24:17 -04:00
|
|
|
"file_write_invalid_sync.json",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const invalidJsonContent = new TextEncoder().encode("[123}");
|
|
|
|
Deno.writeFileSync(existsInvalidJsonFile, invalidJsonContent);
|
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-03-14 10:26:12 -04:00
|
|
|
writeJsonSync(existsInvalidJsonFile, { a: "1" });
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = Deno.readFileSync(existsInvalidJsonFile);
|
|
|
|
|
|
|
|
Deno.removeSync(existsInvalidJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
Deno.test("writeJsonSyncWithSpaces", function (): void {
|
2019-03-14 10:26:12 -04:00
|
|
|
const existsJsonFile = path.join(testdataDir, "file_write_spaces_sync.json");
|
|
|
|
|
|
|
|
const invalidJsonContent = new TextEncoder().encode();
|
|
|
|
Deno.writeFileSync(existsJsonFile, invalidJsonContent);
|
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-03-14 10:26:12 -04:00
|
|
|
writeJsonSync(existsJsonFile, { a: "1" }, { spaces: 2 });
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = Deno.readFileSync(existsJsonFile);
|
|
|
|
|
|
|
|
Deno.removeSync(existsJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
Deno.test("writeJsonSyncWithReplacer", function (): void {
|
2019-03-14 10:26:12 -04:00
|
|
|
const existsJsonFile = path.join(
|
|
|
|
testdataDir,
|
2020-07-14 15:24:17 -04:00
|
|
|
"file_write_replacer_sync.json",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const invalidJsonContent = new TextEncoder().encode();
|
|
|
|
Deno.writeFileSync(existsJsonFile, invalidJsonContent);
|
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-03-14 10:26:12 -04:00
|
|
|
writeJsonSync(
|
|
|
|
existsJsonFile,
|
|
|
|
{ a: "1", b: "2", c: "3" },
|
|
|
|
{
|
2020-03-28 13:03:49 -04:00
|
|
|
replacer: ["a"],
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
throw new Error("should write success");
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"should write success",
|
2019-03-14 10:26:12 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const content = Deno.readFileSync(existsJsonFile);
|
|
|
|
|
|
|
|
Deno.removeSync(existsJsonFile);
|
|
|
|
|
2020-07-26 15:51:33 -04:00
|
|
|
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`);
|
2019-03-14 10:26:12 -04:00
|
|
|
});
|