Skip to content

Commit

Permalink
Add annotations support to dotnet-trx
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Dorner committed Jan 14, 2021
1 parent 6f32e41 commit c4b64b0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
31 changes: 30 additions & 1 deletion __tests__/__snapshots__/dotnet-trx.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,36 @@

exports[`dotnet-trx tests matches report snapshot 1`] = `
Object {
"annotations": Array [],
"annotations": Array [
Object {
"annotation_level": "failure",
"end_line": 9,
"message": "System.DivideByZeroException : Attempted to divide by zero.",
"path": "DotnetTests.Unit/Calculator.cs",
"start_line": 9,
"title": "[DotnetTests.XUnitTests.CalculatorTests] Exception_In_TargetTest",
},
Object {
"annotation_level": "failure",
"end_line": 39,
"message": "System.Exception : Test",
"path": "DotnetTests.XUnitTests/CalculatorTests.cs",
"start_line": 39,
"title": "[DotnetTests.XUnitTests.CalculatorTests] Exception_In_Test",
},
Object {
"annotation_level": "failure",
"end_line": 27,
"message": "Assert.Equal() Failure
Expected: 3
Actual: 2",
"path": "DotnetTests.XUnitTests/CalculatorTests.cs",
"start_line": 27,
"title": "[DotnetTests.XUnitTests.CalculatorTests] Failing_Test",
},
],
"summary": "**7** tests were completed in **1.061s** with **3** passed, **1** skipped and **3** failed.
| Result | Suite | Tests | Time | Passed ✔️ | Failed| Skipped ✖️ |
| :---: | :--- | ---: | ---: | ---: | ---: | ---: |
Expand Down
52 changes: 47 additions & 5 deletions src/parsers/dotnet-trx/dotnet-trx-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ErrorInfo, Outcome, TestMethod, TrxReport} from './dotnet-trx-types'
import {Annotation, ParseOptions, TestResult} from '../parser-types'
import {parseStringPromise} from 'xml2js'

import {normalizeFilePath} from '../../utils/file-utils'
import {parseAttribute} from '../../utils/xml-utils'
import {Icon} from '../../utils/markdown-utils'

Expand Down Expand Up @@ -55,9 +56,7 @@ export async function parseDotnetTrx(content: string, options: ParseOptions): Pr
output: {
title: `${options.name.trim()} ${icon}`,
summary: getReport(testRun),
annotations: options.annotations
? getAnnotations(/*testClasses, options.workDir, options.trackedFiles*/)
: undefined
annotations: options.annotations ? getAnnotations(testClasses, options.workDir, options.trackedFiles) : undefined
}
}
}
Expand Down Expand Up @@ -110,6 +109,49 @@ function getTestClasses(trx: TrxReport): TestClass[] {
return result
}

function getAnnotations(/*testClasses: TestClass[], workDir: string, trackedFiles: string[]*/): Annotation[] {
return []
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
}
annotations.push({
annotation_level: 'failure',
start_line: src.line,
end_line: src.line,
path: src.file,
message: 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
}

0 comments on commit c4b64b0

Please sign in to comment.