-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathTestXUnitParser.ts
103 lines (90 loc) · 3.42 KB
/
TestXUnitParser.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as xml2js from "xml2js";
import { TestRunnerTestRunState } from "./TestRunner";
export interface TestResults {
tests: number;
failures: number;
errors: number;
}
interface XUnitFailure {
$: { message?: string };
}
interface XUnitTestCase {
$: { classname: string; name: string; time: number };
failure?: XUnitFailure[];
}
interface XUnitTestSuite {
$: { name: string; errors: string; failures: string; tests: string; time: string };
testcase: XUnitTestCase[];
}
interface XUnitTestSuites {
testsuite: XUnitTestSuite[];
}
interface XUnit {
testsuites: XUnitTestSuites;
}
export class TestXUnitParser {
constructor(private hasMultiLineParallelTestOutput: boolean) {}
async parse(
buffer: string,
runState: TestRunnerTestRunState
): Promise<TestResults | undefined> {
const xml = await xml2js.parseStringPromise(buffer);
try {
return await this.parseXUnit(xml, runState);
} catch (error) {
// ignore error
console.log(error);
return undefined;
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async parseXUnit(xUnit: XUnit, runState: TestRunnerTestRunState): Promise<TestResults> {
let tests = 0;
let failures = 0;
let errors = 0;
xUnit.testsuites.testsuite.forEach(testsuite => {
const suiteFailures = parseInt(testsuite.$.failures);
failures += suiteFailures;
tests = tests + parseInt(testsuite.$.tests);
errors += parseInt(testsuite.$.errors);
let className: string | undefined;
testsuite.testcase.forEach(testcase => {
className = testcase.$.classname;
const id = `${className}/${testcase.$.name}`;
const index = runState.getTestItemIndex(id);
// From 5.7 to 5.10 running with the --parallel option dumps the test results out
// to the console with no newlines, so it isn't possible to distinguish where errors
// begin and end. Consequently we can't record them, and so we manually mark them
// as passed or failed here with a manufactured issue.
if (!!testcase.failure && !this.hasMultiLineParallelTestOutput) {
runState.recordIssue(
index,
testcase.failure.shift()?.$.message ?? "Test Failed"
);
}
runState.completed(index, { duration: testcase.$.time });
});
if (className !== undefined) {
if (className && suiteFailures === 0) {
runState.passedSuite(className);
} else if (className) {
runState.failedSuite(className);
}
}
});
return { tests: tests, failures: failures, errors: errors };
}
}