-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
testRunner.ts
497 lines (440 loc) · 15.1 KB
/
testRunner.ts
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import * as vscode from 'vscode';
import * as util from './utilities';
import * as string from './util/string';
import * as outputWindow from './repl-window/repl-doc';
import { NReplSession } from './nrepl';
import * as cider from './nrepl/cider';
import * as lsp from './lsp/definitions';
import * as namespace from './namespace';
import { getSession, updateReplSessionType } from './nrepl/repl-session';
import * as getText from './util/get-text';
import * as output from './results-output/output';
const diagnosticCollection = vscode.languages.createDiagnosticCollection('calva');
async function uriForFile(fileName: string): Promise<vscode.Uri> {
if (fileName.startsWith('/')) {
return vscode.Uri.file(fileName);
}
// Sometimes we don't get the full path for some reason. (This is a very inexact
// way of dealing with that. Maybe check for the right `ns`in the file?)
const uris = await vscode.workspace.findFiles('**/' + fileName, null, 1);
return uris[0];
}
// Return a valid TestItem for the namespace.
// Creates a new item if one does not exist, othrwise we find the existing entry.
// If a Range is supplied, that we set the range on the returned item.
function upsertNamespace(
controller: vscode.TestController,
uri: vscode.Uri,
nsName: string,
range?: vscode.Range
): vscode.TestItem {
let ns = controller.items.get(nsName);
if (!ns) {
ns = controller.createTestItem(nsName, nsName, uri);
}
if (range) {
ns.range = range;
}
controller.items.add(ns);
return ns;
}
// Return a valid TestItem for the test var.
// Creates a new item if one does not exist, othrwise we find the existing entry.
// If a Range is supplied, that we set the range on the returned item.
function upsertTest(
controller: vscode.TestController,
uri: vscode.Uri,
nsName: string,
varName: string,
range?: vscode.Range
): vscode.TestItem {
const ns = upsertNamespace(controller, uri, nsName);
const testId = nsName + '/' + varName;
let test = ns.children.get(testId);
if (!test) {
test = controller.createTestItem(testId, varName, uri);
}
if (range) {
test.range = range;
}
ns.children.add(test);
return test;
}
// Cider 0.26 and 0.27 have an issue where context can be an empty array.
// https://github.com/clojure-emacs/cider-nrepl/issues/728#issuecomment-996002988
export function assertionName(result: cider.TestResult): string {
if (string.isNonEmptyString(result.context)) {
return result.context;
}
return 'assertion';
}
function existingUriForNameSpace(
controller: vscode.TestController,
nsName: string
): vscode.Uri | undefined {
return controller.items.get(nsName)?.uri;
}
async function onTestResult(
controller: vscode.TestController,
session: NReplSession,
run: vscode.TestRun,
nsName: string,
varName: string,
assertions: cider.TestResult[]
) {
let uri = existingUriForNameSpace(controller, nsName);
if (!uri) {
uri = await namespace.getUriForNamespace(session, nsName);
}
if (!uri) {
console.warn('Test Runner: Unable to find file corresponding to namespace: ' + nsName);
}
const test = upsertTest(controller, uri, nsName, varName);
// The Clojure LSP gives a very accurate range for a test. In some cases, this
// function will be called before Clojure LSP has analysed the file, so we might
// have a test item with no range set. In this case, we can look at the line
// data in the assertions collection and produce a good approximation of the
// range. If the LSP subsequently scans the file, upsertTest will be called,
// which will set the range correctly.
if (!test.range || test.range.isEmpty) {
const lines = assertions
.filter(cider.hasLineNumber)
.map((a) => a.line)
.sort();
if (lines.length > 0) {
test.range = new vscode.Range(lines[0] - 1, 0, lines[lines.length - 1], 1000);
}
}
// Clear any children, which are assertions left over from previous runs.
test.children.replace([]);
const failures = assertions.filter((result) => {
return result.type != 'pass';
});
if (failures.length == 0) {
run.passed(test);
return;
}
failures.forEach((result) => {
const assertionId = test.id + '/' + result.index;
const assertion = controller.createTestItem(assertionId, assertionName(result), uri);
test.children.add(assertion);
if (cider.hasLineNumber(result)) {
assertion.range = new vscode.Range(result.line - 1, 0, result.line - 1, 1000);
}
switch (result.type) {
case 'error':
run.errored(assertion, new vscode.TestMessage(cider.shortMessage(result)));
break;
case 'fail':
default:
run.failed(assertion, new vscode.TestMessage(cider.shortMessage(result)));
break;
}
});
}
async function onTestResults(
controller: vscode.TestController,
session: NReplSession,
results: cider.TestResults[]
) {
const run = controller.createTestRun(new vscode.TestRunRequest(), 'Clojure', false);
for (const result of results) {
for (const namespace in result.results) {
const tests = result.results[namespace];
for (const test in tests) {
await onTestResult(controller, session, run, namespace, test, tests[test]);
}
}
}
run.end();
}
function useTestExplorer(): boolean | undefined {
return vscode.workspace.getConfiguration('calva').get('useTestExplorer');
}
async function reportTests(
controller: vscode.TestController,
session: NReplSession,
possibleResults: cider.TestResults[]
) {
// Results can sometimes not be actual test results, such as when a namespace is not found: https://github.com/BetterThanTomorrow/calva/issues/1516.
const results = possibleResults.filter((pr) => pr.results);
const diagnostics: { [key: string]: vscode.Diagnostic[] } = {};
diagnosticCollection.clear();
const recordDiagnostic = (result: cider.TestResult) => {
util.assertIsDefined(result.line, 'Expected cider test result to have a line!');
util.assertIsDefined(result.file, 'Expected cider test result to have a file!');
const msg = cider.diagnosticMessage(result);
const err = new vscode.Diagnostic(
new vscode.Range(result.line - 1, 0, result.line - 1, 1000),
msg,
vscode.DiagnosticSeverity.Error
);
if (!diagnostics[result.file]) {
diagnostics[result.file] = [];
}
diagnostics[result.file].push(err);
};
if (useTestExplorer()) {
void onTestResults(controller, session, results);
}
for (const result of results) {
for (const ns in result.results) {
const resultSet = result.results[ns];
for (const test in resultSet) {
for (const a of resultSet[test]) {
const messages = cider.detailedMessage(a);
if (a.type == 'error') {
const stackTrace = await session.testStacktrace(ns, test, a.index);
outputWindow.saveStacktrace(stackTrace.stacktrace);
outputWindow.appendLine(messages, (_, afterResultLocation) => {
outputWindow.markLastStacktraceRange(afterResultLocation);
});
if (output.getDestinationConfiguration().otherOutput !== 'repl-window') {
output.appendLineOtherOut(messages);
}
} else if (messages) {
output.appendLineOtherOut(messages);
}
if (a.type === 'fail') {
recordDiagnostic(a);
}
}
}
}
}
const summary = cider.totalSummary(results.map((r) => r.summary));
output.appendLineOtherOut(cider.summaryMessage(summary));
if (!useTestExplorer()) {
for (const fileName in diagnostics) {
void uriForFile(fileName).then((uri) => {
diagnosticCollection.set(uri, diagnostics[fileName]);
});
}
}
}
// FIXME: use cljs session where necessary
async function runAllTests(controller: vscode.TestController, document = {}) {
const session = getSession(util.getFileType(document));
output.appendLineOtherOut('Running all project tests…');
try {
await reportTests(controller, session, [await session.testAll()]);
} catch (e) {
output.appendLineOtherErr(e);
}
updateReplSessionType();
output.replWindowAppendPrompt();
}
function runAllTestsCommand(controller: vscode.TestController) {
if (!util.getConnectedState()) {
void vscode.window.showInformationMessage(
'You must connect to a REPL server to run this command.'
);
return;
}
runAllTests(controller).catch((msg) => {
void vscode.window.showWarningMessage(msg);
});
}
async function loadTestNS(ns: string, session: NReplSession) {
const testNS = !ns.endsWith('-test') ? ns + '-test' : ns;
const nsPath = await session.nsPath(testNS);
const testFilePath = nsPath.path;
if (testFilePath && testFilePath !== '') {
const filePath = vscode.Uri.parse(testFilePath).path;
const loadForms = `(load-file "${filePath}")`;
await session.eval(loadForms, testNS).value;
}
}
async function runNamespaceTestsImpl(
controller: vscode.TestController,
document: vscode.TextDocument,
nss: string[]
) {
if (!util.getConnectedState()) {
void vscode.window.showInformationMessage(
'You must connect to a REPL server to run this command.'
);
return;
}
if (nss.length === 0) {
void vscode.window.showInformationMessage('No namespace selected.');
return;
}
const session = getSession(util.getFileType(document));
output.appendLineOtherOut(
`Running tests for the following namespaces:\n${
nss.map((item) => ` ${item}`).join('\n') + '\n'
}`
);
const resultPromises = nss.map((ns) => {
return session.testNs(ns);
});
try {
await reportTests(controller, session, await Promise.all(resultPromises));
} catch (e) {
output.appendLineOtherErr(e);
}
outputWindow.setSession(session, nss[0]);
updateReplSessionType();
output.replWindowAppendPrompt();
}
async function runNamespaceTests(controller: vscode.TestController, document: vscode.TextDocument) {
const doc = util.tryToGetDocument(document);
if (outputWindow.isResultsDoc(doc)) {
return;
}
const session = getSession(util.getFileType(document));
const [currentDocNs, _] = namespace.getNamespace(
doc,
vscode.window.activeTextEditor?.selections[0]?.active
);
await loadTestNS(currentDocNs, session);
const namespacesToRunTestsFor = [
currentDocNs,
currentDocNs.endsWith('-test') ? currentDocNs.slice(0, -5) : currentDocNs + '-test',
];
void runNamespaceTestsImpl(controller, document, namespacesToRunTestsFor);
}
function getTestUnderCursor() {
const editor = util.tryToGetActiveTextEditor();
if (editor) {
return getText.currentTopLevelDefined(editor?.document, editor?.selections[0].active)[1];
}
}
async function runTestUnderCursor(controller: vscode.TestController) {
const doc = util.tryToGetDocument({});
const session = getSession(util.getFileType(doc));
const [ns, _] = namespace.getNamespace(
doc,
vscode.window.activeTextEditor?.selections[0]?.active
);
const test = getTestUnderCursor();
if (test) {
output.appendLineOtherOut(`Running test: ${test}…`);
try {
await reportTests(controller, session, [await session.test(ns, test)]);
} catch (e) {
output.appendLineOtherErr(e);
}
} else {
output.appendLineOtherOut('No test found at cursor');
}
output.replWindowAppendPrompt();
}
function runTestUnderCursorCommand(controller: vscode.TestController) {
if (!util.getConnectedState()) {
void vscode.window.showInformationMessage(
'You must connect to a REPL server to run this command.'
);
return;
}
runTestUnderCursor(controller).catch((msg) => {
void vscode.window.showWarningMessage(msg);
});
}
function runNamespaceTestsCommand(controller: vscode.TestController) {
if (!util.getConnectedState()) {
void vscode.window.showInformationMessage(
'You must connect to a REPL server to run this command.'
);
return;
}
runNamespaceTests(controller, util.getActiveTextEditor().document).catch((msg) => {
void vscode.window.showWarningMessage(msg);
});
}
async function rerunTests(controller: vscode.TestController, document = {}) {
const session = getSession(util.getFileType(document));
output.appendLineOtherOut('Running previously failed tests…');
try {
await reportTests(controller, session, [await session.retest()]);
} catch (e) {
output.appendLineOtherErr(e);
}
output.replWindowAppendPrompt();
}
function rerunTestsCommand(controller: vscode.TestController) {
if (!util.getConnectedState()) {
void vscode.window.showInformationMessage(
'You must connect to a REPL server to run this command.'
);
return;
}
rerunTests(controller).catch((msg) => {
void vscode.window.showWarningMessage(msg);
});
}
function initialize(controller: vscode.TestController): void {
const profile = controller.createRunProfile(
'Clojure Run Profile',
vscode.TestRunProfileKind.Run,
(request: vscode.TestRunRequest, token: vscode.CancellationToken) => {
if (!util.getConnectedState()) {
void vscode.window.showInformationMessage(
'You must connect to a REPL server to run tests.'
);
return;
}
if (request.exclude && request.exclude.length > 0) {
void vscode.window.showWarningMessage(
'Excluding tests from a test run is not currently supported - running all tests.'
);
}
if (!request.include) {
runAllTests(controller).catch((msg) => {
void vscode.window.showWarningMessage(msg);
});
return;
}
const runItems = request.include.map((req) => {
// Split into [namespace, var]
return req.id.split('/', 2);
});
// TODO.marc: We don't support running specific vars right now.
// If the user selects to run subset of tests, we run the whole namespace.
// The next steps here would be to turn request.exclude and requst.include
// into a Cider var-query that can be passed to test-var query directly.
const namespaces = util.distinct(runItems.map((ri) => ri[0]));
const doc = util.getActiveTextEditor().document;
runNamespaceTestsImpl(controller, doc, namespaces).catch((msg) => {
void vscode.window.showWarningMessage(msg);
});
},
true
);
controller.resolveHandler = (item: vscode.TestItem | undefined) => {
// currently unused
};
}
function createRange(test: lsp.TestTreeNode): vscode.Range {
return new vscode.Range(
test.range.start.line,
test.range.start.character,
test.range.end.line,
test.range.end.character
);
}
function onTestTree(controller: vscode.TestController, testTree: lsp.TestTreeParams) {
if (!useTestExplorer()) {
return;
}
try {
const uri = vscode.Uri.parse(testTree.uri);
const ns = upsertNamespace(controller, uri, testTree.tree.name, createRange(testTree.tree));
ns.canResolveChildren = true;
testTree.tree.children.forEach((c) => {
upsertTest(controller, uri, testTree.tree.name, c.name, createRange(c));
});
} catch (e) {
void vscode.window.showErrorMessage('Error in test tree parsing', e);
}
}
export default {
initialize,
runNamespaceTests,
runNamespaceTestsCommand,
runAllTestsCommand,
rerunTestsCommand,
runTestUnderCursorCommand,
onTestTree,
};