-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathsmoke-all.test.ts
More file actions
521 lines (475 loc) · 18.1 KB
/
smoke-all.test.ts
File metadata and controls
521 lines (475 loc) · 18.1 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* 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 { os } from "../../src/deno_ral/platform.ts";
import { asArray } from "../../src/core/array.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 {
ensureCssRegexMatches,
ensureEpubFileRegexMatches,
ensureDocxRegexMatches,
ensureDocxXpath,
ensureFileRegexMatches,
ensureHtmlElements,
ensureIpynbCellMatches,
ensurePdfRegexMatches,
ensurePdfTextPositions,
ensurePdfMetadata,
ensureJatsXpath,
ensureOdtXpath,
ensurePptxRegexMatches,
ensureTypstFileRegexMatches,
ensureSnapshotMatches,
fileExists,
fileNotExists,
noErrors,
noErrorsOrWarnings,
ensurePptxXpath,
ensurePptxLayout,
ensurePptxMaxSlides,
ensureLatexFileRegexMatches,
printsMessage,
shouldError,
ensureHtmlElementContents,
ensureHtmlElementCount,
ensureLlmsMdRegexMatches,
ensureLlmsMdExists,
ensureLlmsMdDoesNotExist,
ensureLlmsTxtRegexMatches,
ensureLlmsTxtExists,
ensureLlmsTxtDoesNotExist,
} 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";
import { runningInCI } from "../../src/core/ci-info.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 instanceof Error)) throw 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);
}
function skipTest(metadata: Record<string, any>): string | undefined {
// deno-lint-ignore no-explicit-any
const quartoMeta = metadata["_quarto"] as any;
const runConfig = quartoMeta?.tests?.run;
// No run config means run everywhere
if (!runConfig) {
return undefined;
}
// Check explicit skip with message
if (runConfig.skip) {
return typeof runConfig.skip === "string" ? runConfig.skip : "tests.run.skip is true";
}
// Check CI
if (runningInCI() && runConfig.ci === false) {
return "tests.run.ci is false";
}
// Check OS blacklist (not_os)
const notOs = runConfig.not_os;
if (notOs !== undefined && asArray(notOs).includes(os)) {
return `tests.run.not_os includes ${os}`;
}
// Check OS whitelist (os) - if specified, must match
const onlyOs = runConfig.os;
if (onlyOs !== undefined && !asArray(onlyOs).includes(os)) {
return `tests.run.os does not include ${os}`;
}
return undefined;
}
//deno-lint-ignore no-explicit-any
function hasTestSpecs(metadata: any, input: string): boolean {
const tests = metadata?.["_quarto"]?.["tests"];
if (!tests && metadata?.["_quarto"]?.["test"] != undefined) {
throw new Error(`Test is ${input} is using 'test' in metadata instead of 'tests'. This is probably a typo.`);
}
// Check if tests has any format specs (keys other than 'run')
if (tests && typeof tests === "object") {
const formatKeys = Object.keys(tests).filter(key => key !== "run");
return formatKeys.length > 0;
}
return false;
}
interface QuartoInlineTestSpec {
format: string;
verifyFns: Verify[];
}
// Functions to cleanup leftover testing
const postRenderCleanupFiles: string[] = [];
function registerPostRenderCleanupFile(file: string): void {
postRenderCleanupFiles.push(file);
}
const postRenderCleanup = () => {
if (Deno.env.get("QUARTO_TEST_KEEP_OUTPUTS")) {
return;
}
for (const file of postRenderCleanupFiles) {
console.log(`Cleaning up ${file} in ${Deno.cwd()}`);
if (safeExistsSync(file)) {
Deno.removeSync(file);
}
}
}
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> = {
ensureCssRegexMatches,
ensureEpubFileRegexMatches,
ensureHtmlElements,
ensureHtmlElementContents,
ensureHtmlElementCount,
ensureFileRegexMatches,
ensureIpynbCellMatches,
ensureLatexFileRegexMatches,
ensureTypstFileRegexMatches,
ensureDocxRegexMatches,
ensureDocxXpath,
ensureOdtXpath,
ensureJatsXpath,
ensurePdfRegexMatches,
ensurePdfTextPositions,
ensurePdfMetadata,
ensurePptxRegexMatches,
ensurePptxXpath,
ensurePptxLayout,
ensurePptxMaxSlides,
ensureSnapshotMatches,
printsMessage,
ensureLlmsMdRegexMatches,
ensureLlmsMdExists,
ensureLlmsMdDoesNotExist,
ensureLlmsTxtRegexMatches,
ensureLlmsTxtExists,
ensureLlmsTxtDoesNotExist,
};
for (const [format, testObj] of Object.entries(specs)) {
// Skip the 'run' key - it's not a format
if (format === "run") {
continue;
}
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 == "postRenderCleanup") {
// This is a special key to register cleanup operations
// each entry is a file to cleanup relative to the input file
for (let file of value) {
// if value has `${input_stem}` in the string, replace by input_stem value (input file name without extension)
if (file.includes("${input_stem}")) {
const extension = input.endsWith('.qmd') ? '.qmd' : '.ipynb';
const inputStem = basename(input, extension);
file = file.replace("${input_stem}", inputStem);
}
// file is registered for cleanup in testQuartoCmd teardown step
registerPostRenderCleanupFile(join(dirname(input), file));
}
} else if (key == "shouldError") {
checkWarnings = false;
verifyFns.push(shouldError);
} else if (key === "noErrors") {
checkWarnings = false;
verifyFns.push(noErrors);
} else if (key === "noErrorsOrWarnings") {
checkWarnings = false;
verifyFns.push(noErrorsOrWarnings);
} 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 (key === "fileNotExists") {
for (
const [path, file] of Object.entries(
value as Record<string, string>,
)
) {
if (path === "outputPath") {
verifyFns.push(
fileNotExists(join(dirname(outputFile.outputPath), file)),
);
} else if (path === "supportPath") {
verifyFns.push(
fileNotExists(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") {
// Support both single object and array of printsMessage checks
const messages = Array.isArray(value) ? value : [value];
for (const msg of messages) {
verifyFns.push(verifyMap[key](msg));
}
} else if (key === "ensureEpubFileRegexMatches") {
// this ensure function is special because it takes an array of path + regex specifiers,
// so we should never use the spread operator
verifyFns.push(verifyMap[key](outputFile.outputPath, 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'] && metadata.format?.typst?.['output-ext'] !== 'typ' && metadata['output-ext'] !== '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}`);
}
}
// keep-typ/keep-tex files are alongside source, so pass input path
// But output-ext: typ puts files in output directory, so don't pass input path
const usesKeepTyp = key === "ensureTypstFileRegexMatches" &&
(metadata.format?.typst?.['keep-typ'] || metadata['keep-typ']) &&
!(metadata.format?.typst?.['output-ext'] === 'typ' || metadata['output-ext'] === 'typ');
const usesKeepTex = key === "ensureLatexFileRegexMatches" &&
(metadata.format?.pdf?.['keep-tex'] || metadata['keep-tex']);
const needsInputPath = usesKeepTyp || usesKeepTex;
// For book projects, use intermediateTypstPath (index.typ at project root)
// instead of the output path (which would be _book/BookTitle.typ)
let targetPath = outputFile.outputPath;
if (key === "ensureTypstFileRegexMatches" && outputFile.intermediateTypstPath) {
targetPath = outputFile.intermediateTypstPath;
}
if (typeof value === "object" && Array.isArray(value)) {
// value is [matches, noMatches?] - ensure inputFile goes in the right position
const matches = value[0];
const noMatches = value[1];
const inputFile = needsInputPath ? input : undefined;
verifyFns.push(verifyMap[key](targetPath, matches, noMatches, inputFile));
} else {
verifyFns.push(verifyMap[key](targetPath, value, undefined, needsInputPath ? input : undefined));
}
} 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 skipReason = skipTest(metadata);
if (skipReason !== undefined) {
console.log(`Skipping tests for ${input}: ${skipReason}`);
continue;
}
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);
postRenderCleanup()
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(() => {
if (Deno.env.get("QUARTO_TEST_KEEP_OUTPUTS")) {
return;
}
// 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);
}