Skip to content

Commit c973bb9

Browse files
committed
smoke-all - Add a way to add post render cleanup file from YAML
Using something like ``` _quarto: tests: html: shouldError: default postRenderCleanup: - '${input_stem}.quarto_ipynb' ``` would clean the intermediate file , which is not cleaned by default as render is erroring.
1 parent 3ccc212 commit c973bb9

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

tests/docs/smoke-all/jupyter/error/display-error-false.qmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ _quarto:
55
tests:
66
html:
77
shouldError: default
8+
postRenderCleanup:
9+
- '${input_stem}.quarto_ipynb'
810
---
911

1012
With default setting, this document should error at rendering because of Exception at IPython.display level.

tests/smoke/smoke-all.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ interface QuartoInlineTestSpec {
100100
verifyFns: Verify[];
101101
}
102102

103+
// Functions to cleanup leftover testing
104+
const postRenderCleanupFiles: string[] = [];
105+
function registerPostRenderCleanupFile(file: string): void {
106+
postRenderCleanupFiles.push(file);
107+
}
108+
const postRenderCleanup = () => {
109+
for (const file of postRenderCleanupFiles) {
110+
console.log(`Cleaning up ${file} in ${Deno.cwd()}`);
111+
if (safeExistsSync(file)) {
112+
Deno.removeSync(file);
113+
}
114+
}
115+
}
116+
103117
function resolveTestSpecs(
104118
input: string,
105119
// deno-lint-ignore no-explicit-any
@@ -135,7 +149,20 @@ function resolveTestSpecs(
135149
// deno-lint-ignore no-explicit-any
136150
const [key, value] of Object.entries(testObj as Record<string, any>)
137151
) {
138-
if (key == "shouldError") {
152+
if (key == "postRenderCleanup") {
153+
// This is a special key to register cleanup operations
154+
// each entry is a file to cleanup relative to the input file
155+
for (let file of value) {
156+
// if value has `${input_stem}` in the string, replace by input_stem value (input file name without extension)
157+
if (file.includes("${input_stem}")) {
158+
const extension = input.endsWith('.qmd') ? '.qmd' : '.ipynb';
159+
const inputStem = basename(input, extension);
160+
file = file.replace("${input_stem}", inputStem);
161+
}
162+
// file is registered for cleanup in testQuartoCmd teardown step
163+
registerPostRenderCleanupFile(join(dirname(input), file));
164+
}
165+
} else if (key == "shouldError") {
139166
checkWarnings = false;
140167
verifyFns.push(shouldError);
141168
} else if (key === "noErrors") {
@@ -306,6 +333,7 @@ for (const { path: fileName } of files) {
306333
},
307334
teardown: () => {
308335
cleanoutput(input, format, undefined, undefined, metadata);
336+
postRenderCleanup()
309337
testSpecResolve(); // Resolve the promise for the testSpec
310338
return Promise.resolve();
311339
},

0 commit comments

Comments
 (0)