Skip to content

Commit d7d968b

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 4a922cc commit d7d968b

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
@@ -92,6 +92,20 @@ interface QuartoInlineTestSpec {
9292
verifyFns: Verify[];
9393
}
9494

95+
// Functions to cleanup leftover testing
96+
const postRenderCleanupFiles: string[] = [];
97+
function registerPostRenderCleanupFile(file: string): void {
98+
postRenderCleanupFiles.push(file);
99+
}
100+
const postRenderCleanup = () => {
101+
for (const file of postRenderCleanupFiles) {
102+
console.log(`Cleaning up ${file} in ${Deno.cwd()}`);
103+
if (safeExistsSync(file)) {
104+
Deno.removeSync(file);
105+
}
106+
}
107+
}
108+
95109
function resolveTestSpecs(
96110
input: string,
97111
// deno-lint-ignore no-explicit-any
@@ -127,7 +141,20 @@ function resolveTestSpecs(
127141
// deno-lint-ignore no-explicit-any
128142
const [key, value] of Object.entries(testObj as Record<string, any>)
129143
) {
130-
if (key == "shouldError") {
144+
if (key == "postRenderCleanup") {
145+
// This is a special key to register cleanup operations
146+
// each entry is a file to cleanup relative to the input file
147+
for (let file of value) {
148+
// if value has `${input_stem}` in the string, replace by input_stem value (input file name without extension)
149+
if (file.includes("${input_stem}")) {
150+
const extension = input.endsWith('.qmd') ? '.qmd' : '.ipynb';
151+
const inputStem = basename(input, extension);
152+
file = file.replace("${input_stem}", inputStem);
153+
}
154+
// file is registered for cleanup in testQuartoCmd teardown step
155+
registerPostRenderCleanupFile(join(dirname(input), file));
156+
}
157+
} else if (key == "shouldError") {
131158
checkWarnings = false;
132159
verifyFns.push(shouldError);
133160
} else if (key === "noErrors") {
@@ -298,6 +325,7 @@ for (const { path: fileName } of files) {
298325
},
299326
teardown: () => {
300327
cleanoutput(input, format, undefined, undefined, metadata);
328+
postRenderCleanup()
301329
testSpecResolve(); // Resolve the promise for the testSpec
302330
return Promise.resolve();
303331
},

0 commit comments

Comments
 (0)