Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/smoke/render/render-freeze.test.ts
12921 views
1
/*
2
* render-freeze.test.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*
6
*/
7
import { stringify } from "../../../src/core/yaml.ts";
8
import { dirname, join } from "../../../src/deno_ral/path.ts";
9
import { assert } from "testing/asserts";
10
11
import { Metadata } from "../../../src/config/types.ts";
12
import { removeIfEmptyDir } from "../../../src/core/path.ts";
13
import { quarto } from "../../../src/quarto.ts";
14
import { ExecuteOutput, Verify } from "../../test.ts";
15
import { outputCreated } from "../../verify.ts";
16
import { testRender } from "./render.ts";
17
18
const regex = /output file: .*\.knit\.md/m;
19
20
const testFileName = "freeze-test";
21
const tempDir = Deno.makeTempDirSync();
22
const path = join(tempDir, `${testFileName}.qmd`);
23
const baseMeta: Metadata = {
24
title: "Freeze Test",
25
format: "html",
26
freeze: true,
27
};
28
const baseMarkdown: string[] = [
29
"## Freeze Testing Document",
30
"",
31
"```{r}",
32
"plot(cars)",
33
"```",
34
"",
35
];
36
37
const useFrozen = {
38
name: "Ensure using frozen execution",
39
verify: (output: ExecuteOutput[]) => {
40
output.forEach((msg) => {
41
if (msg.levelName === "INFO") {
42
assert(
43
!msg.msg.match(regex),
44
"Document was executed when it should've used the frozen result",
45
);
46
}
47
});
48
return Promise.resolve();
49
},
50
};
51
52
const ignoreFrozen = {
53
name: "Ensure ignoring frozen execution",
54
verify: (output: ExecuteOutput[]) => {
55
const matches = output.some((msg) => {
56
if (msg.levelName === "INFO") {
57
return msg.msg.match(regex);
58
}
59
});
60
assert(
61
matches,
62
"Document was not executed when it should have been",
63
);
64
return Promise.resolve();
65
},
66
};
67
68
const projectOutputExists: Verify = {
69
name: "Make sure project output exists",
70
verify: (_output: ExecuteOutput[]) => {
71
outputCreated(path, "html");
72
return Promise.resolve();
73
},
74
};
75
76
async function writeFile(
77
path: string,
78
frontMatter: Metadata,
79
markdown: string[],
80
) {
81
const yamlStr = stringify(frontMatter);
82
await Deno.writeTextFile(
83
path,
84
["---", yamlStr, "---", "", ...markdown].join("\n"),
85
);
86
}
87
88
function testFileContext(
89
path: string,
90
frontMatter: Metadata,
91
markdown: string[],
92
) {
93
const dir = dirname(path);
94
const quartoProj = join(dir, "_quarto.yml");
95
return {
96
setup: async () => {
97
// Write a quarto project
98
await Deno.writeTextFile(
99
quartoProj,
100
"title: 'Hello Project'\nproject:\n type: default\n",
101
);
102
103
// Write the test file
104
await writeFile(
105
path,
106
frontMatter,
107
markdown,
108
);
109
110
await quarto(["render", path]);
111
},
112
teardown: async () => {
113
// Clean up the test file
114
await Deno.remove(path);
115
await Deno.remove(quartoProj);
116
117
// Get rid of the freezer
118
const freezerDir = join(dirname(path), "_freeze");
119
Deno.removeSync(join(freezerDir, testFileName), { recursive: true });
120
121
// Maybe clean up empty freeze dir
122
removeIfEmptyDir(freezerDir);
123
},
124
};
125
}
126
127
const testContext = testFileContext(path, baseMeta, baseMarkdown);
128
// Reader and use the freezer (freeze, auto)
129
testRender(
130
dirname(path) + "/",
131
"html",
132
false,
133
[projectOutputExists, useFrozen],
134
{
135
name: "clean fzr - auto",
136
...testContext,
137
},
138
);
139
140
// Render and mutate to be sure we ignore freezer
141
testRender(
142
dirname(path) + "/",
143
"html",
144
false,
145
[projectOutputExists, ignoreFrozen],
146
{
147
name: "dirty fzr - auto",
148
setup: async () => {
149
await testContext.setup();
150
151
// mutate the file
152
writeFile(path, { ...baseMeta, freeze: "auto" }, [
153
...baseMarkdown,
154
"",
155
"```{r}",
156
"head(cars)",
157
"```",
158
"",
159
]);
160
},
161
teardown: testContext.teardown,
162
},
163
);
164
165
// Render and mutate to be sure we ignore freezer
166
testRender(
167
dirname(path) + "/",
168
"html",
169
false,
170
[projectOutputExists, useFrozen],
171
{
172
name: "dirty fzr - freeze",
173
setup: async () => {
174
await testContext.setup();
175
176
// mutate the file
177
writeFile(path, baseMeta, [
178
...baseMarkdown,
179
"",
180
"```{r}",
181
"head(cars)",
182
"```",
183
"",
184
]);
185
},
186
teardown: testContext.teardown,
187
},
188
);
189
190