forked from denoland/deno
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(std): fs/writeJson add test for append option (denoland#6889)
Co-authored-by: Ryan Dahl <ry@tinyclouds.org> Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
- Loading branch information
1 parent
8c0140e
commit f32d280
Showing
1 changed file
with
143 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,243 +1,201 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
import * as path from "../path/mod.ts"; | ||
import { | ||
assertEquals, | ||
assertThrowsAsync, | ||
assertThrows, | ||
assertThrowsAsync, | ||
} from "../testing/asserts.ts"; | ||
import * as path from "../path/mod.ts"; | ||
import { | ||
exists, | ||
existsSync, | ||
} from "./exists.ts"; | ||
import { writeJson, writeJsonSync } from "./write_json.ts"; | ||
|
||
const testdataDir = path.resolve("fs", "testdata"); | ||
|
||
Deno.test("writeJsonIfNotExists", async function (): Promise<void> { | ||
const notExistsJsonFile = path.join(testdataDir, "file_not_exists.json"); | ||
Deno.test("writeJson not exists", async function (): Promise<void> { | ||
const notExistsJsonFile = path.join(testdataDir, "writeJson_not_exists.json"); | ||
|
||
await assertThrowsAsync( | ||
async (): Promise<void> => { | ||
await writeJson(notExistsJsonFile, { a: "1" }); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
); | ||
await writeJson(notExistsJsonFile, { a: "1" }); | ||
|
||
const content = await Deno.readFile(notExistsJsonFile); | ||
const content = await Deno.readTextFile(notExistsJsonFile); | ||
|
||
await Deno.remove(notExistsJsonFile); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`); | ||
}); | ||
|
||
Deno.test("writeJsonIfExists", async function (): Promise<void> { | ||
const existsJsonFile = path.join(testdataDir, "file_write_exists.json"); | ||
|
||
await Deno.writeFile(existsJsonFile, new Uint8Array()); | ||
|
||
await assertThrowsAsync( | ||
async (): Promise<void> => { | ||
await writeJson(existsJsonFile, { a: "1" }); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
); | ||
|
||
const content = await Deno.readFile(existsJsonFile); | ||
|
||
await Deno.remove(existsJsonFile); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`); | ||
assertEquals(content, `{"a":"1"}\n`); | ||
}); | ||
|
||
Deno.test("writeJsonIfExistsAnInvalidJson", async function (): Promise<void> { | ||
const existsInvalidJsonFile = path.join( | ||
Deno.test("writeJson if not exists", async function (): Promise<void> { | ||
const notExistsJsonFile = path.join( | ||
testdataDir, | ||
"file_write_invalid.json", | ||
"writeJson_file_not_exists.json", | ||
); | ||
|
||
const invalidJsonContent = new TextEncoder().encode("[123}"); | ||
await Deno.writeFile(existsInvalidJsonFile, invalidJsonContent); | ||
|
||
await assertThrowsAsync( | ||
async (): Promise<void> => { | ||
await writeJson(existsInvalidJsonFile, { a: "1" }); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
); | ||
|
||
const content = await Deno.readFile(existsInvalidJsonFile); | ||
|
||
await Deno.remove(existsInvalidJsonFile); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`); | ||
try { | ||
assertThrowsAsync( | ||
async function (): Promise<void> { | ||
await writeJson(notExistsJsonFile, { a: "1" }, { create: false }); | ||
}, | ||
Deno.errors.NotFound, | ||
); | ||
} finally { | ||
if (await exists(notExistsJsonFile)) await Deno.remove(notExistsJsonFile); | ||
} | ||
}); | ||
|
||
Deno.test("writeJsonWithSpaces", async function (): Promise<void> { | ||
const existsJsonFile = path.join(testdataDir, "file_write_spaces.json"); | ||
|
||
const invalidJsonContent = new TextEncoder().encode(); | ||
await Deno.writeFile(existsJsonFile, invalidJsonContent); | ||
|
||
await assertThrowsAsync( | ||
async (): Promise<void> => { | ||
await writeJson(existsJsonFile, { a: "1" }, { spaces: 2 }); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
); | ||
|
||
const content = await Deno.readFile(existsJsonFile); | ||
|
||
await Deno.remove(existsJsonFile); | ||
Deno.test("writeJson exists", async function (): Promise<void> { | ||
const existsJsonFile = path.join(testdataDir, "writeJson_exists.json"); | ||
await Deno.writeFile(existsJsonFile, new Uint8Array()); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}\n`); | ||
try { | ||
await writeJson(existsJsonFile, { a: "1" }); | ||
const content = await Deno.readTextFile(existsJsonFile); | ||
assertEquals(content, `{"a":"1"}\n`); | ||
} finally { | ||
await Deno.remove(existsJsonFile); | ||
} | ||
}); | ||
|
||
Deno.test("writeJsonWithReplacer", async function (): Promise<void> { | ||
const existsJsonFile = path.join(testdataDir, "file_write_replacer.json"); | ||
|
||
const invalidJsonContent = new TextEncoder().encode(); | ||
await Deno.writeFile(existsJsonFile, invalidJsonContent); | ||
|
||
await assertThrowsAsync( | ||
async (): Promise<void> => { | ||
await writeJson( | ||
existsJsonFile, | ||
{ a: "1", b: "2", c: "3" }, | ||
{ | ||
replacer: ["a"], | ||
}, | ||
); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
); | ||
|
||
const content = await Deno.readFile(existsJsonFile); | ||
|
||
await Deno.remove(existsJsonFile); | ||
Deno.test("writeJson spaces", async function (): Promise<void> { | ||
const existsJsonFile = path.join(testdataDir, "writeJson_spaces.json"); | ||
await Deno.writeFile(existsJsonFile, new Uint8Array()); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`); | ||
try { | ||
await writeJson(existsJsonFile, { a: "1" }, { spaces: 2 }); | ||
const content = await Deno.readTextFile(existsJsonFile); | ||
assertEquals(content, `{\n "a": "1"\n}\n`); | ||
} finally { | ||
await Deno.remove(existsJsonFile); | ||
} | ||
}); | ||
|
||
Deno.test("writeJsonSyncIfNotExists", function (): void { | ||
const notExistsJsonFile = path.join(testdataDir, "file_not_exists_sync.json"); | ||
Deno.test("writeJson replacer", async function (): Promise<void> { | ||
const existsJsonFile = path.join(testdataDir, "writeJson_replacer.json"); | ||
await Deno.writeFile(existsJsonFile, new Uint8Array()); | ||
|
||
assertThrows( | ||
(): void => { | ||
writeJsonSync(notExistsJsonFile, { a: "1" }); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
); | ||
try { | ||
await writeJson( | ||
existsJsonFile, | ||
{ a: "1", b: "2", c: "3" }, | ||
{ replacer: ["a"] }, | ||
); | ||
|
||
const content = await Deno.readTextFile(existsJsonFile); | ||
assertEquals(content, `{"a":"1"}\n`); | ||
} finally { | ||
await Deno.remove(existsJsonFile); | ||
} | ||
}); | ||
|
||
const content = Deno.readFileSync(notExistsJsonFile); | ||
Deno.test("writeJson append", async function (): Promise<void> { | ||
const existsJsonFile = path.join(testdataDir, "writeJson_append.json"); | ||
await Deno.writeFile(existsJsonFile, new Uint8Array()); | ||
|
||
Deno.removeSync(notExistsJsonFile); | ||
try { | ||
await writeJson(existsJsonFile, { a: "1" }, { append: true }); | ||
await writeJson(existsJsonFile, { b: "2" }, { append: true }); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`); | ||
const content = await Deno.readTextFile(existsJsonFile); | ||
assertEquals(content, `{"a":"1"}\n{"b":"2"}\n`); | ||
} finally { | ||
await Deno.remove(existsJsonFile); | ||
} | ||
}); | ||
|
||
Deno.test("writeJsonSyncIfExists", function (): void { | ||
const existsJsonFile = path.join(testdataDir, "file_write_exists_sync.json"); | ||
|
||
Deno.writeFileSync(existsJsonFile, new Uint8Array()); | ||
|
||
assertThrows( | ||
(): void => { | ||
writeJsonSync(existsJsonFile, { a: "1" }); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
Deno.test("writeJsonSync not exists", function (): void { | ||
const notExistsJsonFile = path.join( | ||
testdataDir, | ||
"writeJsonSync_not_exists.json", | ||
); | ||
|
||
const content = Deno.readFileSync(existsJsonFile); | ||
writeJsonSync(notExistsJsonFile, { a: "1" }); | ||
|
||
Deno.removeSync(existsJsonFile); | ||
const content = Deno.readTextFileSync(notExistsJsonFile); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`); | ||
Deno.removeSync(notExistsJsonFile); | ||
|
||
assertEquals(content, `{"a":"1"}\n`); | ||
}); | ||
|
||
Deno.test("writeJsonSyncIfExistsAnInvalidJson", function (): void { | ||
const existsInvalidJsonFile = path.join( | ||
Deno.test("writeJsonSync if not exists", function (): void { | ||
const notExistsJsonFile = path.join( | ||
testdataDir, | ||
"file_write_invalid_sync.json", | ||
); | ||
|
||
const invalidJsonContent = new TextEncoder().encode("[123}"); | ||
Deno.writeFileSync(existsInvalidJsonFile, invalidJsonContent); | ||
|
||
assertThrows( | ||
(): void => { | ||
writeJsonSync(existsInvalidJsonFile, { a: "1" }); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
"writeJsonSync_file_not_exists.json", | ||
); | ||
|
||
const content = Deno.readFileSync(existsInvalidJsonFile); | ||
|
||
Deno.removeSync(existsInvalidJsonFile); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`); | ||
try { | ||
assertThrows( | ||
function (): void { | ||
writeJsonSync(notExistsJsonFile, { a: "1" }, { create: false }); | ||
}, | ||
Deno.errors.NotFound, | ||
); | ||
} finally { | ||
if (existsSync(notExistsJsonFile)) Deno.removeSync(notExistsJsonFile); | ||
} | ||
}); | ||
|
||
Deno.test("writeJsonSyncWithSpaces", function (): void { | ||
const existsJsonFile = path.join(testdataDir, "file_write_spaces_sync.json"); | ||
|
||
const invalidJsonContent = new TextEncoder().encode(); | ||
Deno.writeFileSync(existsJsonFile, invalidJsonContent); | ||
Deno.test("writeJsonSync exists", function (): void { | ||
const existsJsonFile = path.join(testdataDir, "writeJsonSync_exists.json"); | ||
Deno.writeFileSync(existsJsonFile, new Uint8Array()); | ||
|
||
assertThrows( | ||
(): void => { | ||
writeJsonSync(existsJsonFile, { a: "1" }, { spaces: 2 }); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
); | ||
try { | ||
writeJsonSync(existsJsonFile, { a: "1" }); | ||
const content = Deno.readTextFileSync(existsJsonFile); | ||
assertEquals(content, `{"a":"1"}\n`); | ||
} finally { | ||
Deno.removeSync(existsJsonFile); | ||
} | ||
}); | ||
|
||
const content = Deno.readFileSync(existsJsonFile); | ||
Deno.test("writeJsonSync spaces", function (): void { | ||
const existsJsonFile = path.join(testdataDir, "writeJsonSync_spaces.json"); | ||
|
||
Deno.removeSync(existsJsonFile); | ||
Deno.writeFileSync(existsJsonFile, new Uint8Array()); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{\n "a": "1"\n}\n`); | ||
try { | ||
writeJsonSync(existsJsonFile, { a: "1" }, { spaces: 2 }); | ||
const content = Deno.readTextFileSync(existsJsonFile); | ||
assertEquals(content, `{\n "a": "1"\n}\n`); | ||
} finally { | ||
Deno.removeSync(existsJsonFile); | ||
} | ||
}); | ||
|
||
Deno.test("writeJsonSyncWithReplacer", function (): void { | ||
Deno.test("writeJsonSync replacer", function (): void { | ||
const existsJsonFile = path.join( | ||
testdataDir, | ||
"file_write_replacer_sync.json", | ||
"writeJsonSync_replacer.json", | ||
); | ||
|
||
const invalidJsonContent = new TextEncoder().encode(); | ||
Deno.writeFileSync(existsJsonFile, invalidJsonContent); | ||
|
||
assertThrows( | ||
(): void => { | ||
writeJsonSync( | ||
existsJsonFile, | ||
{ a: "1", b: "2", c: "3" }, | ||
{ | ||
replacer: ["a"], | ||
}, | ||
); | ||
throw new Error("should write success"); | ||
}, | ||
Error, | ||
"should write success", | ||
); | ||
Deno.writeFileSync(existsJsonFile, new Uint8Array()); | ||
|
||
const content = Deno.readFileSync(existsJsonFile); | ||
try { | ||
writeJsonSync( | ||
existsJsonFile, | ||
{ a: "1", b: "2", c: "3" }, | ||
{ replacer: ["a"] }, | ||
); | ||
|
||
const content = Deno.readTextFileSync(existsJsonFile); | ||
assertEquals(content, `{"a":"1"}\n`); | ||
} finally { | ||
Deno.removeSync(existsJsonFile); | ||
} | ||
}); | ||
|
||
Deno.test("writeJsonSync append", function (): void { | ||
const existsJsonFile = path.join(testdataDir, "writeJsonSync_append.json"); | ||
|
||
Deno.writeFileSync(existsJsonFile, new Uint8Array()); | ||
|
||
Deno.removeSync(existsJsonFile); | ||
try { | ||
writeJsonSync(existsJsonFile, { a: "1" }, { append: true }); | ||
writeJsonSync(existsJsonFile, { b: "2" }, { append: true }); | ||
|
||
assertEquals(new TextDecoder().decode(content), `{"a":"1"}\n`); | ||
const content = Deno.readTextFileSync(existsJsonFile); | ||
assertEquals(content, `{"a":"1"}\n{"b":"2"}\n`); | ||
} finally { | ||
Deno.removeSync(existsJsonFile); | ||
} | ||
}); |