Skip to content

Commit

Permalink
remove auto conversion of XML attributes based on value
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Dorner committed Jan 24, 2021
1 parent 1a9ca69 commit 40b5f47
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 55 deletions.
11 changes: 5 additions & 6 deletions src/parsers/dotnet-trx/dotnet-trx-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types
import {parseStringPromise} from 'xml2js'

import {normalizeFilePath} from '../../utils/file-utils'
import {parseAttribute} from '../../utils/xml-utils'
import {Icon, fixEol} from '../../utils/markdown-utils'
import {parseIsoDate, parseNetDuration} from '../../utils/parse-utils'

import {
TestExecutionResult,
Expand Down Expand Up @@ -70,17 +70,15 @@ export async function parseDotnetTrx(files: FileContent[], options: ParseOptions
async function getTrxReport(file: FileContent): Promise<TrxReport> {
core.info(`Parsing content of '${file.path}'`)
try {
return (await parseStringPromise(file.content, {
attrValueProcessors: [parseAttribute]
})) as TrxReport
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 = times.finish.getTime() - times.start.getTime()
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))
Expand Down Expand Up @@ -113,7 +111,8 @@ function getTestClasses(trx: TrxReport): TestClass[] {
}
const output = r.unitTestResult.Output
const error = output?.length > 0 && output[0].ErrorInfo?.length > 0 ? output[0].ErrorInfo[0] : undefined
const test = new Test(r.testMethod.$.name, r.unitTestResult.$.outcome, r.unitTestResult.$.duration, error)
const duration = parseNetDuration(r.unitTestResult.$.duration)
const test = new Test(r.testMethod.$.name, r.unitTestResult.$.outcome, duration, error)
tc.tests.push(test)
}

Expand Down
10 changes: 5 additions & 5 deletions src/parsers/dotnet-trx/dotnet-trx-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export interface TestRun {

export interface Times {
$: {
creation: Date
queuing: Date
start: Date
finish: Date
creation: string
queuing: string
start: string
finish: string
}
}

Expand Down Expand Up @@ -43,7 +43,7 @@ export interface UnitTestResult {
$: {
testId: string
testName: string
duration: number
duration: string
outcome: Outcome
}
Output: Output[]
Expand Down
11 changes: 4 additions & 7 deletions src/parsers/jest-junit/jest-junit-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {parseStringPromise} from 'xml2js'
import {JunitReport, TestCase, TestSuite} from './jest-junit-types'
import {fixEol, Icon} from '../../utils/markdown-utils'
import {normalizeFilePath} from '../../utils/file-utils'
import {parseAttribute} from '../../utils/xml-utils'

import {
TestExecutionResult,
Expand Down Expand Up @@ -43,9 +42,7 @@ export async function parseJestJunit(files: FileContent[], options: ParseOptions
async function getJunitReport(file: FileContent): Promise<JunitReport> {
core.info(`Parsing content of '${file.path}'`)
try {
return (await parseStringPromise(file.content, {
attrValueProcessors: [parseAttribute]
})) as JunitReport
return (await parseStringPromise(file.content)) as JunitReport
} catch (e) {
throw new Error(`Invalid XML at ${file.path}\n\n${e}`)
}
Expand All @@ -54,12 +51,12 @@ async function getJunitReport(file: FileContent): Promise<JunitReport> {
function getTestRunResult(path: string, junit: JunitReport): TestRunResult {
const suites = junit.testsuites.testsuite.map(ts => {
const name = ts.$.name.trim()
const time = ts.$.time * 1000
const time = parseFloat(ts.$.time) * 1000
const sr = new TestSuiteResult(name, getGroups(ts), time)
return sr
})

const time = junit.testsuites.$.time * 1000
const time = parseFloat(junit.testsuites.$.time) * 1000
return new TestRunResult(path, suites, time)
}

Expand All @@ -78,7 +75,7 @@ function getGroups(suite: TestSuite): TestGroupResult[] {
const tests = grp.tests.map(tc => {
const name = tc.$.name.trim()
const result = getTestCaseResult(tc)
const time = tc.$.time * 1000
const time = parseFloat(tc.$.time) * 1000
return new TestCaseResult(name, result, time)
})
return new TestGroupResult(grp.describe, tests)
Expand Down
20 changes: 10 additions & 10 deletions src/parsers/jest-junit/jest-junit-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ export interface JunitReport {
export interface TestSuites {
$: {
name: string
tests: number
failures: number // assertion failed
errors: number // unhandled exception during test execution
time: number
tests: string
failures: string // assertion failed
errors: string // unhandled exception during test execution
time: string
}
testsuite: TestSuite[]
}

export interface TestSuite {
$: {
name: string
tests: number
errors: number
failures: number
skipped: number
time: number
tests: string
errors: string
failures: string
skipped: string
time: string
timestamp?: Date
}
testcase: TestCase[]
Expand All @@ -31,7 +31,7 @@ export interface TestCase {
classname: string
file?: string
name: string
time: number
time: string
}
failure?: string[]
skipped?: string[]
Expand Down
20 changes: 20 additions & 0 deletions src/utils/parse-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function parseNetDuration(str: string): number {
// matches dotnet duration: 00:00:00.0010000
const durationRe = /^(\d\d):(\d\d):(\d\d\.\d+)$/
const durationMatch = str.match(durationRe)
if (durationMatch === null) {
throw new Error(`Invalid format: "${str}" is not NET duration`)
}

const [_, hourStr, minStr, secStr] = durationMatch
return (parseInt(hourStr) * 3600 + parseInt(minStr) * 60 + parseFloat(secStr)) * 1000
}

export function parseIsoDate(str: string): Date {
const isoDateRe = /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)$/
if (str === undefined || !isoDateRe.test(str)) {
throw new Error(`Invalid format: "${str}" is not ISO date`)
}

return new Date(str)
}
27 changes: 0 additions & 27 deletions src/utils/xml-utils.ts

This file was deleted.

0 comments on commit 40b5f47

Please sign in to comment.