-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_test.ts
68 lines (58 loc) 路 1.78 KB
/
io_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { assertEquals, assertIsError } from "@std/assert";
import { join } from "@std/path";
import { readTOML, writeXML } from "@5ouma/opml-generator/libs";
import { convertFromTOML, convertToOPML } from "../../src/libs/convert.ts";
import type { Lists } from "@5ouma/opml-generator/types";
Deno.test("Read TOML", async (t: Deno.TestContext) => {
await t.step("normal", async () => {
const toml = `
[[lists]]
name = "list name"
[[lists.feeds]]
title = "feed title"
xmlUrl = "https://example.com/feed"
`;
const file: string = await Deno.makeTempFile({ suffix: ".toml" });
await Deno.writeTextFile(file, toml);
const lists: Lists = await readTOML(file);
assertEquals(convertFromTOML(toml), lists);
});
await t.step("file not found", async () => {
try {
await readTOML("file-not-found.toml");
} catch (error) {
assertEquals(error.message, 'file not found: "file-not-found.toml"');
}
});
await t.step("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}"`);
}
});
await t.step("unexpected error", async () => {
try {
await readTOML("");
} catch (error) {
assertIsError(error);
}
});
});
Deno.test("Write XML", async () => {
const feeds: Lists = {
lists: [{
name: "list name",
feeds: [{
title: "feed title",
xmlUrl: new URL("https://example.com/feed"),
}],
}],
};
const dir: string = await Deno.makeTempDir();
await writeXML(feeds, dir);
const data: string = await Deno.readTextFile(join(dir, "list-name.xml"));
assertEquals(convertToOPML(feeds.lists[0]), data);
});