forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxUnitParser.ts
More file actions
139 lines (124 loc) · 5.68 KB
/
xUnitParser.ts
File metadata and controls
139 lines (124 loc) · 5.68 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
import * as fs from 'fs';
import * as xml2js from 'xml2js';
import {TestFile, TestsToRun, TestSuite, TestFunction, FlattenedTestFunction, Tests, TestStatus, FlattenedTestSuite} from './contracts';
export enum PassCalculationFormulae {
pytest,
nosetests
}
interface TestSuiteResult {
$: {
errors: string;
failures: string;
name: string;
skips: string;
skip: string;
tests: string;
time: string;
};
testcase: TestCaseResult[];
}
interface TestCaseResult {
$: {
classname: string;
file: string;
line: string;
name: string;
time: string;
};
failure: {
_: string;
$: { message: string, type: string }
}[];
error: {
_: string;
$: { message: string, type: string }
}[];
skipped: {
_: string;
$: { message: string, type: string }
}[];
}
function getSafeInt(value: string, defaultValue: any = 0): number {
const num = parseInt(value);
if (isNaN(num)) { return defaultValue; }
return num;
}
export function updateResultsFromXmlLogFile(tests: Tests, outputXmlFile: string, passCalculationFormulae: PassCalculationFormulae): Promise<any> {
return new Promise<any>((resolve, reject) => {
fs.readFile(outputXmlFile, 'utf8', (err, data) => {
if (err) {
return reject(err);
}
xml2js.parseString(data, (err, result) => {
if (err) {
return reject(err);
}
let testSuiteResult: TestSuiteResult = result.testsuite;
tests.summary.errors = getSafeInt(testSuiteResult.$.errors);
tests.summary.failures = getSafeInt(testSuiteResult.$.failures);
tests.summary.skipped = getSafeInt(testSuiteResult.$.skips ? testSuiteResult.$.skips : testSuiteResult.$.skip);
let testCount = getSafeInt(testSuiteResult.$.tests);
switch (passCalculationFormulae) {
case PassCalculationFormulae.pytest: {
tests.summary.passed = testCount - tests.summary.failures - tests.summary.skipped - tests.summary.errors;
break;
}
case PassCalculationFormulae.nosetests: {
tests.summary.passed = testCount - tests.summary.failures - tests.summary.skipped - tests.summary.errors;
break;
}
default: {
throw new Error("Unknown Test Pass Calculation");
}
}
if (!Array.isArray(testSuiteResult.testcase)) {
return resolve();
}
testSuiteResult.testcase.forEach((testcase: TestCaseResult) => {
const xmlClassName = testcase.$.classname.replace(/\(\)/g, '').replace(/\.\./g, '.').replace(/\.\./g, '.').replace(/\.+$/, '');
let result = tests.testFunctions.find(fn => fn.xmlClassName === xmlClassName && fn.testFunction.name === testcase.$.name);
if (!result) {
// Possible we're dealing with nosetests, where the file name isn't returned to us
// When dealing with nose tests
// It is possible to have a test file named x in two separate test sub directories and have same functions/classes
// And unforutnately xunit log doesn't ouput the filename
// result = tests.testFunctions.find(fn => fn.testFunction.name === testcase.$.name &&
// fn.parentTestSuite && fn.parentTestSuite.name === testcase.$.classname);
// Look for failed file test
let fileTest = testcase.$.file && tests.testFiles.find(file => file.nameToRun === testcase.$.file);
if (fileTest && testcase.error) {
fileTest.status = TestStatus.Error;
fileTest.passed = false;
fileTest.message = testcase.error[0].$.message;
fileTest.traceback = testcase.error[0]._;
}
return;
}
result.testFunction.line = getSafeInt(testcase.$.line, null);
result.testFunction.time = parseFloat(testcase.$.time);
result.testFunction.passed = true;
result.testFunction.status = TestStatus.Pass;
if (testcase.failure) {
result.testFunction.status = TestStatus.Fail;
result.testFunction.passed = false;
result.testFunction.message = testcase.failure[0].$.message;
result.testFunction.traceback = testcase.failure[0]._;
}
if (testcase.error) {
result.testFunction.status = TestStatus.Error;
result.testFunction.passed = false;
result.testFunction.message = testcase.error[0].$.message;
result.testFunction.traceback = testcase.error[0]._;
}
if (testcase.skipped) {
result.testFunction.status = TestStatus.Skipped;
result.testFunction.passed = null;
result.testFunction.message = testcase.skipped[0].$.message;
result.testFunction.traceback = '';
}
});
resolve();
});
});
});
}