mirror of
https://github.com/dorny/test-reporter.git
synced 2025-12-16 14:27:10 +01:00
Refactoring & cleanup of whole codebase
Improves report summary and annotations
This commit is contained in:
parent
07a0223ee3
commit
60b35d601a
20 changed files with 38784 additions and 33667 deletions
|
|
@ -1,11 +1,10 @@
|
|||
import * as core from '@actions/core'
|
||||
import {ErrorInfo, Outcome, TestMethod, TrxReport} from './dotnet-trx-types'
|
||||
|
||||
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
|
||||
import {parseStringPromise} from 'xml2js'
|
||||
|
||||
import {ErrorInfo, Outcome, TestMethod, TrxReport} from './dotnet-trx-types'
|
||||
import {ParseOptions, TestParser} from '../../test-parser'
|
||||
|
||||
import {normalizeFilePath} from '../../utils/file-utils'
|
||||
import {fixEol} from '../../utils/markdown-utils'
|
||||
import {parseIsoDate, parseNetDuration} from '../../utils/parse-utils'
|
||||
|
||||
import {
|
||||
|
|
@ -13,8 +12,9 @@ import {
|
|||
TestRunResult,
|
||||
TestSuiteResult,
|
||||
TestGroupResult,
|
||||
TestCaseResult
|
||||
} from '../../report/test-results'
|
||||
TestCaseResult,
|
||||
TestCaseError
|
||||
} from '../../test-results'
|
||||
|
||||
class TestClass {
|
||||
constructor(readonly name: string) {}
|
||||
|
|
@ -41,125 +41,117 @@ class Test {
|
|||
}
|
||||
}
|
||||
|
||||
export async function parseDotnetTrx(files: FileContent[], options: ParseOptions): Promise<TestResult> {
|
||||
const testRuns: TestRunResult[] = []
|
||||
const testClasses: TestClass[] = []
|
||||
export class DotnetTrxParser implements TestParser {
|
||||
constructor(readonly options: ParseOptions) {}
|
||||
|
||||
for (const file of files) {
|
||||
const trx = await getTrxReport(file)
|
||||
const tc = getTestClasses(trx)
|
||||
const tr = getTestRunResult(file.path, trx, tc)
|
||||
testRuns.push(tr)
|
||||
testClasses.push(...tc)
|
||||
async parse(path: string, content: string): Promise<TestRunResult> {
|
||||
const trx = await this.getTrxReport(path, content)
|
||||
const tc = this.getTestClasses(trx)
|
||||
const tr = this.getTestRunResult(path, trx, tc)
|
||||
return tr
|
||||
}
|
||||
|
||||
return {
|
||||
testRuns,
|
||||
annotations: options.annotations ? getAnnotations(testClasses, options.workDir, options.trackedFiles) : []
|
||||
}
|
||||
}
|
||||
|
||||
async function getTrxReport(file: FileContent): Promise<TrxReport> {
|
||||
core.info(`Parsing content of '${file.path}'`)
|
||||
try {
|
||||
return (await parseStringPromise(file.content)) as TrxReport
|
||||
} catch (e) {
|
||||
throw new Error(`Invalid XML at ${file.path}\n\n${e}`)
|
||||
}
|
||||
}
|
||||
|
||||
function getTestRunResult(path: string, trx: TrxReport, testClasses: TestClass[]): TestRunResult {
|
||||
const times = trx.TestRun.Times[0].$
|
||||
const totalTime = parseIsoDate(times.finish).getTime() - parseIsoDate(times.start).getTime()
|
||||
|
||||
const suites = testClasses.map(tc => {
|
||||
const tests = tc.tests.map(t => new TestCaseResult(t.name, t.result, t.duration))
|
||||
const group = new TestGroupResult(null, tests)
|
||||
return new TestSuiteResult(tc.name, [group])
|
||||
})
|
||||
|
||||
return new TestRunResult(path, suites, totalTime)
|
||||
}
|
||||
|
||||
function getTestClasses(trx: TrxReport): TestClass[] {
|
||||
const unitTests: {[id: string]: TestMethod} = {}
|
||||
for (const td of trx.TestRun.TestDefinitions) {
|
||||
for (const ut of td.UnitTest) {
|
||||
unitTests[ut.$.id] = ut.TestMethod[0]
|
||||
private async getTrxReport(path: string, content: string): Promise<TrxReport> {
|
||||
core.info(`Parsing content of '${path}'`)
|
||||
try {
|
||||
return (await parseStringPromise(content)) as TrxReport
|
||||
} catch (e) {
|
||||
throw new Error(`Invalid XML at ${path}\n\n${e}`)
|
||||
}
|
||||
}
|
||||
|
||||
const unitTestsResults = trx.TestRun.Results.flatMap(r => r.UnitTestResult).flatMap(unitTestResult => ({
|
||||
unitTestResult,
|
||||
testMethod: unitTests[unitTestResult.$.testId]
|
||||
}))
|
||||
|
||||
const testClasses: {[name: string]: TestClass} = {}
|
||||
for (const r of unitTestsResults) {
|
||||
let tc = testClasses[r.testMethod.$.className]
|
||||
if (tc === undefined) {
|
||||
tc = new TestClass(r.testMethod.$.className)
|
||||
testClasses[tc.name] = tc
|
||||
private getTestClasses(trx: TrxReport): TestClass[] {
|
||||
const unitTests: {[id: string]: TestMethod} = {}
|
||||
for (const td of trx.TestRun.TestDefinitions) {
|
||||
for (const ut of td.UnitTest) {
|
||||
unitTests[ut.$.id] = ut.TestMethod[0]
|
||||
}
|
||||
}
|
||||
const output = r.unitTestResult.Output
|
||||
const error = output?.length > 0 && output[0].ErrorInfo?.length > 0 ? output[0].ErrorInfo[0] : undefined
|
||||
const duration = parseNetDuration(r.unitTestResult.$.duration)
|
||||
const test = new Test(r.testMethod.$.name, r.unitTestResult.$.outcome, duration, error)
|
||||
tc.tests.push(test)
|
||||
|
||||
const unitTestsResults = trx.TestRun.Results.flatMap(r => r.UnitTestResult).flatMap(unitTestResult => ({
|
||||
unitTestResult,
|
||||
testMethod: unitTests[unitTestResult.$.testId]
|
||||
}))
|
||||
|
||||
const testClasses: {[name: string]: TestClass} = {}
|
||||
for (const r of unitTestsResults) {
|
||||
let tc = testClasses[r.testMethod.$.className]
|
||||
if (tc === undefined) {
|
||||
tc = new TestClass(r.testMethod.$.className)
|
||||
testClasses[tc.name] = tc
|
||||
}
|
||||
const output = r.unitTestResult.Output
|
||||
const error = output?.length > 0 && output[0].ErrorInfo?.length > 0 ? output[0].ErrorInfo[0] : undefined
|
||||
const duration = parseNetDuration(r.unitTestResult.$.duration)
|
||||
const test = new Test(r.testMethod.$.name, r.unitTestResult.$.outcome, duration, error)
|
||||
tc.tests.push(test)
|
||||
}
|
||||
|
||||
const result = Object.values(testClasses)
|
||||
result.sort((a, b) => a.name.localeCompare(b.name))
|
||||
for (const tc of result) {
|
||||
tc.tests.sort((a, b) => a.name.localeCompare(b.name))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
const result = Object.values(testClasses)
|
||||
result.sort((a, b) => a.name.localeCompare(b.name))
|
||||
for (const tc of result) {
|
||||
tc.tests.sort((a, b) => a.name.localeCompare(b.name))
|
||||
private getTestRunResult(path: string, trx: TrxReport, testClasses: TestClass[]): TestRunResult {
|
||||
const times = trx.TestRun.Times[0].$
|
||||
const totalTime = parseIsoDate(times.finish).getTime() - parseIsoDate(times.start).getTime()
|
||||
|
||||
const suites = testClasses.map(testClass => {
|
||||
const tests = testClass.tests.map(test => {
|
||||
const error = this.getError(test)
|
||||
return new TestCaseResult(test.name, test.result, test.duration, error)
|
||||
})
|
||||
const group = new TestGroupResult(null, tests)
|
||||
return new TestSuiteResult(testClass.name, [group])
|
||||
})
|
||||
|
||||
return new TestRunResult(path, suites, totalTime)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
private getError(test: Test): TestCaseError | undefined {
|
||||
if (!this.options.parseErrors || !test.error) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
function getAnnotations(testClasses: TestClass[], workDir: string, trackedFiles: string[]): Annotation[] {
|
||||
const annotations: Annotation[] = []
|
||||
for (const tc of testClasses) {
|
||||
for (const t of tc.tests) {
|
||||
if (t.error) {
|
||||
const src = exceptionThrowSource(t.error.StackTrace[0], workDir, trackedFiles)
|
||||
if (src === null) {
|
||||
continue
|
||||
const message = test.error.Message[0]
|
||||
const stackTrace = test.error.StackTrace[0]
|
||||
let path
|
||||
let line
|
||||
|
||||
const src = this.exceptionThrowSource(stackTrace)
|
||||
if (src) {
|
||||
path = src.path
|
||||
line = src.line
|
||||
}
|
||||
|
||||
return {
|
||||
path,
|
||||
line,
|
||||
message,
|
||||
stackTrace: `${message}\n${stackTrace}`
|
||||
}
|
||||
}
|
||||
|
||||
private exceptionThrowSource(stackTrace: string): {path: string; line: number} | undefined {
|
||||
const lines = stackTrace.split(/\r*\n/)
|
||||
const re = / in (.+):line (\d+)$/
|
||||
const {workDir, trackedFiles} = this.options
|
||||
|
||||
for (const str of lines) {
|
||||
const match = str.match(re)
|
||||
if (match !== null) {
|
||||
const [_, fileStr, lineStr] = match
|
||||
const filePath = normalizeFilePath(fileStr)
|
||||
const file = filePath.startsWith(workDir) ? filePath.substr(workDir.length) : filePath
|
||||
if (trackedFiles.includes(file)) {
|
||||
const line = parseInt(lineStr)
|
||||
return {path: file, line}
|
||||
}
|
||||
annotations.push({
|
||||
annotation_level: 'failure',
|
||||
start_line: src.line,
|
||||
end_line: src.line,
|
||||
path: src.file,
|
||||
message: fixEol(t.error.Message[0]),
|
||||
title: `[${tc.name}] ${t.name}`
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return annotations
|
||||
}
|
||||
|
||||
export function exceptionThrowSource(
|
||||
ex: string,
|
||||
workDir: string,
|
||||
trackedFiles: string[]
|
||||
): {file: string; line: number} | null {
|
||||
const lines = ex.split(/\r*\n/)
|
||||
const re = / in (.+):line (\d+)$/
|
||||
|
||||
for (const str of lines) {
|
||||
const match = str.match(re)
|
||||
if (match !== null) {
|
||||
const [_, fileStr, lineStr] = match
|
||||
const filePath = normalizeFilePath(fileStr)
|
||||
const file = filePath.startsWith(workDir) ? filePath.substr(workDir.length) : filePath
|
||||
if (trackedFiles.includes(file)) {
|
||||
const line = parseInt(lineStr)
|
||||
return {file, line}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue