Skip to content

Commit 47577cd

Browse files
committed
test specs in qmd files
1 parent d906731 commit 47577cd

File tree

2 files changed

+97
-11
lines changed

2 files changed

+97
-11
lines changed

src/command/render/render-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
RenderFilesResult,
5656
RenderOptions,
5757
} from "./types.ts";
58-
import { error, info } from "log/mod.ts";
58+
import { error, info, warning } from "log/mod.ts";
5959
import * as ld from "../../core/lodash.ts";
6060
import { basename, dirname, join, relative } from "path/mod.ts";
6161
import { Format } from "../../config/types.ts";

tests/smoke/smoke-all.test.ts

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { expandGlobSync } from "fs/mod.ts";
9-
import { testQuartoCmd } from "../test.ts";
9+
import { testQuartoCmd, Verify } from "../test.ts";
1010

1111
import { initYamlIntelligenceResourcesFromFilesystem } from "../../src/core/schema/utils.ts";
1212
import {
@@ -17,7 +17,15 @@ import {
1717
import { breakQuartoMd } from "../../src/core/lib/break-quarto-md.ts";
1818
import { parse } from "encoding/yaml.ts";
1919
import { cleanoutput } from "./render/render.ts";
20-
import { noErrorsOrWarnings } from "../verify.ts";
20+
import {
21+
ensureDocxRegexMatches,
22+
ensureFileRegexMatches,
23+
ensureHtmlElements,
24+
noErrors,
25+
noErrorsOrWarnings,
26+
} from "../verify.ts";
27+
import { readYamlFromMarkdown } from "../../src/core/yaml.ts";
28+
import { outputForInput } from "../utils.ts";
2129

2230
async function fullInit() {
2331
await initYamlIntelligenceResourcesFromFilesystem();
@@ -52,21 +60,99 @@ async function guessFormat(fileName: string): Promise<string[]> {
5260
return Array.from(formats);
5361
}
5462

55-
for (
56-
const { path: fileName } of expandGlobSync(
63+
//deno-lint-ignore no-explicit-any
64+
function hasTestSpecs(metadata: any): boolean {
65+
return metadata?.["_quarto"]?.["tests"] != undefined;
66+
}
67+
68+
interface QuartoInlineTestSpec {
69+
format: string;
70+
verifyFns: Verify[];
71+
}
72+
73+
function resolveTestSpecs(
74+
input: string,
75+
// deno-lint-ignore no-explicit-any
76+
metadata: Record<string, any>,
77+
): QuartoInlineTestSpec[] {
78+
const specs = metadata["_quarto"]["tests"];
79+
80+
const result = [];
81+
// deno-lint-ignore no-explicit-any
82+
const verifyMap: Record<string, any> = {
83+
ensureHtmlElements,
84+
ensureFileRegexMatches,
85+
ensureDocxRegexMatches,
86+
};
87+
88+
for (const [format, testObj] of Object.entries(specs)) {
89+
let checkWarnings = true;
90+
const verifyFns: Verify[] = [];
91+
if (testObj) {
92+
for (
93+
// deno-lint-ignore no-explicit-any
94+
const [key, value] of Object.entries(testObj as Record<string, any>)
95+
) {
96+
if (key === "noErrors") {
97+
console.log("NO ERRORS!!!");
98+
checkWarnings = false;
99+
verifyFns.push(noErrors);
100+
} else {
101+
if (verifyMap[key]) {
102+
const outputFile = outputForInput(input, format);
103+
verifyFns.push(verifyMap[key](outputFile.outputPath, ...value));
104+
}
105+
}
106+
}
107+
}
108+
if (checkWarnings) {
109+
console.log("added no errors or warnings");
110+
verifyFns.push(noErrorsOrWarnings);
111+
}
112+
113+
result.push({
114+
format,
115+
verifyFns,
116+
});
117+
}
118+
return result;
119+
}
120+
121+
const globOutput = Deno.args.length
122+
? expandGlobSync(Deno.args[0])
123+
: expandGlobSync(
57124
"docs/smoke-all/**/*.qmd",
58-
)
125+
);
126+
127+
for (
128+
const { path: fileName } of globOutput
59129
) {
60130
const input = fileName;
61131

62-
const formats = await guessFormat(input);
132+
const metadata = readYamlFromMarkdown(Deno.readTextFileSync(input));
133+
const testSpecs = [];
134+
135+
if (hasTestSpecs(metadata)) {
136+
testSpecs.push(...resolveTestSpecs(input, metadata));
137+
} else {
138+
const formats = await guessFormat(input);
63139

64-
if (formats.length == 0) {
65-
formats.push("html");
140+
if (formats.length == 0) {
141+
formats.push("html");
142+
}
143+
for (const format of formats) {
144+
testSpecs.push({ format: format, verifyFns: [noErrorsOrWarnings] });
145+
}
66146
}
67147

68-
for (const format of formats) {
69-
testQuartoCmd("render", [input, "--to", format], [noErrorsOrWarnings], {
148+
for (const testSpec of testSpecs) {
149+
const {
150+
format,
151+
verifyFns,
152+
//deno-lint-ignore no-explicit-any
153+
} = testSpec as any;
154+
155+
testQuartoCmd("render", [input, "--to", format], verifyFns, {
70156
prereq: async () => {
71157
setInitializer(fullInit);
72158
await initState();

0 commit comments

Comments
 (0)