forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresults.ts
More file actions
560 lines (519 loc) · 17.9 KB
/
results.ts
File metadata and controls
560 lines (519 loc) · 17.9 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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as path from 'path';
import { Uri } from 'vscode';
import { getDedentedLines, getIndent } from '../../client/common/utils/text';
import {
FlattenedTestFunction,
FlattenedTestSuite,
SubtestParent,
TestFile,
TestFolder,
TestFunction,
TestingType,
TestResult,
Tests,
TestStatus,
TestSuite,
TestSummary,
} from '../../client/testing/common/types';
import { TestProvider } from '../../client/testing/types';
import { fixPath, RESOURCE } from './helper';
type SuperTest = TestFunction & {
subtests: TestFunction[];
};
export type TestItem = TestFolder | TestFile | TestSuite | SuperTest | TestFunction;
export type TestNode = TestItem & {
testType: TestingType;
};
// Return an initialized test results.
export function createEmptyResults(): Tests {
return {
summary: {
passed: 0,
failures: 0,
errors: 0,
skipped: 0,
},
testFiles: [],
testFunctions: [],
testSuites: [],
testFolders: [],
rootTestFolders: [],
};
}
// Increment the appropriate summary property.
export function updateSummary(summary: TestSummary, status: TestStatus) {
switch (status) {
case TestStatus.Pass:
summary.passed += 1;
break;
case TestStatus.Fail:
summary.failures += 1;
break;
case TestStatus.Error:
summary.errors += 1;
break;
case TestStatus.Skipped:
summary.skipped += 1;
break;
default:
// Do not update the results.
}
}
// Return the file found walking up the parents, if any.
//
// There should only be one parent file.
export function findParentFile(parents: TestNode[]): TestFile | undefined {
// Iterate in reverse order.
for (let i = parents.length; i > 0; i -= 1) {
const parent = parents[i - 1];
if (parent.testType === TestingType.file) {
return parent as TestFile;
}
}
return;
}
// Return the first suite found walking up the parents, if any.
export function findParentSuite(parents: TestNode[]): TestSuite | undefined {
// Iterate in reverse order.
for (let i = parents.length; i > 0; i -= 1) {
const parent = parents[i - 1];
if (parent.testType === TestingType.suite) {
return parent as TestSuite;
}
}
return;
}
// Return the "flattened" test suite node.
export function flattenSuite(node: TestSuite, parents: TestNode[]): FlattenedTestSuite {
const found = findParentFile(parents);
if (!found) {
throw Error('parent file not found');
}
const parentFile: TestFile = found;
return {
testSuite: node,
parentTestFile: parentFile,
xmlClassName: node.xmlName,
};
}
// Return the "flattened" test function node.
export function flattenFunction(node: TestFunction, parents: TestNode[]): FlattenedTestFunction {
const found = findParentFile(parents);
if (!found) {
throw Error('parent file not found');
}
const parentFile: TestFile = found;
const parentSuite = findParentSuite(parents);
return {
testFunction: node,
parentTestFile: parentFile,
parentTestSuite: parentSuite,
xmlClassName: parentSuite ? parentSuite.xmlName : '',
};
}
// operations on raw test nodes
export namespace nodes {
// Set the result-oriented properties back to their "unset" values.
export function resetResult(node: TestNode) {
node.time = 0;
node.status = TestStatus.Unknown;
}
//********************************
// builders for empty low-level test results
export function createFolderResults(dirname: string, nameToRun?: string, resource: Uri = RESOURCE): TestNode {
dirname = fixPath(dirname);
return {
resource: resource,
name: dirname,
nameToRun: nameToRun || dirname,
folders: [],
testFiles: [],
testType: TestingType.folder,
// result
time: 0,
status: TestStatus.Unknown,
};
}
export function createFileResults(
filename: string,
nameToRun?: string,
xmlName?: string,
resource: Uri = RESOURCE,
): TestNode {
filename = fixPath(filename);
if (!xmlName) {
xmlName = filename
.replace(/\.[^.]+$/, '')
.replace(/[\\\/]/, '.')
.replace(/^[.\\\/]*/, '');
}
return {
resource: resource,
fullPath: filename,
name: path.basename(filename),
nameToRun: nameToRun || filename,
xmlName: xmlName!,
suites: [],
functions: [],
testType: TestingType.file,
// result
time: 0,
status: TestStatus.Unknown,
};
}
export function createSuiteResults(
name: string,
nameToRun?: string,
xmlName?: string,
provider: TestProvider = 'pytest',
isInstance: boolean = false,
resource: Uri = RESOURCE,
): TestNode {
return {
resource: resource,
name: name,
nameToRun: nameToRun || '', // must be set for parent
xmlName: xmlName || '', // must be set for parent
isUnitTest: provider === 'unittest',
isInstance: isInstance,
suites: [],
functions: [],
testType: TestingType.suite,
// result
time: 0,
status: TestStatus.Unknown,
};
}
export function createTestResults(
name: string,
nameToRun?: string,
subtestParent?: SubtestParent,
resource: Uri = RESOURCE,
): TestNode {
return {
resource: resource,
name: name,
nameToRun: nameToRun || name,
subtestParent: subtestParent,
testType: TestingType.function,
// result
time: 0,
status: TestStatus.Unknown,
};
}
//********************************
// adding children to low-level nodes
export function addDiscoveredSubFolder(
parent: TestFolder,
basename: string,
nameToRun?: string,
resource?: Uri,
): TestNode {
const dirname = path.join(parent.name, fixPath(basename));
const subFolder = createFolderResults(dirname, nameToRun, resource || parent.resource || RESOURCE);
parent.folders.push(subFolder as TestFolder);
return subFolder;
}
export function addDiscoveredFile(
parent: TestFolder,
basename: string,
nameToRun?: string,
xmlName?: string,
resource?: Uri,
): TestNode {
const filename = path.join(parent.name, fixPath(basename));
const file = createFileResults(filename, nameToRun, xmlName, resource || parent.resource || RESOURCE);
parent.testFiles.push(file as TestFile);
return file;
}
export function addDiscoveredSuite(
parent: TestFile | TestSuite,
name: string,
nameToRun?: string,
xmlName?: string,
provider: TestProvider = 'pytest',
isInstance?: boolean,
resource?: Uri,
): TestNode {
if (!nameToRun) {
const sep = provider === 'pytest' ? '::' : '.';
nameToRun = `${parent.nameToRun}${sep}${name}`;
}
const suite = createSuiteResults(
name,
nameToRun!,
xmlName || `${parent.xmlName}.${name}`,
provider,
isInstance,
resource || parent.resource || RESOURCE,
);
parent.suites.push(suite as TestSuite);
return suite;
}
export function addDiscoveredTest(
parent: TestFile | TestSuite,
name: string,
nameToRun?: string,
provider: TestProvider = 'pytest',
resource?: Uri,
): TestNode {
if (!nameToRun) {
const sep = provider === 'pytest' ? '::' : '.';
nameToRun = `${parent.nameToRun}${sep}${name}`;
}
const test = createTestResults(name, nameToRun, undefined, resource || parent.resource || RESOURCE);
parent.functions.push(test as TestFunction);
return test;
}
export function addDiscoveredSubtest(
parent: SuperTest,
name: string,
nameToRun?: string,
provider: TestProvider = 'pytest',
resource?: Uri,
): TestNode {
const subtest = createTestResults(
name,
nameToRun!,
{
name: parent.name,
nameToRun: parent.nameToRun,
asSuite: createSuiteResults(
parent.name,
parent.nameToRun,
'',
provider,
false,
parent.resource,
) as TestSuite,
time: 0,
},
resource || parent.resource || RESOURCE,
);
(subtest as TestFunction).subtestParent!.asSuite.functions.push(subtest);
parent.subtests.push(subtest as TestFunction);
return subtest;
}
}
namespace declarative {
type TestParent = TestNode & {
indent: string;
};
type ParsedTestNode = {
indent: string;
name: string;
testType: TestingType;
result: TestResult;
};
// Return a test tree built from concise declarative text.
export function parseResults(text: string, tests: Tests, provider: TestProvider, resource: Uri) {
// Build the tree (and populate the return value at the same time).
const parents: TestParent[] = [];
let prev: TestParent;
for (const line of getDedentedLines(text)) {
if (line.trim() === '') {
continue;
}
const parsed = parseTestLine(line);
let node: TestNode;
if (isRootNode(parsed)) {
parents.length = 0; // Clear the array.
node = nodes.createFolderResults(parsed.name, undefined, resource);
tests.rootTestFolders.push(node as TestFolder);
tests.testFolders.push(node as TestFolder);
} else {
const parent = setMatchingParent(parents, prev!, parsed.indent);
node = buildDiscoveredChildNode(parent, parsed.name, parsed.testType, provider, resource);
switch (parsed.testType) {
case TestingType.folder:
tests.testFolders.push(node as TestFolder);
break;
case TestingType.file:
tests.testFiles.push(node as TestFile);
break;
case TestingType.suite:
tests.testSuites.push(flattenSuite(node as TestSuite, parents));
break;
case TestingType.function:
// This does not deal with subtests?
tests.testFunctions.push(flattenFunction(node as TestFunction, parents));
break;
default:
}
}
// Set the result.
node.status = parsed.result.status;
node.time = parsed.result.time;
updateSummary(tests.summary, node.status!);
// Prepare for the next line.
prev = node as TestParent;
prev.indent = parsed.indent;
}
}
// Determine the kind, indent, and result info based on the line.
function parseTestLine(line: string): ParsedTestNode {
if (line.includes('\\')) {
throw Error('expected / as path separator (even on Windows)');
}
const indent = getIndent(line);
line = line.trim();
const parts = line.split(' ');
let name = parts.shift();
if (!name) {
throw Error('missing name');
}
// Determine the type from the name.
let testType: TestingType;
if (name.endsWith('/')) {
// folder
testType = TestingType.folder;
while (name.endsWith('/')) {
name = name.slice(0, -1);
}
} else if (name.includes('.')) {
// file
if (name.includes('/')) {
throw Error('filename must not include directories');
}
testType = TestingType.file;
} else if (name.startsWith('<')) {
// suite
if (!name.endsWith('>')) {
throw Error('suite missing closing bracket');
}
testType = TestingType.suite;
name = name.slice(1, -1);
} else {
// test
testType = TestingType.function;
}
// Parse the results.
const result: TestResult = {
time: 0,
};
if (parts.length !== 0 && testType !== TestingType.function) {
throw Error('non-test nodes do not have results');
}
switch (parts.length) {
case 0:
break;
case 1:
if (isNaN(parts[0] as any)) {
throw Error(`expected a time (float), got ${parts[0]}`);
}
result.time = parseFloat(parts[0]);
break;
case 2:
switch (parts[0]) {
case 'P':
result.status = TestStatus.Pass;
break;
case 'F':
result.status = TestStatus.Fail;
break;
case 'E':
result.status = TestStatus.Error;
break;
case 'S':
result.status = TestStatus.Skipped;
break;
default:
throw Error('expected a status and then a time');
}
if (isNaN(parts[1] as any)) {
throw Error(`expected a time (float), got ${parts[1]}`);
}
result.time = parseFloat(parts[1]);
break;
default:
throw Error('too many items on line');
}
return {
indent: indent,
name: name,
testType: testType,
result: result,
};
}
function isRootNode(parsed: ParsedTestNode): boolean {
if (parsed.indent === '') {
if (parsed.testType !== TestingType.folder) {
throw Error('a top-level node must be a folder');
}
return true;
}
return false;
}
function setMatchingParent(parents: TestParent[], prev: TestParent, parsedIndent: string): TestParent {
let current = parents.length > 0 ? parents[parents.length - 1] : prev;
if (parsedIndent.length > current.indent.length) {
parents.push(prev);
current = prev;
} else {
while (parsedIndent !== current.indent) {
if (parsedIndent.length > current.indent.length) {
throw Error('mis-aligned indentation');
}
parents.pop();
if (parents.length === 0) {
throw Error('mis-aligned indentation');
}
current = parents[parents.length - 1];
}
}
return current;
}
function buildDiscoveredChildNode(
parent: TestParent,
name: string,
testType: TestingType,
provider: TestProvider,
resource?: Uri,
): TestNode {
switch (testType) {
case TestingType.folder:
if (parent.testType !== TestingType.folder) {
throw Error('parent must be a folder');
}
return nodes.addDiscoveredSubFolder(parent as TestFolder, name, undefined, resource);
case TestingType.file:
if (parent.testType !== TestingType.folder) {
throw Error('parent must be a folder');
}
return nodes.addDiscoveredFile(parent as TestFolder, name, undefined, undefined, resource);
case TestingType.suite:
let suiteParent: TestFile | TestSuite;
if (parent.testType === TestingType.file) {
suiteParent = parent as TestFile;
} else if (parent.testType === TestingType.suite) {
suiteParent = parent as TestSuite;
} else {
throw Error('parent must be a file or suite');
}
return nodes.addDiscoveredSuite(suiteParent, name, undefined, undefined, provider, undefined, resource);
case TestingType.function:
let funcParent: TestFile | TestSuite;
if (parent.testType === TestingType.file) {
funcParent = parent as TestFile;
} else if (parent.testType === TestingType.suite) {
funcParent = parent as TestSuite;
} else if (parent.testType === TestingType.function) {
throw Error('not finished: use addDiscoveredSubTest()');
} else {
throw Error('parent must be a file, suite, or function');
}
return nodes.addDiscoveredTest(funcParent, name, undefined, provider, resource);
default:
throw Error('unsupported');
}
}
}
// Return a test tree built from concise declarative text.
export function createDeclaratively(text: string, provider: TestProvider = 'pytest', resource: Uri = RESOURCE): Tests {
const tests = createEmptyResults();
declarative.parseResults(text, tests, provider, resource);
return tests;
}