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,8 +1,7 @@
|
|||
import * as core from '@actions/core'
|
||||
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
|
||||
import {ParseOptions, TestParser} from '../../test-parser'
|
||||
|
||||
import {normalizeFilePath} from '../../utils/file-utils'
|
||||
import {fixEol} from '../../utils/markdown-utils'
|
||||
|
||||
import {
|
||||
ReportEvent,
|
||||
|
|
@ -24,8 +23,9 @@ import {
|
|||
TestRunResult,
|
||||
TestSuiteResult,
|
||||
TestGroupResult,
|
||||
TestCaseResult
|
||||
} from '../../report/test-results'
|
||||
TestCaseResult,
|
||||
TestCaseError
|
||||
} from '../../test-results'
|
||||
|
||||
class TestRun {
|
||||
constructor(readonly path: string, readonly suites: TestSuite[], readonly success: boolean, readonly time: number) {}
|
||||
|
|
@ -68,159 +68,143 @@ class TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
export async function parseDartJson(files: FileContent[], options: ParseOptions): Promise<TestResult> {
|
||||
const testRuns = files.map(f => getTestRun(f.path, f.content))
|
||||
const testRunsResults = testRuns.map(getTestRunResult)
|
||||
export class DartJsonParser implements TestParser {
|
||||
constructor(readonly options: ParseOptions) {}
|
||||
|
||||
return {
|
||||
testRuns: testRunsResults,
|
||||
annotations: options.annotations ? getAnnotations(testRuns, options.workDir, options.trackedFiles) : []
|
||||
async parse(path: string, content: string): Promise<TestRunResult> {
|
||||
const tr = this.getTestRun(path, content)
|
||||
const result = this.getTestRunResult(tr)
|
||||
return Promise.resolve(result)
|
||||
}
|
||||
}
|
||||
|
||||
function getTestRun(path: string, content: string): TestRun {
|
||||
core.info(`Parsing content of '${path}'`)
|
||||
const lines = content.split(/\n\r?/g)
|
||||
const events = lines
|
||||
.map((str, i) => {
|
||||
if (str.trim() === '') {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return JSON.parse(str)
|
||||
} catch (e) {
|
||||
const col = e.columnNumber !== undefined ? `:${e.columnNumber}` : ''
|
||||
new Error(`Invalid JSON at ${path}:${i + 1}${col}\n\n${e}`)
|
||||
private getTestRun(path: string, content: string): TestRun {
|
||||
core.info(`Parsing content of '${path}'`)
|
||||
const lines = content.split(/\n\r?/g)
|
||||
const events = lines
|
||||
.map((str, i) => {
|
||||
if (str.trim() === '') {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
return JSON.parse(str)
|
||||
} catch (e) {
|
||||
const col = e.columnNumber !== undefined ? `:${e.columnNumber}` : ''
|
||||
new Error(`Invalid JSON at ${path}:${i + 1}${col}\n\n${e}`)
|
||||
}
|
||||
})
|
||||
.filter(evt => evt != null) as ReportEvent[]
|
||||
|
||||
let success = false
|
||||
let totalTime = 0
|
||||
const suites: {[id: number]: TestSuite} = {}
|
||||
const tests: {[id: number]: TestCase} = {}
|
||||
|
||||
for (const evt of events) {
|
||||
if (isSuiteEvent(evt)) {
|
||||
suites[evt.suite.id] = new TestSuite(evt.suite)
|
||||
} else if (isGroupEvent(evt)) {
|
||||
suites[evt.group.suiteID].groups[evt.group.id] = new TestGroup(evt.group)
|
||||
} else if (isTestStartEvent(evt) && evt.test.url !== null) {
|
||||
const test: TestCase = new TestCase(evt)
|
||||
const suite = suites[evt.test.suiteID]
|
||||
const group = suite.groups[evt.test.groupIDs[evt.test.groupIDs.length - 1]]
|
||||
group.tests.push(test)
|
||||
tests[evt.test.id] = test
|
||||
} else if (isTestDoneEvent(evt) && !evt.hidden) {
|
||||
tests[evt.testID].testDone = evt
|
||||
} else if (isErrorEvent(evt)) {
|
||||
tests[evt.testID].error = evt
|
||||
} else if (isDoneEvent(evt)) {
|
||||
success = evt.success
|
||||
totalTime = evt.time
|
||||
}
|
||||
}
|
||||
|
||||
return new TestRun(path, Object.values(suites), success, totalTime)
|
||||
}
|
||||
|
||||
private getTestRunResult(tr: TestRun): TestRunResult {
|
||||
const suites = tr.suites.map(s => {
|
||||
return new TestSuiteResult(s.suite.path, this.getGroups(s))
|
||||
})
|
||||
.filter(evt => evt != null) as ReportEvent[]
|
||||
|
||||
let success = false
|
||||
let totalTime = 0
|
||||
const suites: {[id: number]: TestSuite} = {}
|
||||
const tests: {[id: number]: TestCase} = {}
|
||||
return new TestRunResult(tr.path, suites, tr.time)
|
||||
}
|
||||
|
||||
for (const evt of events) {
|
||||
if (isSuiteEvent(evt)) {
|
||||
suites[evt.suite.id] = new TestSuite(evt.suite)
|
||||
} else if (isGroupEvent(evt)) {
|
||||
suites[evt.group.suiteID].groups[evt.group.id] = new TestGroup(evt.group)
|
||||
} else if (isTestStartEvent(evt) && evt.test.url !== null) {
|
||||
const test: TestCase = new TestCase(evt)
|
||||
const suite = suites[evt.test.suiteID]
|
||||
const group = suite.groups[evt.test.groupIDs[evt.test.groupIDs.length - 1]]
|
||||
group.tests.push(test)
|
||||
tests[evt.test.id] = test
|
||||
} else if (isTestDoneEvent(evt) && !evt.hidden) {
|
||||
tests[evt.testID].testDone = evt
|
||||
} else if (isErrorEvent(evt)) {
|
||||
tests[evt.testID].error = evt
|
||||
} else if (isDoneEvent(evt)) {
|
||||
success = evt.success
|
||||
totalTime = evt.time
|
||||
private getGroups(suite: TestSuite): TestGroupResult[] {
|
||||
const groups = Object.values(suite.groups).filter(grp => grp.tests.length > 0)
|
||||
groups.sort((a, b) => (a.group.line ?? 0) - (b.group.line ?? 0))
|
||||
|
||||
return groups.map(group => {
|
||||
group.tests.sort((a, b) => (a.testStart.test.line ?? 0) - (b.testStart.test.line ?? 0))
|
||||
const tests = group.tests.map(t => this.getTest(t))
|
||||
return new TestGroupResult(group.group.name, tests)
|
||||
})
|
||||
}
|
||||
|
||||
private getTest(tc: TestCase): TestCaseResult {
|
||||
const error = this.getError(tc)
|
||||
return new TestCaseResult(tc.testStart.test.name, tc.result, tc.time, error)
|
||||
}
|
||||
|
||||
private getError(test: TestCase): TestCaseError | undefined {
|
||||
if (!this.options.parseErrors || !test.error) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const {workDir, trackedFiles} = this.options
|
||||
const message = test.error?.error ?? ''
|
||||
const stackTrace = test.error?.stackTrace ?? ''
|
||||
const src = this.exceptionThrowSource(stackTrace, trackedFiles)
|
||||
let path
|
||||
let line
|
||||
|
||||
if (src !== undefined) {
|
||||
;(path = src.path), (line = src.line)
|
||||
} else {
|
||||
const testStartPath = this.getRelativePathFromUrl(test.testStart.test.url ?? '', workDir)
|
||||
if (trackedFiles.includes(testStartPath)) {
|
||||
path = testStartPath
|
||||
}
|
||||
line = test.testStart.test.line ?? undefined
|
||||
}
|
||||
|
||||
return {
|
||||
path,
|
||||
line,
|
||||
message,
|
||||
stackTrace
|
||||
}
|
||||
}
|
||||
|
||||
return new TestRun(path, Object.values(suites), success, totalTime)
|
||||
}
|
||||
private exceptionThrowSource(ex: string, trackedFiles: string[]): {path: string; line: number} | undefined {
|
||||
// imports from package which is tested are listed in stack traces as 'package:xyz/' which maps to relative path 'lib/'
|
||||
const packageRe = /^package:[a-zA-z0-9_$]+\//
|
||||
const lines = ex.split(/\r?\n/).map(str => str.replace(packageRe, 'lib/'))
|
||||
|
||||
function getTestRunResult(tr: TestRun): TestRunResult {
|
||||
const suites = tr.suites.map(s => {
|
||||
return new TestSuiteResult(s.suite.path, getGroups(s))
|
||||
})
|
||||
|
||||
return new TestRunResult(tr.path, suites, tr.time)
|
||||
}
|
||||
|
||||
function getGroups(suite: TestSuite): TestGroupResult[] {
|
||||
const groups = Object.values(suite.groups).filter(grp => grp.tests.length > 0)
|
||||
groups.sort((a, b) => (a.group.line ?? 0) - (b.group.line ?? 0))
|
||||
|
||||
return groups.map(group => {
|
||||
group.tests.sort((a, b) => (a.testStart.test.line ?? 0) - (b.testStart.test.line ?? 0))
|
||||
const tests = group.tests.map(t => new TestCaseResult(t.testStart.test.name, t.result, t.time))
|
||||
return new TestGroupResult(group.group.name, tests)
|
||||
})
|
||||
}
|
||||
|
||||
function getAnnotations(testRuns: TestRun[], workDir: string, trackedFiles: string[]): Annotation[] {
|
||||
const annotations: Annotation[] = []
|
||||
for (const tr of testRuns) {
|
||||
for (const suite of tr.suites) {
|
||||
for (const group of Object.values(suite.groups)) {
|
||||
for (const test of group.tests) {
|
||||
if (test.error) {
|
||||
const err = getAnnotation(test, suite, workDir, trackedFiles)
|
||||
if (err !== null) {
|
||||
annotations.push(err)
|
||||
}
|
||||
}
|
||||
// regexp to extract file path and line number from stack trace
|
||||
const re = /^(.*)\s+(\d+):\d+\s+/
|
||||
for (const str of lines) {
|
||||
const match = str.match(re)
|
||||
if (match !== null) {
|
||||
const [_, pathStr, lineStr] = match
|
||||
const path = normalizeFilePath(pathStr)
|
||||
if (trackedFiles.includes(path)) {
|
||||
const line = parseInt(lineStr)
|
||||
return {path, line}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return annotations
|
||||
}
|
||||
|
||||
function getAnnotation(
|
||||
test: TestCase,
|
||||
testSuite: TestSuite,
|
||||
workDir: string,
|
||||
trackedFiles: string[]
|
||||
): Annotation | null {
|
||||
const stack = test.error?.stackTrace ?? ''
|
||||
let src = exceptionThrowSource(stack, trackedFiles)
|
||||
if (src === null) {
|
||||
const file = getRelativePathFromUrl(test.testStart.test.url ?? '', workDir)
|
||||
if (!trackedFiles.includes(file)) {
|
||||
return null
|
||||
private getRelativePathFromUrl(file: string, workDir: string): string {
|
||||
const prefix = 'file:///'
|
||||
if (file.startsWith(prefix)) {
|
||||
file = file.substr(prefix.length)
|
||||
}
|
||||
src = {
|
||||
file,
|
||||
line: test.testStart.test.line ?? 0
|
||||
if (file.startsWith(workDir)) {
|
||||
file = file.substr(workDir.length)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
annotation_level: 'failure',
|
||||
start_line: src.line,
|
||||
end_line: src.line,
|
||||
path: src.file,
|
||||
message: `${fixEol(test.error?.error)}\n\n${fixEol(test.error?.stackTrace)}`,
|
||||
title: `[${testSuite.suite.path}] ${test.testStart.test.name}`
|
||||
return file
|
||||
}
|
||||
}
|
||||
|
||||
function exceptionThrowSource(ex: string, trackedFiles: string[]): {file: string; line: number} | null {
|
||||
// imports from package which is tested are listed in stack traces as 'package:xyz/' which maps to relative path 'lib/'
|
||||
const packageRe = /^package:[a-zA-z0-9_$]+\//
|
||||
const lines = ex.split(/\r?\n/).map(str => str.replace(packageRe, 'lib/'))
|
||||
|
||||
// regexp to extract file path and line number from stack trace
|
||||
const re = /^(.*)\s+(\d+):\d+\s+/
|
||||
for (const str of lines) {
|
||||
const match = str.match(re)
|
||||
if (match !== null) {
|
||||
const [_, fileStr, lineStr] = match
|
||||
const file = normalizeFilePath(fileStr)
|
||||
if (trackedFiles.includes(file)) {
|
||||
const line = parseInt(lineStr)
|
||||
return {file, line}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function getRelativePathFromUrl(file: string, workdir: string): string {
|
||||
const prefix = 'file:///'
|
||||
if (file.startsWith(prefix)) {
|
||||
file = file.substr(prefix.length)
|
||||
}
|
||||
if (file.startsWith(workdir)) {
|
||||
file = file.substr(workdir.length)
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
import * as core from '@actions/core'
|
||||
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
|
||||
import {ParseOptions, TestParser} from '../../test-parser'
|
||||
import {parseStringPromise} from 'xml2js'
|
||||
|
||||
import {JunitReport, TestCase, TestSuite} from './jest-junit-types'
|
||||
import {fixEol} from '../../utils/markdown-utils'
|
||||
import {normalizeFilePath} from '../../utils/file-utils'
|
||||
|
||||
import {
|
||||
|
|
@ -11,124 +10,107 @@ import {
|
|||
TestRunResult,
|
||||
TestSuiteResult,
|
||||
TestGroupResult,
|
||||
TestCaseResult
|
||||
} from '../../report/test-results'
|
||||
TestCaseResult,
|
||||
TestCaseError
|
||||
} from '../../test-results'
|
||||
|
||||
export async function parseJestJunit(files: FileContent[], options: ParseOptions): Promise<TestResult> {
|
||||
const junit: JunitReport[] = []
|
||||
const testRuns: TestRunResult[] = []
|
||||
export class JestJunitParser implements TestParser {
|
||||
constructor(readonly options: ParseOptions) {}
|
||||
|
||||
for (const file of files) {
|
||||
const ju = await getJunitReport(file)
|
||||
const tr = getTestRunResult(file.path, ju)
|
||||
junit.push(ju)
|
||||
testRuns.push(tr)
|
||||
async parse(path: string, content: string): Promise<TestRunResult> {
|
||||
const ju = await this.getJunitReport(path, content)
|
||||
return this.getTestRunResult(path, ju)
|
||||
}
|
||||
|
||||
return {
|
||||
testRuns,
|
||||
annotations: options.annotations ? getAnnotations(junit, options.workDir, options.trackedFiles) : []
|
||||
}
|
||||
}
|
||||
|
||||
async function getJunitReport(file: FileContent): Promise<JunitReport> {
|
||||
core.info(`Parsing content of '${file.path}'`)
|
||||
try {
|
||||
return (await parseStringPromise(file.content)) as JunitReport
|
||||
} catch (e) {
|
||||
throw new Error(`Invalid XML at ${file.path}\n\n${e}`)
|
||||
}
|
||||
}
|
||||
|
||||
function getTestRunResult(path: string, junit: JunitReport): TestRunResult {
|
||||
const suites = junit.testsuites.testsuite.map(ts => {
|
||||
const name = ts.$.name.trim()
|
||||
const time = parseFloat(ts.$.time) * 1000
|
||||
const sr = new TestSuiteResult(name, getGroups(ts), time)
|
||||
return sr
|
||||
})
|
||||
|
||||
const time = parseFloat(junit.testsuites.$.time) * 1000
|
||||
return new TestRunResult(path, suites, time)
|
||||
}
|
||||
|
||||
function getGroups(suite: TestSuite): TestGroupResult[] {
|
||||
const groups: {describe: string; tests: TestCase[]}[] = []
|
||||
for (const tc of suite.testcase) {
|
||||
let grp = groups.find(g => g.describe === tc.$.classname)
|
||||
if (grp === undefined) {
|
||||
grp = {describe: tc.$.classname, tests: []}
|
||||
groups.push(grp)
|
||||
private async getJunitReport(path: string, content: string): Promise<JunitReport> {
|
||||
core.info(`Parsing content of '${path}'`)
|
||||
try {
|
||||
return (await parseStringPromise(content)) as JunitReport
|
||||
} catch (e) {
|
||||
throw new Error(`Invalid XML at ${path}\n\n${e}`)
|
||||
}
|
||||
grp.tests.push(tc)
|
||||
}
|
||||
|
||||
return groups.map(grp => {
|
||||
const tests = grp.tests.map(tc => {
|
||||
const name = tc.$.name.trim()
|
||||
const result = getTestCaseResult(tc)
|
||||
const time = parseFloat(tc.$.time) * 1000
|
||||
return new TestCaseResult(name, result, time)
|
||||
private getTestRunResult(path: string, junit: JunitReport): TestRunResult {
|
||||
const suites = junit.testsuites.testsuite.map(ts => {
|
||||
const name = ts.$.name.trim()
|
||||
const time = parseFloat(ts.$.time) * 1000
|
||||
const sr = new TestSuiteResult(name, this.getGroups(ts), time)
|
||||
return sr
|
||||
})
|
||||
return new TestGroupResult(grp.describe, tests)
|
||||
})
|
||||
}
|
||||
|
||||
function getTestCaseResult(test: TestCase): TestExecutionResult {
|
||||
if (test.failure) return 'failed'
|
||||
if (test.skipped) return 'skipped'
|
||||
return 'success'
|
||||
}
|
||||
const time = parseFloat(junit.testsuites.$.time) * 1000
|
||||
return new TestRunResult(path, suites, time)
|
||||
}
|
||||
|
||||
function getAnnotations(junitReports: JunitReport[], workDir: string, trackedFiles: string[]): Annotation[] {
|
||||
const annotations: Annotation[] = []
|
||||
for (const junit of junitReports) {
|
||||
for (const suite of junit.testsuites.testsuite) {
|
||||
for (const tc of suite.testcase) {
|
||||
if (!tc.failure) {
|
||||
continue
|
||||
}
|
||||
for (const ex of tc.failure) {
|
||||
const src = exceptionThrowSource(ex, workDir, trackedFiles)
|
||||
if (src === null) {
|
||||
continue
|
||||
}
|
||||
annotations.push({
|
||||
annotation_level: 'failure',
|
||||
start_line: src.line,
|
||||
end_line: src.line,
|
||||
path: src.file,
|
||||
message: fixEol(ex),
|
||||
title: `[${suite.$.name}] ${tc.$.name.trim()}`
|
||||
})
|
||||
private getGroups(suite: TestSuite): TestGroupResult[] {
|
||||
const groups: {describe: string; tests: TestCase[]}[] = []
|
||||
for (const tc of suite.testcase) {
|
||||
let grp = groups.find(g => g.describe === tc.$.classname)
|
||||
if (grp === undefined) {
|
||||
grp = {describe: tc.$.classname, tests: []}
|
||||
groups.push(grp)
|
||||
}
|
||||
grp.tests.push(tc)
|
||||
}
|
||||
|
||||
return groups.map(grp => {
|
||||
const tests = grp.tests.map(tc => {
|
||||
const name = tc.$.name.trim()
|
||||
const result = this.getTestCaseResult(tc)
|
||||
const time = parseFloat(tc.$.time) * 1000
|
||||
const error = this.getTestCaseError(tc)
|
||||
return new TestCaseResult(name, result, time, error)
|
||||
})
|
||||
return new TestGroupResult(grp.describe, tests)
|
||||
})
|
||||
}
|
||||
|
||||
private getTestCaseResult(test: TestCase): TestExecutionResult {
|
||||
if (test.failure) return 'failed'
|
||||
if (test.skipped) return 'skipped'
|
||||
return 'success'
|
||||
}
|
||||
|
||||
private getTestCaseError(tc: TestCase): TestCaseError | undefined {
|
||||
if (!this.options.parseErrors || !tc.failure) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const stackTrace = tc.failure[0]
|
||||
let path
|
||||
let line
|
||||
|
||||
const src = this.exceptionThrowSource(stackTrace)
|
||||
if (src) {
|
||||
path = src.path
|
||||
line = src.line
|
||||
}
|
||||
|
||||
return {
|
||||
path,
|
||||
line,
|
||||
stackTrace
|
||||
}
|
||||
}
|
||||
|
||||
private exceptionThrowSource(stackTrace: string): {path: string; line: number} | undefined {
|
||||
const lines = stackTrace.split(/\r?\n/)
|
||||
const re = /\((.*):(\d+):\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 path = filePath.startsWith(workDir) ? filePath.substr(workDir.length) : filePath
|
||||
if (trackedFiles.includes(path)) {
|
||||
const line = parseInt(lineStr)
|
||||
|
||||
return {path, line}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return annotations
|
||||
}
|
||||
|
||||
export function exceptionThrowSource(
|
||||
ex: string,
|
||||
workDir: string,
|
||||
trackedFiles: string[]
|
||||
): {file: string; line: number; column: number} | null {
|
||||
const lines = ex.split(/\r?\n/)
|
||||
const re = /\((.*):(\d+):(\d+)\)$/
|
||||
|
||||
for (const str of lines) {
|
||||
const match = str.match(re)
|
||||
if (match !== null) {
|
||||
const [_, fileStr, lineStr, colStr] = match
|
||||
const filePath = normalizeFilePath(fileStr)
|
||||
const file = filePath.startsWith(workDir) ? filePath.substr(workDir.length) : filePath
|
||||
if (trackedFiles.includes(file)) {
|
||||
const line = parseInt(lineStr)
|
||||
const column = parseInt(colStr)
|
||||
return {file, line, column}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +0,0 @@
|
|||
import {TestRunResult} from '../report/test-results'
|
||||
|
||||
export type Annotation = {
|
||||
path: string
|
||||
start_line: number
|
||||
end_line: number
|
||||
start_column?: number
|
||||
end_column?: number
|
||||
annotation_level: 'notice' | 'warning' | 'failure'
|
||||
message: string
|
||||
title?: string
|
||||
raw_details?: string
|
||||
}
|
||||
|
||||
export type ParseTestResult = (files: FileContent[], options: ParseOptions) => Promise<TestResult>
|
||||
|
||||
export type FileContent = {path: string; content: string}
|
||||
|
||||
export interface ParseOptions {
|
||||
annotations: boolean
|
||||
workDir: string
|
||||
trackedFiles: string[]
|
||||
}
|
||||
|
||||
export interface TestResult {
|
||||
testRuns: TestRunResult[]
|
||||
annotations: Annotation[]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue