Improve logging and error handling

This commit is contained in:
Michal Dorner 2021-01-18 22:21:19 +01:00
parent 967dbab3c6
commit 1ab5efa052
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
7 changed files with 174 additions and 33 deletions

View file

@ -1,3 +1,4 @@
import * as core from '@actions/core'
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
import getReport from '../../report/get-report'
@ -85,8 +86,21 @@ export async function parseDartJson(files: FileContent[], options: ParseOptions)
}
function getTestRun(path: string, content: string): TestRun {
const lines = content.split(/\n\r?/g).filter(line => line !== '')
const events = lines.map(str => JSON.parse(str)) as ReportEvent[]
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

View file

@ -1,3 +1,4 @@
import * as core from '@actions/core'
import {ErrorInfo, Outcome, TestMethod, TrxReport} from './dotnet-trx-types'
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
@ -29,7 +30,7 @@ class Test {
readonly error?: ErrorInfo
) {}
get result(): TestExecutionResult {
get result(): TestExecutionResult | undefined {
switch (this.outcome) {
case 'Passed':
return 'success'
@ -46,7 +47,7 @@ export async function parseDotnetTrx(files: FileContent[], options: ParseOptions
const testClasses: TestClass[] = []
for (const file of files) {
const trx = await getTrxReport(file.content)
const trx = await getTrxReport(file)
const tc = getTestClasses(trx)
const tr = getTestRunResult(file.path, trx, tc)
testRuns.push(tr)
@ -66,10 +67,15 @@ export async function parseDotnetTrx(files: FileContent[], options: ParseOptions
}
}
async function getTrxReport(content: string): Promise<TrxReport> {
return (await parseStringPromise(content, {
attrValueProcessors: [parseAttribute]
})) as TrxReport
async function getTrxReport(file: FileContent): Promise<TrxReport> {
core.info(`Parsing content of '${file.path}'`)
try {
return (await parseStringPromise(file.content, {
attrValueProcessors: [parseAttribute]
})) as TrxReport
} catch (e) {
throw new Error(`Invalid XML at ${file.path}\n\n${e}`)
}
}
function getTestRunResult(path: string, trx: TrxReport, testClasses: TestClass[]): TestRunResult {

View file

@ -1,3 +1,4 @@
import * as core from '@actions/core'
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
import {parseStringPromise} from 'xml2js'
@ -20,7 +21,7 @@ export async function parseJestJunit(files: FileContent[], options: ParseOptions
const testRuns: TestRunResult[] = []
for (const file of files) {
const ju = await getJunitReport(file.content)
const ju = await getJunitReport(file)
const tr = getTestRunResult(file.path, ju)
junit.push(ju)
testRuns.push(tr)
@ -39,10 +40,15 @@ export async function parseJestJunit(files: FileContent[], options: ParseOptions
}
}
async function getJunitReport(content: string): Promise<JunitReport> {
return (await parseStringPromise(content, {
attrValueProcessors: [parseAttribute]
})) as JunitReport
async function getJunitReport(file: FileContent): Promise<JunitReport> {
core.info(`Parsing content of '${file.path}'`)
try {
return (await parseStringPromise(file.content, {
attrValueProcessors: [parseAttribute]
})) as JunitReport
} catch (e) {
throw new Error(`Invalid XML at ${file.path}\n\n${e}`)
}
}
function getTestRunResult(path: string, junit: JunitReport): TestRunResult {