From 0fb8ded09ce9edd96a92c21e2535aa4823ccbc4a Mon Sep 17 00:00:00 2001 From: Souma <101255979+5ouma@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:45:02 +0900 Subject: [PATCH] chore(test): Improve test coverage (#67) Now, test coverage becomes 100%, and tests are run more reliably. --- deno.json | 2 +- src/libs/io.ts | 6 +++--- test/libs/io_test.ts | 28 +++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/deno.json b/deno.json index 1f7f998..217919d 100644 --- a/deno.json +++ b/deno.json @@ -4,7 +4,7 @@ "test": { "include": ["src/", "test/"] }, "tasks": { "gen": "deno run --allow-read --allow-write ./src/main.ts", - "test": "deno test --allow-read --allow-write" + "test": "deno test --allow-read --allow-write --parallel --shuffle" }, "imports": { "@libs/xml": "jsr:@libs/xml@5.4.6", diff --git a/src/libs/io.ts b/src/libs/io.ts index a140509..d468f26 100644 --- a/src/libs/io.ts +++ b/src/libs/io.ts @@ -6,12 +6,12 @@ import { convertToOPML, convertToTOML } from "./mod.ts"; export async function readTOML(file: string): Promise { try { const data: string = await Deno.readTextFile(file); - return await convertToTOML(data); + return convertToTOML(data); } catch (error) { if (error instanceof Deno.errors.NotFound) { - throw Error(`File not found: "${file}"`); + throw new Error(`File not found: "${file}"`); } else if (error instanceof Deno.errors.PermissionDenied) { - throw Error(`Permission denied: "${file}"`); + throw new Error(`Permission denied: "${file}"`); } else throw error; } } diff --git a/test/libs/io_test.ts b/test/libs/io_test.ts index 0db5846..50f7929 100644 --- a/test/libs/io_test.ts +++ b/test/libs/io_test.ts @@ -1,4 +1,4 @@ -import { assertEquals } from "@std/assert"; +import { assertEquals, assertIsError } from "@std/assert"; import { join } from "@std/path"; import { convertToOPML, @@ -25,6 +25,32 @@ xmlUrl = "https://example.com/feed" assertEquals(convertToTOML(toml), lists); }); +Deno.test("Read TOML (File not found)", async () => { + try { + await readTOML("file-not-found.toml"); + } catch (error) { + assertEquals(error.message, 'File not found: "file-not-found.toml"'); + } +}); + +Deno.test("Read TOML (Permission denied)", async () => { + const file: string = await Deno.makeTempFile({ suffix: ".toml" }); + await Deno.chmod(file, 0o000); + try { + await readTOML(file); + } catch (error) { + assertEquals(error.message, `Permission denied: "${file}"`); + } +}); + +Deno.test("Read TOML (Unexpected error)", async () => { + try { + await readTOML(""); + } catch (error) { + assertIsError(error); + } +}); + Deno.test("Write XML", async () => { const feeds: Lists = { lists: [