Skip to content

Commit

Permalink
refactor: extract parsing java StackTraceElement to allow improving
Browse files Browse the repository at this point in the history
  • Loading branch information
Ats Uiboupin authored and Ats Uiboupin committed Nov 19, 2022
1 parent b41f730 commit e5edb61
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
40 changes: 40 additions & 0 deletions __tests__/java-stack-trace-element-parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {parseStackTraceElement} from '../src/parsers/java-junit/java-stack-trace-element-parser'

describe('parseStackTraceLine tests', () => {
it('empty line is not parsed', async () => {
const line = ''
expect(parseStackTraceElement(line)).toBe(undefined)
})

describe('java class', () => {
it('simple', async () => {
const line =
'at org.apache.pulsar.AddMissingPatchVersionTest.testVersionStrings(AddMissingPatchVersionTest.java:29)'
expect(parseStackTraceElement(line)).toEqual({
tracePath: 'org.apache.pulsar.AddMissingPatchVersionTest.testVersionStrings',
fileName: 'AddMissingPatchVersionTest.java',
lineStr: '29'
})
})

it('inner class', async () => {
const line = 'at com.foo.Main$Inner.run(Main.java:29)'
expect(parseStackTraceElement(line)).toEqual({
tracePath: 'com.foo.Main$Inner.run',
fileName: 'Main.java',
lineStr: '29'
})
})
})

describe('Kotlin class', () => {
it('method name containing whitespaces', async () => {
const line = 'at com.foo.Main.method with whitespaces(Main.kt:18)'
expect(parseStackTraceElement(line)).toEqual({
tracePath: 'com.foo.Main.method with whitespaces',
fileName: 'Main.kt',
lineStr: '18'
})
})
})
})
8 changes: 4 additions & 4 deletions src/parsers/java-junit/java-junit-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ParseOptions, TestParser} from '../../test-parser'
import {parseStringPromise} from 'xml2js'

import {JunitReport, SingleSuiteReport, TestCase, TestSuite} from './java-junit-types'
import {parseStackTraceElement} from './java-stack-trace-element-parser'
import {normalizeFilePath} from '../../utils/path-utils'

import {
Expand Down Expand Up @@ -144,12 +145,11 @@ export class JavaJunitParser implements TestParser {

private exceptionThrowSource(stackTrace: string): {filePath: string; line: number} | undefined {
const lines = stackTrace.split(/\r?\n/)
const re = /^at (.*)\((.*):(\d+)\)$/

for (const str of lines) {
const match = str.match(re)
if (match !== null) {
const [_, tracePath, fileName, lineStr] = match
const stackTraceElement = parseStackTraceElement(str)
if (stackTraceElement) {
const {tracePath, fileName, lineStr} = stackTraceElement
const filePath = this.getFilePath(tracePath, fileName)
if (filePath !== undefined) {
const line = parseInt(lineStr)
Expand Down
22 changes: 22 additions & 0 deletions src/parsers/java-junit/java-stack-trace-element-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface StackTraceElement {
tracePath: string
fileName: string
lineStr: string
}

// simple format:
// at <FULLY_QUALIFIED_METHOD_NAME>(<FILE_NAME>:<LINE_NUMBER>)
const re = /^at (.*)\((.*):(\d+)\)$/

export function parseStackTraceElement(stackTraceLine: string): StackTraceElement | undefined {
const match = stackTraceLine.match(re)
if (match !== null) {
const [_, tracePath, fileName, lineStr] = match
return {
tracePath,
fileName,
lineStr
}
}
return undefined
}

0 comments on commit e5edb61

Please sign in to comment.