mirror of
https://github.com/dorny/test-reporter.git
synced 2025-12-18 15:07:10 +01:00
fix stdout issue
This commit is contained in:
parent
46691ebeaf
commit
2db037664f
2 changed files with 89 additions and 83 deletions
|
|
@ -1,10 +1,10 @@
|
||||||
import {parseStringPromise} from 'xml2js'
|
import { parseStringPromise } from 'xml2js';
|
||||||
|
|
||||||
import {ErrorInfo, Outcome, TrxReport, UnitTest, UnitTestResult} from './dotnet-trx-types'
|
import { ErrorInfo, Outcome, TrxReport, UnitTest, UnitTestResult } from './dotnet-trx-types';
|
||||||
import {ParseOptions, TestParser} from '../../test-parser'
|
import { ParseOptions, TestParser } from '../../test-parser';
|
||||||
|
|
||||||
import {getBasePath, normalizeFilePath} from '../../utils/path-utils'
|
import { getBasePath, normalizeFilePath } from '../../utils/path-utils';
|
||||||
import {parseIsoDate, parseNetDuration} from '../../utils/parse-utils'
|
import { parseIsoDate, parseNetDuration } from '../../utils/parse-utils';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
TestExecutionResult,
|
TestExecutionResult,
|
||||||
|
|
@ -12,12 +12,12 @@ import {
|
||||||
TestSuiteResult,
|
TestSuiteResult,
|
||||||
TestGroupResult,
|
TestGroupResult,
|
||||||
TestCaseResult,
|
TestCaseResult,
|
||||||
TestCaseError
|
TestCaseError,
|
||||||
} from '../../test-results'
|
} from '../../test-results';
|
||||||
|
|
||||||
class TestClass {
|
class TestClass {
|
||||||
constructor(readonly name: string) {}
|
constructor(readonly name: string) {}
|
||||||
readonly tests: Test[] = []
|
readonly tests: Test[] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
|
|
@ -25,162 +25,168 @@ class Test {
|
||||||
readonly name: string,
|
readonly name: string,
|
||||||
readonly outcome: Outcome,
|
readonly outcome: Outcome,
|
||||||
readonly duration: number,
|
readonly duration: number,
|
||||||
readonly error?: ErrorInfo
|
readonly error?: ErrorInfo,
|
||||||
|
readonly stdOut?: string
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
get result(): TestExecutionResult | undefined {
|
get result(): TestExecutionResult | undefined {
|
||||||
switch (this.outcome) {
|
switch (this.outcome) {
|
||||||
case 'Passed':
|
case 'Passed':
|
||||||
return 'success'
|
return 'success';
|
||||||
case 'NotExecuted':
|
case 'NotExecuted':
|
||||||
return 'skipped'
|
return 'skipped';
|
||||||
case 'Failed':
|
case 'Failed':
|
||||||
return 'failed'
|
return 'failed';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DotnetTrxParser implements TestParser {
|
export class DotnetTrxParser implements TestParser {
|
||||||
assumedWorkDir: string | undefined
|
assumedWorkDir: string | undefined;
|
||||||
|
|
||||||
constructor(readonly options: ParseOptions) {}
|
constructor(readonly options: ParseOptions) {}
|
||||||
|
|
||||||
async parse(path: string, content: string): Promise<TestRunResult> {
|
async parse(path: string, content: string): Promise<TestRunResult> {
|
||||||
const trx = await this.getTrxReport(path, content)
|
const trx = await this.getTrxReport(path, content);
|
||||||
const tc = this.getTestClasses(trx)
|
const tc = this.getTestClasses(trx);
|
||||||
const tr = this.getTestRunResult(path, trx, tc)
|
const tr = this.getTestRunResult(path, trx, tc);
|
||||||
tr.sort(true)
|
tr.sort(true);
|
||||||
return tr
|
return tr;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getTrxReport(path: string, content: string): Promise<TrxReport> {
|
private async getTrxReport(path: string, content: string): Promise<TrxReport> {
|
||||||
try {
|
try {
|
||||||
return (await parseStringPromise(content)) as TrxReport
|
return (await parseStringPromise(content)) as TrxReport;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(`Invalid XML at ${path}\n\n${e}`)
|
throw new Error(`Invalid XML at ${path}\n\n${e}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getTestClasses(trx: TrxReport): TestClass[] {
|
private getTestClasses(trx: TrxReport): TestClass[] {
|
||||||
if (trx.TestRun.TestDefinitions === undefined || trx.TestRun.Results === undefined) {
|
if (trx.TestRun.TestDefinitions === undefined || trx.TestRun.Results === undefined) {
|
||||||
return []
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const unitTests: {[id: string]: UnitTest} = {}
|
const unitTests: { [id: string]: UnitTest } = {};
|
||||||
for (const td of trx.TestRun.TestDefinitions) {
|
for (const td of trx.TestRun.TestDefinitions) {
|
||||||
for (const ut of td.UnitTest) {
|
for (const ut of td.UnitTest) {
|
||||||
unitTests[ut.$.id] = ut
|
unitTests[ut.$.id] = ut;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const unitTestsResults = trx.TestRun.Results.flatMap(r => r.UnitTestResult).flatMap(result => ({
|
const unitTestsResults = trx.TestRun.Results.flatMap((r) =>
|
||||||
|
r.UnitTestResult.map((result) => ({
|
||||||
result,
|
result,
|
||||||
test: unitTests[result.$.testId]
|
test: unitTests[result.$.testId],
|
||||||
}))
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
const testClasses: {[name: string]: TestClass} = {}
|
const testClasses: { [name: string]: TestClass } = {};
|
||||||
for (const r of unitTestsResults) {
|
for (const r of unitTestsResults) {
|
||||||
const className = r.test.TestMethod[0].$.className
|
const className = r.test.TestMethod[0].$.className;
|
||||||
let tc = testClasses[className]
|
let tc = testClasses[className];
|
||||||
if (tc === undefined) {
|
if (tc === undefined) {
|
||||||
tc = new TestClass(className)
|
tc = new TestClass(className);
|
||||||
testClasses[tc.name] = tc
|
testClasses[tc.name] = tc;
|
||||||
}
|
}
|
||||||
const error = this.getErrorInfo(r.result)
|
|
||||||
const durationAttr = r.result.$.duration
|
|
||||||
const duration = durationAttr ? parseNetDuration(durationAttr) : 0
|
|
||||||
|
|
||||||
const resultTestName = r.result.$.testName
|
const error = this.getErrorInfo(r.result);
|
||||||
|
const durationAttr = r.result.$.duration;
|
||||||
|
const duration = durationAttr ? parseNetDuration(durationAttr) : 0;
|
||||||
|
|
||||||
|
const stdOut = r.result.Output?.[0]?.StdOut?.join('\n') || ''; // Extract StdOut
|
||||||
|
|
||||||
|
const resultTestName = r.result.$.testName;
|
||||||
const testName =
|
const testName =
|
||||||
resultTestName.startsWith(className) && resultTestName[className.length] === '.'
|
resultTestName.startsWith(className) && resultTestName[className.length] === '.'
|
||||||
? resultTestName.substr(className.length + 1)
|
? resultTestName.substr(className.length + 1)
|
||||||
: resultTestName
|
: resultTestName;
|
||||||
|
|
||||||
const test = new Test(testName, r.result.$.outcome, duration, error)
|
const test = new Test(testName, r.result.$.outcome, duration, error, stdOut);
|
||||||
tc.tests.push(test)
|
tc.tests.push(test);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = Object.values(testClasses)
|
const result = Object.values(testClasses);
|
||||||
return result
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getTestRunResult(path: string, trx: TrxReport, testClasses: TestClass[]): TestRunResult {
|
private getTestRunResult(path: string, trx: TrxReport, testClasses: TestClass[]): TestRunResult {
|
||||||
const times = trx.TestRun.Times[0].$
|
const times = trx.TestRun.Times[0].$;
|
||||||
const totalTime = parseIsoDate(times.finish).getTime() - parseIsoDate(times.start).getTime()
|
const totalTime = parseIsoDate(times.finish).getTime() - parseIsoDate(times.start).getTime();
|
||||||
|
|
||||||
const suites = testClasses.map(testClass => {
|
const suites = testClasses.map((testClass) => {
|
||||||
const tests = testClass.tests.map(test => {
|
const tests = testClass.tests.map((test) => {
|
||||||
const error = this.getError(test)
|
const error = this.getError(test);
|
||||||
return new TestCaseResult(test.name, test.result, test.duration, error)
|
return new TestCaseResult(test.name, test.result, test.duration, error);
|
||||||
})
|
});
|
||||||
const group = new TestGroupResult(null, tests)
|
const group = new TestGroupResult(null, tests);
|
||||||
return new TestSuiteResult(testClass.name, [group])
|
return new TestSuiteResult(testClass.name, [group]);
|
||||||
})
|
});
|
||||||
|
|
||||||
return new TestRunResult(path, suites, totalTime)
|
return new TestRunResult(path, suites, totalTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getErrorInfo(testResult: UnitTestResult): ErrorInfo | undefined {
|
private getErrorInfo(testResult: UnitTestResult): ErrorInfo | undefined {
|
||||||
if (testResult.$.outcome !== 'Failed') {
|
if (testResult.$.outcome !== 'Failed') {
|
||||||
return undefined
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const output = testResult.Output
|
const output = testResult.Output;
|
||||||
const error = output?.length > 0 && output[0].ErrorInfo?.length > 0 ? output[0].ErrorInfo[0] : undefined
|
const error = output?.length > 0 && output[0].ErrorInfo?.length > 0 ? output[0].ErrorInfo[0] : undefined;
|
||||||
return error
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getError(test: Test): TestCaseError | undefined {
|
private getError(test: Test): TestCaseError | undefined {
|
||||||
if (!this.options.parseErrors || !test.error) {
|
if (!this.options.parseErrors || !test.error) {
|
||||||
return undefined
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const error = test.error
|
const error = test.error;
|
||||||
if (
|
if (
|
||||||
!Array.isArray(error.Message) ||
|
!Array.isArray(error.Message) ||
|
||||||
error.Message.length === 0 ||
|
error.Message.length === 0 ||
|
||||||
!Array.isArray(error.StackTrace) ||
|
!Array.isArray(error.StackTrace) ||
|
||||||
error.StackTrace.length === 0
|
error.StackTrace.length === 0
|
||||||
) {
|
) {
|
||||||
return undefined
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = test.error.Message[0]
|
const message = error.Message[0];
|
||||||
const stackTrace = test.error.StackTrace[0]
|
const stackTrace = error.StackTrace[0];
|
||||||
const stdOut = test.error.StdOut?.join('\n') || ''
|
const stdOut = test.stdOut || ''; // Use StdOut from Test object
|
||||||
let path
|
let path;
|
||||||
let line
|
let line;
|
||||||
|
|
||||||
const src = this.exceptionThrowSource(stackTrace)
|
const src = this.exceptionThrowSource(stackTrace);
|
||||||
if (src) {
|
if (src) {
|
||||||
path = src.path
|
path = src.path;
|
||||||
line = src.line
|
line = src.line;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
path,
|
path,
|
||||||
line,
|
line,
|
||||||
message,
|
message,
|
||||||
details: `${message}\n${stackTrace}\n${stdOut}`
|
details: `${message}\n${stackTrace}\n${stdOut}`,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private exceptionThrowSource(stackTrace: string): { path: string; line: number } | undefined {
|
private exceptionThrowSource(stackTrace: string): { path: string; line: number } | undefined {
|
||||||
const lines = stackTrace.split(/\r*\n/)
|
const lines = stackTrace.split(/\r*\n/);
|
||||||
const re = / in (.+):line (\d+)$/
|
const re = / in (.+):line (\d+)$/;
|
||||||
const {trackedFiles} = this.options
|
const { trackedFiles } = this.options;
|
||||||
|
|
||||||
for (const str of lines) {
|
for (const str of lines) {
|
||||||
const match = str.match(re)
|
const match = str.match(re);
|
||||||
if (match !== null) {
|
if (match !== null) {
|
||||||
const [_, fileStr, lineStr] = match
|
const [_, fileStr, lineStr] = match;
|
||||||
const filePath = normalizeFilePath(fileStr)
|
const filePath = normalizeFilePath(fileStr);
|
||||||
const workDir = this.getWorkDir(filePath)
|
const workDir = this.getWorkDir(filePath);
|
||||||
if (workDir) {
|
if (workDir) {
|
||||||
const file = filePath.substr(workDir.length)
|
const file = filePath.substr(workDir.length);
|
||||||
if (trackedFiles.includes(file)) {
|
if (trackedFiles.includes(file)) {
|
||||||
const line = parseInt(lineStr)
|
const line = parseInt(lineStr);
|
||||||
return {path: file, line}
|
return { path: file, line };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -192,6 +198,6 @@ export class DotnetTrxParser implements TestParser {
|
||||||
this.options.workDir ??
|
this.options.workDir ??
|
||||||
this.assumedWorkDir ??
|
this.assumedWorkDir ??
|
||||||
(this.assumedWorkDir = getBasePath(path, this.options.trackedFiles))
|
(this.assumedWorkDir = getBasePath(path, this.options.trackedFiles))
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ export interface UnitTestResult {
|
||||||
|
|
||||||
export interface Output {
|
export interface Output {
|
||||||
ErrorInfo: ErrorInfo[]
|
ErrorInfo: ErrorInfo[]
|
||||||
StdOut: string[]
|
StdOut: string[] | undefined;
|
||||||
}
|
}
|
||||||
export interface ErrorInfo {
|
export interface ErrorInfo {
|
||||||
Message: string[]
|
Message: string[]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue