-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathsmoke-all.test.ts
More file actions
354 lines (320 loc) · 11.8 KB
/
smoke-all.test.ts
File metadata and controls
354 lines (320 loc) · 11.8 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* smoke-all.test.ts
*
* Copyright (C) 2022 Posit Software, PBC
*/
import { expandGlobSync } from "../../src/core/deno/expand-glob.ts";
import { testQuartoCmd, Verify } from "../test.ts";
import { initYamlIntelligenceResourcesFromFilesystem } from "../../src/core/schema/utils.ts";
import {
initState,
setInitializer,
} from "../../src/core/lib/yaml-validation/state.ts";
import { breakQuartoMd } from "../../src/core/lib/break-quarto-md.ts";
import { parse } from "../../src/core/yaml.ts";
import { cleanoutput } from "./render/render.ts";
import {
ensureDocxRegexMatches,
ensureDocxXpath,
ensureFileRegexMatches,
ensureHtmlElements,
ensurePdfRegexMatches,
ensureJatsXpath,
ensureOdtXpath,
ensurePptxRegexMatches,
ensureTypstFileRegexMatches,
ensureSnapshotMatches,
fileExists,
noErrors,
noErrorsOrWarnings,
ensurePptxXpath,
ensurePptxLayout,
ensurePptxMaxSlides,
ensureLatexFileRegexMatches,
printsMessage,
} from "../verify.ts";
import { readYamlFromMarkdown } from "../../src/core/yaml.ts";
import { findProjectDir, findProjectOutputDir, outputForInput } from "../utils.ts";
import { jupyterNotebookToMarkdown } from "../../src/command/convert/jupyter.ts";
import { basename, dirname, join, relative } from "../../src/deno_ral/path.ts";
import { WalkEntry } from "../../src/deno_ral/fs.ts";
import { quarto } from "../../src/quarto.ts";
import { safeExistsSync, safeRemoveSync } from "../../src/core/path.ts";
async function fullInit() {
await initYamlIntelligenceResourcesFromFilesystem();
}
async function guessFormat(fileName: string): Promise<string[]> {
const { cells } = await breakQuartoMd(Deno.readTextFileSync(fileName));
const formats: Set<string> = new Set();
for (const cell of cells) {
if (cell.cell_type === "raw") {
const src = cell.source.value.replaceAll(/^---$/mg, "");
let yaml;
try {
yaml = parse(src);
} catch (e) {
if (e.message.includes("unknown tag")) {
// assume it's not necessary to guess the format
continue;
}
}
if (yaml && typeof yaml === "object") {
// deno-lint-ignore no-explicit-any
const format = (yaml as Record<string, any>).format;
if (typeof format === "object") {
for (
const [k, _] of Object.entries(
// deno-lint-ignore no-explicit-any
(yaml as Record<string, any>).format || {},
)
) {
formats.add(k);
}
} else if (typeof format === "string") {
formats.add(format);
}
}
}
}
return Array.from(formats);
}
//deno-lint-ignore no-explicit-any
function hasTestSpecs(metadata: any, input: string): boolean {
const hasTestSpecs = metadata?.["_quarto"]?.["tests"] != undefined
if (!hasTestSpecs && metadata?.["_quarto"]?.["test"] != undefined) {
throw new Error(`Test is ${input} is using 'test' in metadata instead of 'tests'. This is probably a typo.`);
}
return hasTestSpecs
}
interface QuartoInlineTestSpec {
format: string;
verifyFns: Verify[];
}
function resolveTestSpecs(
input: string,
// deno-lint-ignore no-explicit-any
metadata: Record<string, any>,
): QuartoInlineTestSpec[] {
const specs = metadata["_quarto"]["tests"];
const result = [];
// deno-lint-ignore no-explicit-any
const verifyMap: Record<string, any> = {
ensureHtmlElements,
ensureFileRegexMatches,
ensureLatexFileRegexMatches,
ensureTypstFileRegexMatches,
ensureDocxRegexMatches,
ensureDocxXpath,
ensureOdtXpath,
ensureJatsXpath,
ensurePdfRegexMatches,
ensurePptxRegexMatches,
ensurePptxXpath,
ensurePptxLayout,
ensurePptxMaxSlides,
ensureSnapshotMatches,
printsMessage
};
for (const [format, testObj] of Object.entries(specs)) {
let checkWarnings = true;
const verifyFns: Verify[] = [];
if (testObj && typeof testObj === "object") {
for (
// deno-lint-ignore no-explicit-any
const [key, value] of Object.entries(testObj as Record<string, any>)
) {
if (key === "noErrors") {
checkWarnings = false;
verifyFns.push(noErrors);
} else {
// See if there is a project and grab it's type
const projectPath = findRootTestsProjectDir(input)
const projectOutDir = findProjectOutputDir(projectPath);
const outputFile = outputForInput(input, format, projectOutDir, projectPath, metadata);
if (key === "fileExists") {
for (
const [path, file] of Object.entries(
value as Record<string, string>,
)
) {
if (path === "outputPath") {
verifyFns.push(
fileExists(join(dirname(outputFile.outputPath), file)),
);
} else if (path === "supportPath") {
verifyFns.push(
fileExists(join(outputFile.supportPath, file)),
);
}
}
} else if (["ensurePptxLayout", "ensurePptxXpath"].includes(key)) {
if (Array.isArray(value) && Array.isArray(value[0])) {
// several slides to check
value.forEach((slide: any) => {
verifyFns.push(verifyMap[key](outputFile.outputPath, ...slide));
});
} else {
verifyFns.push(verifyMap[key](outputFile.outputPath, ...value));
}
} else if (key === "printsMessage") {
verifyFns.push(verifyMap[key](value));
} else if (verifyMap[key]) {
// FIXME: We should find another way that having this requirement of keep-* in the metadata
if (key === "ensureTypstFileRegexMatches") {
if (!metadata.format?.typst?.['keep-typ'] && !metadata['keep-typ']) {
throw new Error(`Using ensureTypstFileRegexMatches requires setting 'keep-typ: true' in file ${input}`);
}
} else if (key === "ensureLatexFileRegexMatches") {
if (!metadata.format?.pdf?.['keep-tex'] && !metadata['keep-tex']) {
throw new Error(`Using ensureLatexFileRegexMatches requires setting 'keep-tex: true' in file ${input}`);
}
}
if (typeof value === "object") {
verifyFns.push(verifyMap[key](outputFile.outputPath, ...value));
} else {
verifyFns.push(verifyMap[key](outputFile.outputPath, value));
}
} else {
throw new Error(`Unknown verify function used: ${key} in file ${input} for format ${format}`) ;
}
}
}
}
if (checkWarnings) {
verifyFns.push(noErrorsOrWarnings);
}
result.push({
format,
verifyFns,
});
}
return result;
}
await initYamlIntelligenceResourcesFromFilesystem();
// Ideally we'd just walk the one single glob here,
// but because smoke-all.test.ts ends up being called
// from a number of different places (including different shell
// scripts run under a variety of shells), it's
// actually non-trivial to guarantee that we'll see a single
// unexpanded glob pattern. So we assume that a pattern
// might have already been expanded here, and we also
// accommodate cases where it hasn't been expanded.
//
// (Do note that this means that files that don't exist will
// be silently ignored.)
const files: WalkEntry[] = [];
if (Deno.args.length === 0) {
// ignore file starting with `_`
files.push(...[...expandGlobSync("docs/smoke-all/**/*.{md,qmd,ipynb}")].filter((entry) => /^[^_]/.test(basename(entry.path))));
} else {
for (const arg of Deno.args) {
files.push(...expandGlobSync(arg));
}
}
// To store project path we render before testing file testSpecs
const renderedProjects: Set<string> = new Set();
// To store information of all the project we render so that we can cleanup after testing
const testedProjects: Set<string> = new Set();
// Create an array to hold all the promises for the tests of files
let testFilesPromises = [];
for (const { path: fileName } of files) {
const input = relative(Deno.cwd(), fileName);
const metadata = input.endsWith("md") // qmd or md
? readYamlFromMarkdown(Deno.readTextFileSync(input))
: readYamlFromMarkdown(await jupyterNotebookToMarkdown(input, false));
const testSpecs: QuartoInlineTestSpec[] = [];
if (hasTestSpecs(metadata, input)) {
testSpecs.push(...resolveTestSpecs(input, metadata));
} else {
const formats = await guessFormat(input);
if (formats.length == 0) {
formats.push("html");
}
for (const format of formats) {
testSpecs.push({ format: format, verifyFns: [noErrorsOrWarnings] });
}
}
// Get project path for this input and store it if this is a project (used for cleaning)
const projectPath = findRootTestsProjectDir(input);
if (projectPath) testedProjects.add(projectPath);
// Render project before testing individual document if required
if (
(metadata["_quarto"] as any)?.["render-project"] &&
projectPath &&
!renderedProjects.has(projectPath)
) {
await quarto(["render", projectPath]);
renderedProjects.add(projectPath);
}
testFilesPromises.push(new Promise<void>(async (resolve, reject) => {
try {
// Create an array to hold all the promises for the testSpecs
let testSpecPromises = [];
for (const testSpec of testSpecs) {
const {
format,
verifyFns,
//deno-lint-ignore no-explicit-any
} = testSpec as any;
testSpecPromises.push(new Promise<void>((testSpecResolve, testSpecReject) => {
try {
if (format === "editor-support-crossref") {
const tempFile = Deno.makeTempFileSync();
testQuartoCmd("editor-support", ["crossref", "--input", input, "--output", tempFile], verifyFns, {
teardown: () => {
Deno.removeSync(tempFile);
testSpecResolve(); // Resolve the promise for the testSpec
return Promise.resolve();
}
}, `quarto editor-support crossref < ${input}`);
} else {
testQuartoCmd("render", [input, "--to", format], verifyFns, {
prereq: async () => {
setInitializer(fullInit);
await initState();
return Promise.resolve(true);
},
teardown: () => {
cleanoutput(input, format, undefined, undefined, metadata);
testSpecResolve(); // Resolve the promise for the testSpec
return Promise.resolve();
},
});
}
} catch (error) {
testSpecReject(error);
}
}));
}
// Wait for all the promises to resolve
await Promise.all(testSpecPromises);
// Resolve the promise for the file
resolve();
} catch (error) {
reject(error);
}
}));
}
// Wait for all the promises to resolve
// Meaning all the files have been tested and we can clean
Promise.all(testFilesPromises).then(() => {
// Clean up any projects that were tested
for (const project of testedProjects) {
// Clean project output directory
const projectOutDir = join(project, findProjectOutputDir(project));
if (projectOutDir !== project && safeExistsSync(projectOutDir)) {
safeRemoveSync(projectOutDir, { recursive: true });
}
// Clean hidden .quarto directory
const hiddenQuarto = join(project, ".quarto");
if (safeExistsSync(hiddenQuarto)) {
safeRemoveSync(hiddenQuarto, { recursive: true });
}
}
}).catch((_error) => {});
function findRootTestsProjectDir(input: string) {
const smokeAllRootDir = 'smoke-all$'
const ffMatrixRootDir = 'feature-format-matrix[/]qmd-files$'
const RootTestsRegex = new RegExp(`${smokeAllRootDir}|${ffMatrixRootDir}`);
return findProjectDir(input, RootTestsRegex);
}