-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathevent_data_collector.ts
133 lines (120 loc) · 4.87 KB
/
event_data_collector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { EventEmitter } from 'node:events'
import * as messages from '@cucumber/messages'
import { doesHaveValue, doesNotHaveValue } from '../../value_checker'
interface ITestCaseAttemptData {
attempt: number
willBeRetried: boolean
testCaseId: string
stepAttachments: Record<string, messages.Attachment[]>
stepResults: Record<string, messages.TestStepResult>
worstTestStepResult: messages.TestStepResult
}
export interface ITestCaseAttempt {
attempt: number
willBeRetried: boolean
gherkinDocument: messages.GherkinDocument
pickle: messages.Pickle
stepAttachments: Record<string, messages.Attachment[]>
stepResults: Record<string, messages.TestStepResult>
testCase: messages.TestCase
worstTestStepResult: messages.TestStepResult
}
export default class EventDataCollector {
private gherkinDocumentMap: Record<string, messages.GherkinDocument> = {}
private pickleMap: Record<string, messages.Pickle> = {}
private testCaseMap: Record<string, messages.TestCase> = {}
private testCaseAttemptDataMap: Record<string, ITestCaseAttemptData> = {}
readonly undefinedParameterTypes: messages.UndefinedParameterType[] = []
constructor(eventBroadcaster: EventEmitter) {
eventBroadcaster.on('envelope', this.parseEnvelope.bind(this))
}
getGherkinDocument(uri: string): messages.GherkinDocument {
return this.gherkinDocumentMap[uri]
}
getPickle(pickleId: string): messages.Pickle {
return this.pickleMap[pickleId]
}
getTestCaseAttempts(): ITestCaseAttempt[] {
return Object.keys(this.testCaseAttemptDataMap).map((testCaseStartedId) => {
return this.getTestCaseAttempt(testCaseStartedId)
})
}
getTestCaseAttempt(testCaseStartedId: string): ITestCaseAttempt {
const testCaseAttemptData = this.testCaseAttemptDataMap[testCaseStartedId]
const testCase = this.testCaseMap[testCaseAttemptData.testCaseId]
const pickle = this.pickleMap[testCase.pickleId]
return {
gherkinDocument: this.gherkinDocumentMap[pickle.uri],
pickle,
testCase,
attempt: testCaseAttemptData.attempt,
willBeRetried: testCaseAttemptData.willBeRetried,
stepAttachments: testCaseAttemptData.stepAttachments,
stepResults: testCaseAttemptData.stepResults,
worstTestStepResult: testCaseAttemptData.worstTestStepResult,
}
}
parseEnvelope(envelope: messages.Envelope): void {
if (doesHaveValue(envelope.gherkinDocument)) {
this.gherkinDocumentMap[envelope.gherkinDocument.uri] =
envelope.gherkinDocument
} else if (doesHaveValue(envelope.pickle)) {
this.pickleMap[envelope.pickle.id] = envelope.pickle
} else if (doesHaveValue(envelope.undefinedParameterType)) {
this.undefinedParameterTypes.push(envelope.undefinedParameterType)
} else if (doesHaveValue(envelope.testCase)) {
this.testCaseMap[envelope.testCase.id] = envelope.testCase
} else if (doesHaveValue(envelope.testCaseStarted)) {
this.initTestCaseAttempt(envelope.testCaseStarted)
} else if (doesHaveValue(envelope.attachment)) {
this.storeAttachment(envelope.attachment)
} else if (doesHaveValue(envelope.testStepFinished)) {
this.storeTestStepResult(envelope.testStepFinished)
} else if (doesHaveValue(envelope.testCaseFinished)) {
this.storeTestCaseResult(envelope.testCaseFinished)
}
}
private initTestCaseAttempt(testCaseStarted: messages.TestCaseStarted): void {
this.testCaseAttemptDataMap[testCaseStarted.id] = {
attempt: testCaseStarted.attempt,
willBeRetried: false,
testCaseId: testCaseStarted.testCaseId,
stepAttachments: {},
stepResults: {},
worstTestStepResult: {
duration: { seconds: 0, nanos: 0 },
status: messages.TestStepResultStatus.UNKNOWN,
},
}
}
storeAttachment(attachment: messages.Attachment): void {
const { testCaseStartedId, testStepId } = attachment
// TODO: we shouldn't have to check if these properties have values - they are non-nullable
if (doesHaveValue(testCaseStartedId) && doesHaveValue(testStepId)) {
const { stepAttachments } = this.testCaseAttemptDataMap[testCaseStartedId]
if (doesNotHaveValue(stepAttachments[testStepId])) {
stepAttachments[testStepId] = []
}
stepAttachments[testStepId].push(attachment)
}
}
storeTestStepResult({
testCaseStartedId,
testStepId,
testStepResult,
}: messages.TestStepFinished): void {
this.testCaseAttemptDataMap[testCaseStartedId].stepResults[testStepId] =
testStepResult
}
storeTestCaseResult({
testCaseStartedId,
willBeRetried,
}: messages.TestCaseFinished): void {
const stepResults = Object.values(
this.testCaseAttemptDataMap[testCaseStartedId].stepResults
)
this.testCaseAttemptDataMap[testCaseStartedId].worstTestStepResult =
messages.getWorstTestStepResult(stepResults)
this.testCaseAttemptDataMap[testCaseStartedId].willBeRetried = willBeRetried
}
}