deno.land / x / lume@v2.1.4 / tests / utils.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { assertSnapshot } from "../deps/snapshot.ts";import lume from "../mod.ts";import { basename, fromFileUrl, join } from "../deps/path.ts";import { DeepPartial } from "../core/utils/object.ts";
import type { Writer } from "../core/writer.ts";import type { default as Site, SiteOptions } from "../core/site.ts";import type { SourceMap } from "../plugins/source_maps.ts";
const cwUrl = import.meta.resolve("./");const cwd = fromFileUrl(import.meta.resolve("./"));
export function getPath(path: string): string { return join(cwd, path);}
class TestWriter implements Writer { savePages() { return Promise.resolve([]); }
copyFiles() { return Promise.resolve([]); }
clear() { return Promise.resolve(); }
removeFiles() { return Promise.resolve(); }}
/** Create a new lume site using the "assets" path as cwd */export function getSite( options: DeepPartial<SiteOptions> = {}, pluginOptions = {},): Site { options.cwd = getPath("assets");
const site = lume(options, pluginOptions, false); site.writer = new TestWriter();
return site;}
/** Returns a generated page by src path */export function getPage(site: Site, path: string) { const page = site.pages.find((page) => page.src.path === path);
if (!page) { throw new Error(`Page "${path}" not found`); }
return page;}
/** Build a site and print errors */export async function build(site: Site) { try { await site.build(); } catch (error) { console.error(Deno.inspect(error, { colors: true })); throw error; }}
function normalizeValue( content: unknown[] | Uint8Array | string | undefined, options: SiteSnapshotOptions,): string { if (content === undefined) { return "undefined"; }
if (typeof content === "string") { // Normalize line ending for Windows return content .replaceAll("\r\n", "\n") .replaceAll(/base64,[^"]+/g, "base64,(...)"); }
if (content instanceof Uint8Array) { if (options.avoidBinaryFilesLength) { return `Uint8Array()`; } return `Uint8Array(${content.length})`; }
return `Array(${content.length})`;}function normalizeSourceMap(content: string) { const sourceMap: SourceMap = JSON.parse(content); sourceMap.sourceRoot = sourceMap.sourceRoot ? basename(sourceMap.sourceRoot) : undefined; sourceMap.file = sourceMap.file ? basename(sourceMap.file) : undefined; sourceMap.sources = sourceMap.sources.map((source: string) => basename(source) ); return JSON.stringify(sourceMap);}
interface SiteSnapshotOptions { avoidBinaryFilesLength?: boolean;}
export async function assertSiteSnapshot( context: Deno.TestContext, site: Site, options: SiteSnapshotOptions = {},) { const { pages, files } = site;
// To-do: test site configuration await assertSnapshot( context, { formats: Array.from(site.formats.entries.values()).map((format) => { return { ...format, engines: format.engines?.length, }; }), src: Array.from(site.fs.entries.keys()).sort(), }, );
// Sort pages and files alphabetically pages.sort((a, b) => compare(a.src.path, b.src.path) || compare(a.outputPath, b.outputPath) ); files.sort((a, b) => compare(a.entry.path, b.entry.path));
// Normalize data of the pages const normalizedPages = pages.map((page) => { const isSourceMap = page.outputPath.endsWith(".map"); return { data: Object.fromEntries( Object.entries(page.data).map(([key, value]) => { switch (typeof value) { case "string": if (isSourceMap && key === "content") { return [key, normalizeSourceMap(value)]; } return [key, normalizeValue(value, options)]; case "undefined": return [key, normalizeValue(value, options)]; case "number": case "boolean": return [key, value]; case "object": if (value === null) { return [key, null]; } if (Array.isArray(value) || value instanceof Uint8Array) { return [key, normalizeValue(value, options)]; } if (value instanceof Map || value instanceof Set) { return [key, [...value.keys()].sort(compare)]; } return [key, Object.keys(value)]; case "function": return [key, value.name]; case "symbol": return [key, value.toString()]; case "bigint": return [key, `${value}n`]; default: throw new Error(`Unknown type "${typeof value}"`); } }).sort((a, b) => a[0].localeCompare(b[0])), ), content: isSourceMap ? normalizeSourceMap(page.content as string) : normalizeValue(page.content, options), src: { path: page.src.path, ext: page.src.ext, remote: page.src.entry?.flags.has("remote") ? page.sourcePath.replace(cwUrl, "") : undefined, asset: page.src.asset, }, }; });
const normalizedFiles = files.map((file) => { return { ...file, entry: file.entry.path, flags: [...file.entry.flags], }; });
// Test static files await assertSnapshot(context, normalizedFiles); await assertSnapshot(context, normalizedPages);}
function compare(a: string, b: string): number { return a > b ? 1 : a < b ? -1 : 0;}
lume

Version Info

Tagged at
a month ago