Skip to content

Commit

Permalink
chore(test): Improve test coverage (#67)
Browse files Browse the repository at this point in the history
Now, test coverage becomes 100%, and tests are run more reliably.
  • Loading branch information
5ouma committed Jun 4, 2024
1 parent e2f6da0 commit 0fb8ded
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]",
Expand Down
6 changes: 3 additions & 3 deletions src/libs/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { convertToOPML, convertToTOML } from "./mod.ts";
export async function readTOML(file: string): Promise<Lists> {
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;
}
}
Expand Down
28 changes: 27 additions & 1 deletion test/libs/io_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "@std/assert";
import { assertEquals, assertIsError } from "@std/assert";
import { join } from "@std/path";
import {
convertToOPML,
Expand All @@ -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: [
Expand Down

0 comments on commit 0fb8ded

Please sign in to comment.