export async function updateResultFromJunitXml()

in src/client/testing/testController/common/resultsHelper.ts [77:174]


export async function updateResultFromJunitXml(
    outputXmlFile: string,
    testNode: TestItem,
    runInstance: TestRun,
    idToRawData: Map<string, TestData>,
): Promise<void> {
    const data = await fsapi.readFile(outputXmlFile);
    const parserResult = await parseXML(data.toString('utf8'));
    const junitSuite = getJunitResults(parserResult);
    const testCaseNodes = getTestCaseNodes(testNode);

    if (junitSuite && junitSuite.testcase.length > 0 && testCaseNodes.length > 0) {
        let failures = 0;
        let skipped = 0;
        let errors = 0;
        let passed = 0;

        testCaseNodes.forEach((node) => {
            const rawTestCaseNode = idToRawData.get(node.id);
            if (!rawTestCaseNode) {
                return;
            }

            const result = junitSuite.testcase.find((t) => {
                const idResult = getRunIdFromRawData(`${t.$.classname}::${t.$.name}`);
                const idNode = rawTestCaseNode.runId;
                return idResult === idNode || idNode.endsWith(idResult);
            });
            if (result) {
                if (result.error) {
                    errors += 1;
                    const error = result.error[0];
                    const text = `${rawTestCaseNode.rawId} Failed with Error: [${error.$.type}]${error.$.message}\r\n${error._}\r\n\r\n`;
                    const message = new TestMessage(text);

                    if (node.uri && node.range) {
                        message.location = new Location(node.uri, node.range);
                    }

                    runInstance.errored(node, message);
                    runInstance.appendOutput(fixLogLines(text));
                } else if (result.failure) {
                    failures += 1;
                    const failure = result.failure[0];
                    const text = `${rawTestCaseNode.rawId} Failed: [${failure.$.type}]${failure.$.message}\r\n${failure._}\r\n`;
                    const message = new TestMessage(text);

                    if (node.uri && node.range) {
                        message.location = new Location(node.uri, node.range);
                    }

                    runInstance.failed(node, message);
                    runInstance.appendOutput(fixLogLines(text));
                } else if (result.skipped) {
                    const skip = result.skipped[0];
                    let text = '';
                    if (skip.$.type === 'pytest.xfail') {
                        passed += 1;
                        // pytest.xfail ==> expected failure via @unittest.expectedFailure
                        text = `${rawTestCaseNode.rawId} Passed: [${skip.$.type}]${skip.$.message}\r\n`;
                        runInstance.passed(node);
                    } else {
                        skipped += 1;
                        text = `${rawTestCaseNode.rawId} Skipped: [${skip.$.type}]${skip.$.message}\r\n`;
                        runInstance.skipped(node);
                    }
                    runInstance.appendOutput(fixLogLines(text));
                } else {
                    passed += 1;
                    const text = `${rawTestCaseNode.rawId} Passed\r\n`;
                    runInstance.passed(node);
                    runInstance.appendOutput(fixLogLines(text));
                }
            } else {
                const text = `Test result not found for: ${rawTestCaseNode.rawId}\r\n`;
                runInstance.appendOutput(fixLogLines(text));
                const message = new TestMessage(text);

                if (node.uri && node.range) {
                    message.location = new Location(node.uri, node.range);
                }
                runInstance.errored(node, message);
            }
        });

        runInstance.appendOutput(`Total number of tests expected to run: ${testCaseNodes.length}\r\n`);
        runInstance.appendOutput(`Total number of tests run: ${passed + failures + errors + skipped}\r\n`);
        runInstance.appendOutput(`Total number of tests passed: ${passed}\r\n`);
        runInstance.appendOutput(`Total number of tests failed: ${failures}\r\n`);
        runInstance.appendOutput(`Total number of tests failed with errors: ${errors}\r\n`);
        runInstance.appendOutput(`Total number of tests skipped: ${skipped}\r\n`);
        runInstance.appendOutput(
            `Total number of tests with no result data: ${
                testCaseNodes.length - passed - failures - errors - skipped
            }\r\n`,
        );
    }
}