From ce82a72d1bc96c5979db79e54bc6aea10e336f0b Mon Sep 17 00:00:00 2001 From: 5ouma <101255979+5ouma@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:33:13 +0900 Subject: [PATCH 1/2] test(io): Write more tests To cover all try-catch trials. --- src/libs/io.ts | 6 +++--- test/libs/io_test.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) 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: [ From b9214cdbc52a4cffa8aa4d3d9c93ac029cb5b025 Mon Sep 17 00:00:00 2001 From: 5ouma <101255979+5ouma@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:34:04 +0900 Subject: [PATCH 2/2] chore(test): Run tests in parallel and shuffle To check all tests are passed with random ways faster. --- deno.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",