Skip to content

Commit

Permalink
Factor out getJunitResults().
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Sep 6, 2019
1 parent 2937a1f commit dfd23d9
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions src/client/testing/common/xUnitParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,44 +65,17 @@ export class XUnitParser implements IXUnitParser {
throw new Error('Unknown Test Pass Calculation');
}
}

const data = await this.fs.readFile(outputXmlFile);
// Un-comment this line to capture the results file for later use in tests:
//await fs.writeFile('/tmp/results.xml', data);

const parserResult = await parseXML(data);
let junitResults: TestSuiteResult;
const fullResults = parserResult as { testsuites: { testsuite: TestSuiteResult[] }};
if (fullResults.testsuites) {
const junitSuites = fullResults.testsuites.testsuite;
if (!Array.isArray(junitSuites)) {
throw Error('bad JUnit XML data');
}
if (junitSuites.length === 0) {
return;
}
if (junitSuites.length > 1) {
throw Error('got multiple XML results');
}
junitResults = junitSuites[0];
} else {
junitResults = (parserResult as { testsuite: TestSuiteResult }).testsuite;
const junitResults = getJunitResults(parserResult);
if (junitResults) {
updateTests(tests, junitResults);
}
updateTests(tests, junitResults);
}
}

// const parserResult = await parseXML(data);
// const junitResults = getJunitResults(parserResult);
// updateTests(tests, junitResults);
// }
//}
//
//function getTestResults(data: any): TestSuiteResult {
// const full = data as { testsuite: TestSuiteResult };
// return full.testsuite;
//}

// tslint:disable-next-line:no-any
async function parseXML(data: string): Promise<any> {
// tslint:disable-next-line:no-require-imports
Expand All @@ -119,6 +92,28 @@ async function parseXML(data: string): Promise<any> {
});
}

// Return the actual test results from the given data.
// tslint:disable-next-line:no-any
function getJunitResults(parserResult: any): TestSuiteResult | undefined {
// This is the newer JUnit XML format (e.g. pytest 5.1 and later).
const fullResults = parserResult as { testsuites: { testsuite: TestSuiteResult[] }};
if (!fullResults.testsuites) {
return (parserResult as { testsuite: TestSuiteResult }).testsuite;
}

const junitSuites = fullResults.testsuites.testsuite;
if (!Array.isArray(junitSuites)) {
throw Error('bad JUnit XML data');
}
if (junitSuites.length === 0) {
return;
}
if (junitSuites.length > 1) {
throw Error('got multiple XML results');
}
return junitSuites[0];
}

// Set the number of passing tests given the total number.
function setPassing(
summary: TestSummary,
Expand Down

0 comments on commit dfd23d9

Please sign in to comment.