Skip to content

Commit

Permalink
Fix dart/flutter stack trace parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Dorner committed Feb 1, 2021
1 parent 2834fd0 commit effa386
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
6 changes: 3 additions & 3 deletions __tests__/__snapshots__/dart-json.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ test\\\\main_test.dart 13:9 main.<fn>.<fn>.<fn>
"details": "package:darttest/main.dart 2:3 throwError
test\\\\main_test.dart 17:9 main.<fn>.<fn>.<fn>
",
"line": 2,
"line": 17,
"message": "Exception: Some error",
"path": "lib/main.dart",
"path": "test/main_test.dart",
},
"name": "Test 1 Test 1.1 Exception in target unit",
"result": "failed",
Expand Down Expand Up @@ -159,7 +159,7 @@ When the exception was thrown, this was the stack:
The test description was:
pass updateShouldNotify
════════════════════════════════════════════════════════════════════════════════════════════════════",
"line": 94,
"line": 112,
"message": "Test failed. See exception logs above.
The test description was: pass updateShouldNotify",
"path": "test/value_listenable_provider_test.dart",
Expand Down
4 changes: 2 additions & 2 deletions __tests__/dart-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('dart-json tests', () => {
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})

const parser = new DartJsonParser(opts)
const parser = new DartJsonParser(opts, 'dart')
const result = await parser.parse(filePath, fileContent)
expect(result).toMatchSnapshot()

Expand All @@ -42,7 +42,7 @@ describe('dart-json tests', () => {
workDir: '/__w/provider/provider/'
}

const parser = new DartJsonParser(opts)
const parser = new DartJsonParser(opts, 'flutter')
const result = await parser.parse(filePath, fileContent)
expect(result).toMatchSnapshot()

Expand Down
28 changes: 18 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ async function main(): Promise<void> {
function getParser(reporter: string, options: ParseOptions): TestParser {
switch (reporter) {
case 'dart-json':
return new DartJsonParser(options)
return new DartJsonParser(options, 'dart')
case 'dotnet-trx':
return new DotnetTrxParser(options)
case 'flutter-json':
return new DartJsonParser(options)
return new DartJsonParser(options, 'flutter')
case 'jest-junit':
return new JestJunitParser(options)
default:
Expand Down
19 changes: 12 additions & 7 deletions src/parsers/dart-json/dart-json-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class TestCase {
}

export class DartJsonParser implements TestParser {
constructor(readonly options: ParseOptions) {}
constructor(readonly options: ParseOptions, readonly sdk: 'dart' | 'flutter') {}

async parse(path: string, content: string): Promise<TestRunResult> {
const tr = this.getTestRun(path, content)
Expand Down Expand Up @@ -162,7 +162,7 @@ export class DartJsonParser implements TestParser {
.map(p => p.message)
.join('\n')
const details = [print, stackTrace].filter(str => str !== '').join('\n')
const src = this.exceptionThrowSource(stackTrace, trackedFiles)
const src = this.exceptionThrowSource(details, trackedFiles)
let path
let line

Expand All @@ -186,17 +186,18 @@ export class DartJsonParser implements TestParser {
}

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/'))
const lines = ex.split(/\r?\n/g)

// regexp to extract file path and line number from stack trace
const re = /^(.*)\s+(\d+):\d+\s+/
const dartRe = /^(?!package:)(.*)\s+(\d+):\d+\s+/
const flutterRe = /^#\d+\s+.*\((?!package:)(.*):(\d+):\d+\)$/
const re = this.sdk === 'dart' ? dartRe : flutterRe

for (const str of lines) {
const match = str.match(re)
if (match !== null) {
const [_, pathStr, lineStr] = match
const path = normalizeFilePath(pathStr)
const path = normalizeFilePath(this.getRelativePath(pathStr))
if (trackedFiles.includes(path)) {
const line = parseInt(lineStr)
return {path, line}
Expand All @@ -207,6 +208,10 @@ export class DartJsonParser implements TestParser {

private getRelativePath(path: string): string {
const {workDir} = this.options
const prefix = 'file://'
if (path.startsWith(prefix)) {
path = path.substr(prefix.length)
}
if (path.startsWith(workDir)) {
path = path.substr(workDir.length)
}
Expand Down

0 comments on commit effa386

Please sign in to comment.