-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
helpers.ts
199 lines (189 loc) · 5.57 KB
/
helpers.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { EventEmitter } from 'node:events'
import os from 'node:os'
import shuffle from 'knuth-shuffle-seeded'
import * as messages from '@cucumber/messages'
import { IdGenerator } from '@cucumber/messages'
import detectCiEnvironment from '@cucumber/ci-environment'
import { SupportCodeLibrary } from '../support_code_library_builder/types'
import TestCaseHookDefinition from '../models/test_case_hook_definition'
import TestRunHookDefinition from '../models/test_run_hook_definition'
import { version } from '../version'
import { ILogger } from '../logger'
import { ILineAndUri } from '../types'
import { IPickleOrder } from '../filter'
// Orders the pickleIds in place - morphs input
export function orderPickles<T = string>(
pickleIds: T[],
order: IPickleOrder,
logger: ILogger
): void {
const [type, seed] = splitOrder(order)
switch (type) {
case 'defined':
break
case 'random':
if (seed === '') {
const newSeed = Math.floor(Math.random() * 1000 * 1000).toString()
logger.warn(`Random order using seed: ${newSeed}`)
shuffle(pickleIds, newSeed)
} else {
shuffle(pickleIds, seed)
}
break
default:
throw new Error(
'Unrecognized order type. Should be `defined` or `random`'
)
}
}
function splitOrder(order: string) {
if (!order.includes(':')) {
return [order, '']
}
return order.split(':')
}
export async function emitMetaMessage(
eventBroadcaster: EventEmitter,
env: NodeJS.ProcessEnv
): Promise<void> {
const meta: messages.Meta = {
protocolVersion: messages.version,
implementation: {
version,
name: 'cucumber-js',
},
cpu: {
name: os.arch(),
},
os: {
name: os.platform(),
version: os.release(),
},
runtime: {
name: 'node.js',
version: process.versions.node,
},
ci: detectCiEnvironment(env),
}
eventBroadcaster.emit('envelope', {
meta,
})
}
const makeSourceReference = (source: ILineAndUri) => ({
uri: source.uri,
location: {
line: source.line,
},
})
function emitParameterTypes(
supportCodeLibrary: SupportCodeLibrary,
eventBroadcaster: EventEmitter,
newId: IdGenerator.NewId
): void {
for (const parameterType of supportCodeLibrary.parameterTypeRegistry
.parameterTypes) {
if (parameterType.builtin) {
continue
}
const source =
supportCodeLibrary.parameterTypeRegistry.lookupSource(parameterType)
const envelope: messages.Envelope = {
parameterType: {
id: newId(),
name: parameterType.name,
preferForRegularExpressionMatch: parameterType.preferForRegexpMatch,
regularExpressions: parameterType.regexpStrings,
useForSnippets: parameterType.useForSnippets,
sourceReference: makeSourceReference(source),
},
}
eventBroadcaster.emit('envelope', envelope)
}
}
function emitUndefinedParameterTypes(
supportCodeLibrary: SupportCodeLibrary,
eventBroadcaster: EventEmitter
): void {
for (const undefinedParameterType of supportCodeLibrary.undefinedParameterTypes) {
const envelope: messages.Envelope = {
undefinedParameterType,
}
eventBroadcaster.emit('envelope', envelope)
}
}
function emitStepDefinitions(
supportCodeLibrary: SupportCodeLibrary,
eventBroadcaster: EventEmitter
): void {
supportCodeLibrary.stepDefinitions.forEach((stepDefinition) => {
const envelope: messages.Envelope = {
stepDefinition: {
id: stepDefinition.id,
pattern: {
source: stepDefinition.pattern.toString(),
type:
typeof stepDefinition.pattern === 'string'
? messages.StepDefinitionPatternType.CUCUMBER_EXPRESSION
: messages.StepDefinitionPatternType.REGULAR_EXPRESSION,
},
sourceReference: makeSourceReference(stepDefinition),
},
}
eventBroadcaster.emit('envelope', envelope)
})
}
function emitTestCaseHooks(
supportCodeLibrary: SupportCodeLibrary,
eventBroadcaster: EventEmitter
): void {
;[]
.concat(
supportCodeLibrary.beforeTestCaseHookDefinitions,
supportCodeLibrary.afterTestCaseHookDefinitions
)
.forEach((testCaseHookDefinition: TestCaseHookDefinition) => {
const envelope: messages.Envelope = {
hook: {
id: testCaseHookDefinition.id,
name: testCaseHookDefinition.name,
tagExpression: testCaseHookDefinition.tagExpression,
sourceReference: makeSourceReference(testCaseHookDefinition),
},
}
eventBroadcaster.emit('envelope', envelope)
})
}
function emitTestRunHooks(
supportCodeLibrary: SupportCodeLibrary,
eventBroadcaster: EventEmitter
): void {
;[]
.concat(
supportCodeLibrary.beforeTestRunHookDefinitions,
supportCodeLibrary.afterTestRunHookDefinitions
)
.forEach((testRunHookDefinition: TestRunHookDefinition) => {
const envelope: messages.Envelope = {
hook: {
id: testRunHookDefinition.id,
sourceReference: makeSourceReference(testRunHookDefinition),
},
}
eventBroadcaster.emit('envelope', envelope)
})
}
export function emitSupportCodeMessages({
eventBroadcaster,
supportCodeLibrary,
newId,
}: {
eventBroadcaster: EventEmitter
supportCodeLibrary: SupportCodeLibrary
newId: IdGenerator.NewId
}): void {
emitParameterTypes(supportCodeLibrary, eventBroadcaster, newId)
emitUndefinedParameterTypes(supportCodeLibrary, eventBroadcaster)
emitStepDefinitions(supportCodeLibrary, eventBroadcaster)
emitTestCaseHooks(supportCodeLibrary, eventBroadcaster)
emitTestRunHooks(supportCodeLibrary, eventBroadcaster)
}