-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathmarkdown-test.ts
More file actions
71 lines (64 loc) · 2.64 KB
/
Copy pathmarkdown-test.ts
File metadata and controls
71 lines (64 loc) · 2.64 KB
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
69
70
71
import assert from "node:assert";
import {readdirSync, statSync} from "node:fs";
import {mkdir, readFile, unlink, writeFile} from "node:fs/promises";
import {basename, join, resolve} from "node:path/posix";
import deepEqual from "fast-deep-equal";
import {normalizeConfig} from "../src/config.js";
import {isEnoent} from "../src/error.js";
import type {MarkdownPage} from "../src/markdown.js";
import {parseMarkdown} from "../src/markdown.js";
describe("parseMarkdown(input)", async () => {
const {md} = await normalizeConfig();
const inputRoot = "test/input";
const outputRoot = "test/output";
for (const name of readdirSync(inputRoot)) {
if (!name.endsWith(".md")) continue;
const path = join(inputRoot, name);
if (!statSync(path).isFile()) continue;
const only = name.startsWith("only.");
const skip = name.startsWith("skip.");
const outname = only || skip ? name.slice(5) : name;
(only ? it.only : skip ? it.skip : it)(`test/input/${name}`, async () => {
const source = await readFile(path, "utf8");
const snapshot = parseMarkdown(source, {root: "test/input", path: name, md});
let allequal = true;
for (const ext of ["html", "json"]) {
const actual = ext === "json" ? jsonMeta(snapshot) : snapshot[ext];
const outfile = resolve(outputRoot, `${ext === "json" ? outname : basename(outname, ".md")}.${ext}`);
const diffile = resolve(outputRoot, `${ext === "json" ? outname : basename(outname, ".md")}-changed.${ext}`);
let expected;
try {
expected = await readFile(outfile, "utf8");
} catch (error) {
if (!isEnoent(error) || process.env.CI === "true") throw error;
console.warn(`! generating ${outfile}`);
await mkdir(outputRoot, {recursive: true});
await writeFile(outfile, actual, "utf8");
continue;
}
const equal = ext === "json" ? jsonEqual(expected, actual) : expected === actual;
if (equal) {
if (process.env.CI !== "true") {
try {
await unlink(diffile);
console.warn(`! deleted ${diffile}`);
} catch (error) {
if (!isEnoent(error)) throw error;
}
}
} else {
allequal = false;
console.warn(`! generating ${diffile}`);
await writeFile(diffile, actual, "utf8");
}
}
assert.ok(allequal, `${name} must match snapshot`);
});
}
});
function jsonMeta({html, ...rest}: MarkdownPage): string {
return JSON.stringify(rest, null, 2);
}
function jsonEqual(a: string, b: string): boolean {
return deepEqual(JSON.parse(a), JSON.parse(b));
}