Skip to content

Commit 4c6757c

Browse files
committed
test case attrs with basic names
1 parent d4a31b2 commit 4c6757c

File tree

2 files changed

+74
-23
lines changed

2 files changed

+74
-23
lines changed

javascript/src/ExtendedQuery.ts

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
1+
import * as assert from 'node:assert'
2+
23
import {
34
Envelope,
45
getWorstTestStepResult,
6+
Pickle,
57
TestCase,
68
TestCaseFinished,
79
TestCaseStarted,
@@ -15,48 +17,82 @@ import { Query as CucumberQuery } from '@cucumber/query'
1517
export class ExtendedQuery extends CucumberQuery {
1618
private testRunStarted: TestRunStarted
1719
private testRunFinished: TestRunFinished
20+
private testCaseStarted: Array<TestCaseStarted> = []
21+
private readonly pickleById: Map<string, Pickle> = new Map()
1822
private readonly testCaseById: Map<string, TestCase> = new Map()
19-
private readonly testCaseStartedById: Map<string, TestCaseStarted> = new Map()
2023
private readonly testCaseFinishedByTestCaseStartedId: Map<string, TestCaseFinished> = new Map()
21-
private readonly finalAttemptByTestCaseId: Map<
22-
string,
23-
[TestCase, TestCaseStarted, TestCaseFinished]
24-
> = new Map()
2524

2625
update(envelope: Envelope) {
2726
super.update(envelope)
2827

28+
if (envelope.pickle) {
29+
this.pickleById.set(envelope.pickle.id, envelope.pickle)
30+
}
2931
if (envelope.testRunStarted) {
3032
this.testRunStarted = envelope.testRunStarted
3133
}
3234
if (envelope.testCase) {
3335
this.testCaseById.set(envelope.testCase.id, envelope.testCase)
3436
}
3537
if (envelope.testCaseStarted) {
36-
this.testCaseStartedById.set(envelope.testCaseStarted.id, envelope.testCaseStarted)
38+
this.updateTestCaseStarted(envelope.testCaseStarted)
3739
}
3840
if (envelope.testCaseFinished) {
39-
this.testCaseFinishedByTestCaseStartedId.set(
40-
envelope.testCaseFinished.testCaseStartedId,
41-
envelope.testCaseFinished
42-
)
43-
if (!envelope.testCaseFinished.willBeRetried) {
44-
const testCaseStarted = this.testCaseStartedById.get(
45-
envelope.testCaseFinished.testCaseStartedId
46-
)!
47-
const testCase = this.testCaseById.get(testCaseStarted.testCaseId)!
48-
this.finalAttemptByTestCaseId.set(testCase.id, [
49-
testCase,
50-
testCaseStarted,
51-
envelope.testCaseFinished,
52-
])
53-
}
41+
this.updateTestCaseFinished(envelope.testCaseFinished)
5442
}
5543
if (envelope.testRunFinished) {
5644
this.testRunFinished = envelope.testRunFinished
5745
}
5846
}
5947

48+
private updateTestCaseStarted(testCaseStarted: TestCaseStarted) {
49+
// ensure this replaces any previous attempt for the same test case
50+
this.testCaseStarted = [
51+
...this.testCaseStarted.filter(
52+
(existing) => existing.testCaseId !== testCaseStarted.testCaseId
53+
),
54+
testCaseStarted,
55+
]
56+
}
57+
58+
private updateTestCaseFinished(testCaseFinished: TestCaseFinished) {
59+
this.testCaseFinishedByTestCaseStartedId.set(
60+
testCaseFinished.testCaseStartedId,
61+
testCaseFinished
62+
)
63+
}
64+
65+
findPickleBy(testCaseStarted: TestCaseStarted) {
66+
const testCase = this.findTestCaseBy(testCaseStarted)
67+
if (!testCase) {
68+
return undefined
69+
}
70+
return this.pickleById.get(testCase.pickleId)
71+
}
72+
73+
findAllTestCaseStarted(): ReadonlyArray<TestCaseStarted> {
74+
return [...this.testCaseStarted]
75+
}
76+
77+
findTestCaseBy(testCaseStarted: TestCaseStarted) {
78+
return this.testCaseById.get(testCaseStarted.testCaseId)
79+
}
80+
81+
findTestCaseFinishedBy(testCaseStarted: TestCaseStarted) {
82+
return this.testCaseFinishedByTestCaseStartedId.get(testCaseStarted.id)
83+
}
84+
85+
findTestCaseDurationBy(testCaseStarted: TestCaseStarted) {
86+
const testCaseFinished = this.findTestCaseFinishedBy(testCaseStarted)
87+
if (!testCaseFinished) {
88+
return undefined
89+
}
90+
return TimeConversion.millisecondsToDuration(
91+
TimeConversion.timestampToMillisecondsSinceEpoch(testCaseFinished.timestamp) -
92+
TimeConversion.timestampToMillisecondsSinceEpoch(testCaseStarted.timestamp)
93+
)
94+
}
95+
6096
findTestRunDuration() {
6197
if (!this.testRunStarted || !this.testRunFinished) {
6298
return undefined
@@ -77,7 +113,9 @@ export class ExtendedQuery extends CucumberQuery {
77113
[TestStepResultStatus.UNDEFINED]: 0,
78114
[TestStepResultStatus.UNKNOWN]: 0,
79115
}
80-
for (const [testCase] of this.finalAttemptByTestCaseId.values()) {
116+
for (const testCaseStarted of this.testCaseStarted) {
117+
const testCase = this.findTestCaseBy(testCaseStarted)
118+
assert.ok(testCase, 'Expected to find TestCase for TestCaseStarted')
81119
const statusesFromSteps = testCase.testSteps.map((testStep) => {
82120
return getWorstTestStepResult(this.getTestStepResults(testStep.id))
83121
})

javascript/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as assert from 'node:assert'
2+
13
import { Query as GherkinQuery } from '@cucumber/gherkin-utils'
24
import { Envelope, TestStepResultStatus } from '@cucumber/messages'
35
import xmlbuilder from 'xmlbuilder'
@@ -41,6 +43,17 @@ export default {
4143
)
4244
)
4345
builder.att('errors', 0)
46+
47+
for (const testCaseStarted of cucumberQuery.findAllTestCaseStarted()) {
48+
const pickle = cucumberQuery.findPickleBy(testCaseStarted)
49+
assert.ok(pickle, 'Expected to find Pickle for TestCaseStarted')
50+
builder.ele('testcase', {
51+
classname: pickle.uri,
52+
name: pickle.name,
53+
time: durationToSeconds(cucumberQuery.findTestCaseDurationBy(testCaseStarted)),
54+
})
55+
}
56+
4457
write(builder.end({ pretty: true }))
4558
}
4659
})

0 commit comments

Comments
 (0)