Skip to content

Commit

Permalink
Fix dart-json not stripping cwd from suite paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Dorner committed Feb 1, 2021
1 parent 9c4a2c5 commit 2365963
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 49 deletions.
8 changes: 4 additions & 4 deletions __tests__/__outputs__/dart-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
**6** tests were completed in **3.760s** with **1** passed, **4** failed and **1** skipped.
|Test suite|Passed|Failed|Skipped|Time|
|:---|---:|---:|---:|---:|
|[test\main_test.dart](#r0s0)|1✔️|3❌||74ms|
|[test\second_test.dart](#r0s1)||1❌|1✖️|51ms|
### <a id="user-content-r0s0" href="#r0s0">test\main_test.dart</a> ❌
|[test/main_test.dart](#r0s0)|1✔️|3❌||74ms|
|[test/second_test.dart](#r0s1)||1❌|1✖️|51ms|
### <a id="user-content-r0s0" href="#r0s0">test/main_test.dart</a> ❌
**4** tests were completed in **74ms** with **1** passed, **3** failed and **0** skipped.

**Test 1**
Expand All @@ -23,7 +23,7 @@
|Result|Test|Time|
|:---:|:---|---:|
||Test 2 Exception in test|12ms|
### <a id="user-content-r0s1" href="#r0s1">test\second_test.dart</a> ❌
### <a id="user-content-r0s1" href="#r0s1">test/second_test.dart</a> ❌
**2** tests were completed in **51ms** with **0** passed, **1** failed and **1** skipped.

|Result|Test|Time|
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/dart-json.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test\\\\main_test.dart 17:9 main.<fn>.<fn>.<fn>
],
},
],
"name": "test\\\\main_test.dart",
"name": "test/main_test.dart",
"totalTime": undefined,
},
TestSuiteResult {
Expand Down Expand Up @@ -97,7 +97,7 @@ test\\\\main_test.dart 17:9 main.<fn>.<fn>.<fn>
],
},
],
"name": "test\\\\second_test.dart",
"name": "test/second_test.dart",
"totalTime": undefined,
},
],
Expand Down
38 changes: 17 additions & 21 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.

38 changes: 17 additions & 21 deletions src/parsers/dart-json/dart-json-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class DartJsonParser implements TestParser {
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}`)
throw new Error(`Invalid JSON at ${path}:${i + 1}${col}\n\n${e}`)
}
})
.filter(evt => evt != null) as ReportEvent[]
Expand Down Expand Up @@ -123,7 +123,7 @@ export class DartJsonParser implements TestParser {

private getTestRunResult(tr: TestRun): TestRunResult {
const suites = tr.suites.map(s => {
return new TestSuiteResult(s.suite.path, this.getGroups(s))
return new TestSuiteResult(this.getRelativePath(s.suite.path), this.getGroups(s))
})

return new TestRunResult(tr.path, suites, tr.time)
Expand All @@ -135,36 +135,35 @@ export class DartJsonParser implements TestParser {

return groups.map(group => {
group.tests.sort((a, b) => (a.testStart.test.line ?? 0) - (b.testStart.test.line ?? 0))
const tests = group.tests.map(t => this.getTest(t))
const tests = group.tests.map(tc => {
const error = this.getError(suite, tc)
return new TestCaseResult(tc.testStart.test.name, tc.result, tc.time, error)
})
return new TestGroupResult(group.group.name, tests)
})
}

private getTest(tc: TestCase): TestCaseResult {
const error = this.getError(tc)
return new TestCaseResult(tc.testStart.test.name, tc.result, tc.time, error)
}

private getError(test: TestCase): TestCaseError | undefined {
private getError(testSuite: TestSuite, test: TestCase): TestCaseError | undefined {
if (!this.options.parseErrors || !test.error) {
return undefined
}

const {workDir, trackedFiles} = this.options
const {trackedFiles} = this.options
const message = test.error?.error ?? ''
const stackTrace = test.error?.stackTrace ?? ''
const src = this.exceptionThrowSource(stackTrace, trackedFiles)
let path
let line

if (src !== undefined) {
;(path = src.path), (line = src.line)
path = src.path
line = src.line
} else {
const testStartPath = this.getRelativePathFromUrl(test.testStart.test.url ?? '', workDir)
const testStartPath = this.getRelativePath(testSuite.suite.path)
if (trackedFiles.includes(testStartPath)) {
path = testStartPath
line = test.testStart.test.root_line ?? test.testStart.test.line ?? undefined
}
line = test.testStart.test.line ?? undefined
}

return {
Expand Down Expand Up @@ -195,14 +194,11 @@ export class DartJsonParser implements TestParser {
}
}

private getRelativePathFromUrl(file: string, workDir: string): string {
const prefix = 'file:///'
if (file.startsWith(prefix)) {
file = file.substr(prefix.length)
}
if (file.startsWith(workDir)) {
file = file.substr(workDir.length)
private getRelativePath(path: string): string {
const {workDir} = this.options
if (path.startsWith(workDir)) {
path = path.substr(workDir.length)
}
return file
return normalizeFilePath(path)
}
}

0 comments on commit 2365963

Please sign in to comment.