Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/smoke/render/render.ts
12925 views
1
/*
2
* render.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*
6
*/
7
import { basename, join } from "../../../src/deno_ral/path.ts";
8
9
import { outputForInput } from "../../utils.ts";
10
import { TestContext, testQuartoCmd, Verify } from "../../test.ts";
11
import {
12
ensureFileRegexMatches,
13
hasSupportingFiles,
14
noSupportingFiles,
15
outputCreated,
16
} from "../../verify.ts";
17
import { safeRemoveSync } from "../../../src/core/path.ts";
18
import { safeExistsSync } from "../../../src/core/path.ts";
19
import { assert } from "testing/asserts";
20
21
export function testSimpleIsolatedRender(
22
file: string,
23
to: string,
24
noSupporting: boolean,
25
) {
26
const dir = Deno.makeTempDirSync();
27
const tempInput = join(dir, basename(file));
28
Deno.copyFileSync(file, tempInput);
29
testRender(tempInput, to, noSupporting, [], {
30
teardown: () => {
31
return Deno.remove(dir, { recursive: true });
32
},
33
});
34
}
35
36
export function testRender(
37
input: string,
38
to: string,
39
noSupporting: boolean,
40
addtlVerify?: Verify[],
41
context?: TestContext,
42
args?: string[],
43
projectOutDir?: string
44
) {
45
// Verify that the output was created and
46
// that supporting files are present or missing
47
const verify: Verify[] = [];
48
// If we're not rendering a folder but a single document, add some more assertions
49
if (!input.match(/[\\/]$/)) {
50
verify.push(outputCreated(input, to, projectOutDir));
51
if (noSupporting) {
52
verify.push(noSupportingFiles(input, to, projectOutDir));
53
} else {
54
verify.push(hasSupportingFiles(input, to, projectOutDir));
55
}
56
}
57
if (addtlVerify) {
58
verify.push(...addtlVerify);
59
}
60
context = context || {};
61
62
// Run the command
63
testQuartoCmd(
64
"render",
65
[input, "--to", to, ...(args || [])],
66
verify,
67
{
68
...context,
69
setup: async () => {
70
if (context?.setup) {
71
await context?.setup();
72
}
73
assert(safeExistsSync(input), `Input file ${input} does not exist. Test could not be ran.`);
74
},
75
teardown: async () => {
76
if (context?.teardown) {
77
await context?.teardown();
78
}
79
cleanoutput(input, to);
80
},
81
},
82
);
83
}
84
85
export function cleanoutput(
86
input: string,
87
to: string,
88
projectOutDir?: string,
89
projectRoot?: string,
90
// deno-lint-ignore no-explicit-any
91
metadata?: Record<string, any>,
92
) {
93
if (Deno.env.get("QUARTO_TEST_KEEP_OUTPUTS")) {
94
return;
95
}
96
const out = outputForInput(input, to, projectOutDir, projectRoot, metadata);
97
if (safeExistsSync(out.outputPath)) {
98
safeRemoveSync(out.outputPath);
99
}
100
if (safeExistsSync(out.supportPath)) {
101
safeRemoveSync(out.supportPath, { recursive: true });
102
}
103
}
104
105
export const renderVerifyLatexOutput = (
106
input: string,
107
matches: RegExp[],
108
noMatches?: RegExp[],
109
) => {
110
const teXOutput = outputForInput(input, "latex");
111
testRender(input, "latex", true, [
112
ensureFileRegexMatches(teXOutput.outputPath, matches, noMatches),
113
]);
114
};
115
116