|
| 1 | +/* |
| 2 | + * cleanup.test.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2026 Posit Software, PBC |
| 5 | + */ |
| 6 | + |
| 7 | +import { assert } from "testing/asserts"; |
| 8 | +import { ensureDirSync, existsSync } from "../../../src/deno_ral/fs.ts"; |
| 9 | +import { join } from "../../../src/deno_ral/path.ts"; |
| 10 | +import { unitTest } from "../../test.ts"; |
| 11 | +import { renderCleanup } from "../../../src/command/render/cleanup.ts"; |
| 12 | +import { Format } from "../../../src/config/types.ts"; |
| 13 | +import { createMockProjectContext } from "../project/utils.ts"; |
| 14 | + |
| 15 | +// Minimal ipynb format: ipynb is a self-contained (standalone) output, so the |
| 16 | +// self-contained cleanup path in render.ts passes the supporting dirs here. |
| 17 | +function ipynbFormat(): Format { |
| 18 | + return { |
| 19 | + pandoc: { to: "ipynb" }, |
| 20 | + execute: {}, |
| 21 | + render: {}, |
| 22 | + metadata: {}, |
| 23 | + language: {}, |
| 24 | + } as unknown as Format; |
| 25 | +} |
| 26 | + |
| 27 | +// Regression test for #14613. |
| 28 | +// |
| 29 | +// On a manuscript first render with freeze:auto, the ipynb completion triggers |
| 30 | +// the self-contained supporting cleanup. The supporting dir reported by the |
| 31 | +// engine uses forward slashes; renderCleanup normalizes the computed filesDir |
| 32 | +// with normalizePath (backslashes on Windows). The separator mismatch made the |
| 33 | +// equality check fail, so the parent index_files dir (instead of the narrowed |
| 34 | +// figure-<to> dir) was handed to safeRemoveDirSync, deleting ALL figures. |
| 35 | +// Subsequent format completions then found no index_files and copied nothing |
| 36 | +// into _manuscript. |
| 37 | +// |
| 38 | +// deno-lint-ignore require-await |
| 39 | +unitTest("renderCleanup - narrows ipynb supporting despite path separator mismatch (#14613)", async () => { |
| 40 | + const projectDir = Deno.makeTempDirSync({ prefix: "quarto-cleanup-test" }); |
| 41 | + const project = createMockProjectContext({ dir: projectDir }); |
| 42 | + try { |
| 43 | + const input = join(projectDir, "index.qmd"); |
| 44 | + Deno.writeTextFileSync(input, ""); |
| 45 | + |
| 46 | + const filesDir = join(projectDir, "index_files"); |
| 47 | + const figureHtmlDir = join(filesDir, "figure-html"); |
| 48 | + const figureIpynbDir = join(filesDir, "figure-ipynb"); |
| 49 | + ensureDirSync(figureHtmlDir); |
| 50 | + ensureDirSync(figureIpynbDir); |
| 51 | + const figureHtmlPlot = join(figureHtmlDir, "plot.png"); |
| 52 | + Deno.writeTextFileSync(figureHtmlPlot, "html-figure"); |
| 53 | + Deno.writeTextFileSync(join(figureIpynbDir, "plot.png"), "ipynb-figure"); |
| 54 | + |
| 55 | + // Engine reports the supporting dir with forward slashes (as knitr does). |
| 56 | + const supportingForwardSlash = filesDir.replaceAll("\\", "/"); |
| 57 | + |
| 58 | + renderCleanup( |
| 59 | + input, |
| 60 | + join(projectDir, "_manuscript", "index.out.ipynb"), |
| 61 | + ipynbFormat(), |
| 62 | + project, |
| 63 | + [supportingForwardSlash], |
| 64 | + ); |
| 65 | + |
| 66 | + // Cleanup must narrow to figure-ipynb only; the html figures the other |
| 67 | + // manuscript formats depend on must survive. |
| 68 | + assert( |
| 69 | + existsSync(figureHtmlPlot), |
| 70 | + "index_files/figure-html must survive ipynb supporting cleanup", |
| 71 | + ); |
| 72 | + assert( |
| 73 | + !existsSync(figureIpynbDir), |
| 74 | + "index_files/figure-ipynb should be removed by narrowed cleanup", |
| 75 | + ); |
| 76 | + } finally { |
| 77 | + project.cleanup(); |
| 78 | + try { |
| 79 | + Deno.removeSync(projectDir, { recursive: true }); |
| 80 | + } catch { |
| 81 | + // ignore |
| 82 | + } |
| 83 | + } |
| 84 | +}); |
0 commit comments