-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
configuration_builder.ts
227 lines (210 loc) · 6.99 KB
/
configuration_builder.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import ArgvParser, {
IParsedArgvFormatOptions,
IParsedArgvOptions,
} from './argv_parser'
import fs from 'mz/fs'
import path from 'path'
import OptionSplitter from './option_splitter'
import bluebird from 'bluebird'
import glob from 'glob'
import { promisify } from 'util'
import { IPickleFilterOptions } from '../pickle_filter'
import { IRuntimeOptions } from '../runtime'
import { valueOrDefault } from '../value_checker'
export interface IConfigurationFormat {
outputTo: string
type: string
}
export interface IConfiguration {
esm: boolean
featureDefaultLanguage: string
featurePaths: string[]
formats: IConfigurationFormat[]
formatOptions: IParsedArgvFormatOptions
publishing: boolean
listI18nKeywordsFor: string
listI18nLanguages: boolean
order: string
parallel: number
pickleFilterOptions: IPickleFilterOptions
predictableIds: boolean
profiles: string[]
runtimeOptions: IRuntimeOptions
shouldExitImmediately: boolean
supportCodePaths: string[]
supportCodeRequiredModules: string[]
suppressPublishAdvertisement: boolean
}
export interface INewConfigurationBuilderOptions {
argv: string[]
cwd: string
}
const DEFAULT_CUCUMBER_PUBLISH_URL = 'https://messages.cucumber.io/api/reports'
export default class ConfigurationBuilder {
static async build(
options: INewConfigurationBuilderOptions
): Promise<IConfiguration> {
const builder = new ConfigurationBuilder(options)
return await builder.build()
}
private readonly cwd: string
private readonly args: string[]
private readonly options: IParsedArgvOptions
constructor({ argv, cwd }: INewConfigurationBuilderOptions) {
this.cwd = cwd
const parsedArgv = ArgvParser.parse(argv)
this.args = parsedArgv.args
this.options = parsedArgv.options
}
async build(): Promise<IConfiguration> {
const listI18nKeywordsFor = this.options.i18nKeywords
const listI18nLanguages = this.options.i18nLanguages
const unexpandedFeaturePaths = await this.getUnexpandedFeaturePaths()
let featurePaths: string[] = []
let supportCodePaths: string[] = []
if (listI18nKeywordsFor === '' && !listI18nLanguages) {
featurePaths = await this.expandFeaturePaths(unexpandedFeaturePaths)
let unexpandedSupportCodePaths = this.options.require
if (unexpandedSupportCodePaths.length === 0) {
unexpandedSupportCodePaths = this.getFeatureDirectoryPaths(featurePaths)
}
supportCodePaths = await this.expandPaths(
unexpandedSupportCodePaths,
this.options.esm ? '.@(js|mjs)' : '.js'
)
}
return {
esm: this.options.esm,
featureDefaultLanguage: this.options.language,
featurePaths,
formats: this.getFormats(),
formatOptions: this.options.formatOptions,
publishing: this.isPublishing(),
listI18nKeywordsFor,
listI18nLanguages,
order: this.options.order,
parallel: this.options.parallel,
pickleFilterOptions: {
cwd: this.cwd,
featurePaths: unexpandedFeaturePaths,
names: this.options.name,
tagExpression: this.options.tags,
},
predictableIds: this.options.predictableIds,
profiles: this.options.profile,
runtimeOptions: {
dryRun: this.options.dryRun,
predictableIds: this.options.predictableIds,
failFast: this.options.failFast,
filterStacktraces: !this.options.backtrace,
retry: this.options.retry,
retryTagFilter: this.options.retryTagFilter,
strict: this.options.strict,
worldParameters: this.options.worldParameters,
},
shouldExitImmediately: this.options.exit,
supportCodePaths,
supportCodeRequiredModules: this.options.requireModule,
suppressPublishAdvertisement: this.isPublishAdvertisementSuppressed(),
}
}
async expandPaths(
unexpandedPaths: string[],
defaultExtension: string
): Promise<string[]> {
const expandedPaths = await bluebird.map(
unexpandedPaths,
async (unexpandedPath) => {
const matches = await promisify(glob)(unexpandedPath, {
absolute: true,
cwd: this.cwd,
})
const expanded = await bluebird.map(matches, async (match) => {
if (path.extname(match) === '') {
return await promisify(glob)(`${match}/**/*${defaultExtension}`)
}
return [match]
})
return expanded.flat()
}
)
return expandedPaths.flat().map((x) => path.normalize(x))
}
async expandFeaturePaths(featurePaths: string[]): Promise<string[]> {
featurePaths = featurePaths.map((p) => p.replace(/(:\d+)*$/g, '')) // Strip line numbers
featurePaths = [...new Set(featurePaths)] // Deduplicate the feature files
return this.expandPaths(featurePaths, '.feature')
}
getFeatureDirectoryPaths(featurePaths: string[]): string[] {
const featureDirs = featurePaths.map((featurePath) => {
let featureDir = path.dirname(featurePath)
let childDir: string
let parentDir = featureDir
while (childDir !== parentDir) {
childDir = parentDir
parentDir = path.dirname(childDir)
if (path.basename(parentDir) === 'features') {
featureDir = parentDir
break
}
}
return path.relative(this.cwd, featureDir)
})
return [...new Set(featureDirs)]
}
isPublishing(): boolean {
return (
this.options.publish ||
this.isTruthyString(process.env.CUCUMBER_PUBLISH_ENABLED) ||
process.env.CUCUMBER_PUBLISH_TOKEN !== undefined
)
}
isPublishAdvertisementSuppressed(): boolean {
return (
this.options.publishQuiet ||
this.isTruthyString(process.env.CUCUMBER_PUBLISH_QUIET)
)
}
getFormats(): IConfigurationFormat[] {
const mapping: { [key: string]: string } = { '': 'progress' }
this.options.format.forEach((format) => {
const [type, outputTo] = OptionSplitter.split(format)
mapping[outputTo] = type
})
if (this.isPublishing()) {
const publishUrl = valueOrDefault(
process.env.CUCUMBER_PUBLISH_URL,
DEFAULT_CUCUMBER_PUBLISH_URL
)
mapping[publishUrl] = 'message'
}
return Object.keys(mapping).map((outputTo) => ({
outputTo,
type: mapping[outputTo],
}))
}
isTruthyString(s: string | undefined): boolean {
if (s === undefined) {
return false
}
return s.match(/^(false|no|0)$/i) === null
}
async getUnexpandedFeaturePaths(): Promise<string[]> {
if (this.args.length > 0) {
const nestedFeaturePaths = await bluebird.map(this.args, async (arg) => {
const filename = path.basename(arg)
if (filename[0] === '@') {
const filePath = path.join(this.cwd, arg)
const content = await fs.readFile(filePath, 'utf8')
return content.split('\n').map((x) => x.trim())
}
return [arg]
})
const featurePaths = nestedFeaturePaths.flat()
if (featurePaths.length > 0) {
return featurePaths.filter((x) => x !== '')
}
}
return ['features/**/*.{feature,feature.md}']
}
}