-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathtest.ts
More file actions
348 lines (307 loc) · 9.49 KB
/
test.ts
File metadata and controls
348 lines (307 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* test.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/
import { existsSync, safeRemoveSync } from "../src/deno_ral/fs.ts";
import { AssertionError, fail } from "testing/asserts";
import { warning } from "../src/deno_ral/log.ts";
import { initDenoDom } from "../src/core/deno-dom.ts";
import { cleanupLogger, initializeLogger, flushLoggers, logError } from "../src/core/log.ts";
import { quarto } from "../src/quarto.ts";
import { join } from "../src/deno_ral/path.ts";
import * as colors from "fmt/colors";
import { runningInCI } from "../src/core/ci-info.ts";
import { relative, fromFileUrl } from "../src/deno_ral/path.ts";
import { quartoConfig } from "../src/core/quarto.ts";
import { isWindows } from "../src/deno_ral/platform.ts";
export interface TestDescriptor {
// The name of the test
name: string;
// Sets up the test
context: TestContext;
// Executes the test
execute: () => Promise<void>;
// Used to verify the outcome of the test
verify: Verify[];
// type of test
type: "smoke" | "unit";
}
export interface TestContext {
name?: string;
// Checks that prereqs for the test are met
prereq?: () => Promise<boolean>;
// Cleans up the test
teardown?: () => Promise<void>;
// Sets up the test
setup?: () => Promise<void>;
// Request that the test be run from another working directory
cwd?: () => string;
// Control of underlying sanitizer
sanitize?: { resources?: boolean; ops?: boolean; exit?: boolean };
// control if test is ran or skipped
ignore?: boolean;
// environment to pass to downstream processes
env?: Record<string, string>;
}
// Allow to merge test contexts in Tests helpers
export function mergeTestContexts(baseContext: TestContext, additionalContext?: TestContext): TestContext {
if (!additionalContext) {
return baseContext;
}
return {
// override name if provided
name: additionalContext.name || baseContext.name,
// combine prereq conditions
prereq: async () => {
const baseResult = !baseContext.prereq || await baseContext.prereq();
const additionalResult = !additionalContext.prereq || await additionalContext.prereq();
return baseResult && additionalResult;
},
// run teardowns in reverse order
teardown: async () => {
if (baseContext.teardown) await baseContext.teardown();
if (additionalContext.teardown) await additionalContext.teardown();
},
// run setups in order
setup: async () => {
if (additionalContext.setup) await additionalContext.setup();
if (baseContext.setup) await baseContext.setup();
},
// override cwd if provided
cwd: additionalContext.cwd || baseContext.cwd,
// merge sanitize options
sanitize: {
resources: additionalContext.sanitize?.resources ?? baseContext.sanitize?.resources,
ops: additionalContext.sanitize?.ops ?? baseContext.sanitize?.ops,
exit: additionalContext.sanitize?.exit ?? baseContext.sanitize?.exit,
},
// override ignore if provided
ignore: additionalContext.ignore ?? baseContext.ignore,
// merge env with additional context taking precedence
env: { ...baseContext.env, ...additionalContext.env },
};
}
export function testQuartoCmd(
cmd: string,
args: string[],
verify: Verify[],
context?: TestContext,
name?: string
) {
if (name === undefined) {
name = `quarto ${cmd} ${args.join(" ")}`;
}
test({
name,
execute: async () => {
const timeout = new Promise((_resolve, reject) => {
setTimeout(reject, 600000, "timed out after 10 minutes");
});
await Promise.race([
quarto([cmd, ...args], undefined, context?.env),
timeout,
]);
},
verify,
context: context || {},
type: "smoke",
});
}
export interface Verify {
name: string;
verify: (outputs: ExecuteOutput[]) => Promise<void>;
}
export interface ExecuteOutput {
msg: string;
level: number;
levelName: string;
}
export function unitTest(
name: string,
ver: () => Promise<unknown>, // VoidFunction,
context?: TestContext,
) {
test({
name,
type: "unit",
context: context || {},
execute: () => {
return Promise.resolve();
},
verify: [
{
name: `${name}`,
verify: async (_outputs: ExecuteOutput[]) => {
const timeout = new Promise((_resolve, reject) => {
setTimeout(() => reject(new AssertionError(`timed out after 2 minutes. Something may be wrong with verify function in the test '${name}'.`)), 120000);
});
await Promise.race([ver(), timeout]);
},
},
],
});
}
export function test(test: TestDescriptor) {
const testName = test.context.name
? `[${test.type}] > ${test.name} (${test.context.name})`
: `[${test.type}] > ${test.name}`;
const sanitizeResources = test.context.sanitize?.resources;
const sanitizeOps = test.context.sanitize?.ops;
const sanitizeExit = test.context.sanitize?.exit;
const ignore = test.context.ignore;
const userSession = !runningInCI();
const args: Deno.TestDefinition = {
name: testName,
async fn(context) {
await initDenoDom();
const runTest = !test.context.prereq || await test.context.prereq();
if (runTest) {
const wd = Deno.cwd();
if (test.context?.cwd) {
Deno.chdir(test.context.cwd());
}
if (test.context.setup) {
await test.context.setup();
}
let cleanedup = false;
const cleanupLogOnce = async () => {
if (!cleanedup) {
await cleanupLogger();
cleanedup = true;
}
};
// Capture the output
const log = Deno.makeTempFileSync({ suffix: ".json" });
const handlers = await initializeLogger({
log: log,
level: "INFO",
format: "json-stream",
quiet: true,
});
const logOutput = (path: string) => {
if (existsSync(path)) {
return readExecuteOutput(path);
} else {
return undefined;
}
};
let lastVerify;
try {
try {
await test.execute();
} catch (e) {
logError(e);
}
// Cleanup the output logging
await cleanupLogOnce();
flushLoggers(handlers);
// Read the output
const testOutput = logOutput(log);
if (testOutput) {
for (const ver of test.verify) {
lastVerify = ver;
if (userSession) {
const verifyMsg = "[verify] > " + ver.name;
console.log(userSession ? colors.dim(verifyMsg) : verifyMsg);
}
await ver.verify(testOutput);
}
}
} catch (ex) {
const border = "-".repeat(80);
const coloredName = userSession
? colors.brightGreen(colors.italic(testName))
: testName;
// Compute an inset based upon the testName
const offset = testName.indexOf(">");
// Form the test runner command
const absPath = isWindows
? fromFileUrl(context.origin)
: (new URL(context.origin)).pathname;
const quartoRoot = join(quartoConfig.binPath(), "..", "..", "..");
const relPath = relative(
join(quartoRoot, "tests"),
absPath,
);
const command = isWindows
? "run-tests.ps1"
: "./run-tests.sh";
const testCommand = `${
offset > 0 ? " ".repeat(offset + 2) : ""
}${command} ${relPath}`;
const coloredTestCommand = userSession
? colors.brightGreen(testCommand)
: testCommand;
const verifyFailed = `[verify] > ${
lastVerify ? lastVerify.name : "unknown"
}`;
const coloredVerify = userSession
? colors.brightGreen(verifyFailed)
: verifyFailed;
const logMessages = logOutput(log);
const output: string[] = [
"",
"",
border,
coloredName,
coloredTestCommand,
"",
coloredVerify,
"",
ex.message,
ex.stack,
"",
];
if (logMessages && logMessages.length > 0) {
output.push("OUTPUT:");
logMessages.forEach((out) => {
const parts = out.msg.split("\n");
parts.forEach((part) => {
output.push(" " + part);
});
});
}
fail(output.join("\n"));
} finally {
safeRemoveSync(log);
await cleanupLogOnce();
if (test.context.teardown) {
await test.context.teardown();
}
if (test.context?.cwd) {
Deno.chdir(wd);
}
}
} else {
warning(`Skipped - ${test.name}`);
}
},
ignore,
sanitizeExit,
sanitizeOps,
sanitizeResources,
};
// work around 1.32.5 bug: https://github.com/denoland/deno/issues/18784
if (args.ignore === undefined) {
delete args.ignore;
}
Deno.test(args);
}
function readExecuteOutput(log: string) {
const jsonStream = Deno.readTextFileSync(log);
const lines = jsonStream.split("\n").filter((line) => !!line);
return lines.map((line) => {
return JSON.parse(line) as ExecuteOutput;
});
}
export const removeFilesTeardown = (fileList: string[]) => {
return {
teardown: async () => {
for (const file of fileList) {
safeRemoveSync(file);
}
}
};
}