-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config.js
343 lines (285 loc) · 10.6 KB
/
config.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var path = require('path')
var logger = require('./logger')
var log = logger.create('config')
var helper = require('./helper')
var constant = require('./constants')
var COFFEE_SCRIPT_AVAILABLE = false
var LIVE_SCRIPT_AVAILABLE = false
// Coffee is required here to enable config files written in coffee-script.
// It's not directly used in this file.
try {
require('coffee-script').register()
COFFEE_SCRIPT_AVAILABLE = true
} catch (e) {}
// LiveScript is required here to enable config files written in LiveScript.
// It's not directly used in this file.
try {
require('LiveScript')
LIVE_SCRIPT_AVAILABLE = true
} catch (e) {}
var Pattern = function (pattern, served, included, watched, nocache) {
this.pattern = pattern
this.served = helper.isDefined(served) ? served : true
this.included = helper.isDefined(included) ? included : true
this.watched = helper.isDefined(watched) ? watched : true
this.nocache = helper.isDefined(nocache) ? nocache : false
this.weight = helper.mmPatternWeight(pattern)
}
Pattern.prototype.compare = function (other) {
return helper.mmComparePatternWeights(this.weight, other.weight)
}
var UrlPattern = function (url) {
Pattern.call(this, url, false, true, false, false)
}
var createPatternObject = function (pattern) {
if (pattern && helper.isString(pattern)) {
return helper.isUrlAbsolute(pattern) ? new UrlPattern(pattern) : new Pattern(pattern)
}
if (helper.isObject(pattern)) {
if (pattern.pattern && helper.isString(pattern.pattern)) {
return helper.isUrlAbsolute(pattern.pattern)
? new UrlPattern(pattern.pattern)
: new Pattern(
pattern.pattern,
pattern.served,
pattern.included,
pattern.watched,
pattern.nocache)
}
log.warn('Invalid pattern %s!\n\tObject is missing "pattern" property.', pattern)
return new Pattern(null, false, false, false, false)
}
log.warn('Invalid pattern %s!\n\tExpected string or object with "pattern" property.', pattern)
return new Pattern(null, false, false, false, false)
}
var normalizeUrlRoot = function (urlRoot) {
var normalizedUrlRoot = urlRoot
if (normalizedUrlRoot.charAt(0) !== '/') {
normalizedUrlRoot = '/' + normalizedUrlRoot
}
if (normalizedUrlRoot.charAt(normalizedUrlRoot.length - 1) !== '/') {
normalizedUrlRoot = normalizedUrlRoot + '/'
}
if (normalizedUrlRoot !== urlRoot) {
log.warn('urlRoot normalized to "%s"', normalizedUrlRoot)
}
return normalizedUrlRoot
}
var normalizeConfig = function (config, configFilePath) {
var basePathResolve = function (relativePath) {
if (helper.isUrlAbsolute(relativePath)) {
return relativePath
}
if (!helper.isDefined(config.basePath) || !helper.isDefined(relativePath)) {
return ''
}
return path.resolve(config.basePath, relativePath)
}
var createPatternMapper = function (resolve) {
return function (objectPattern) {
objectPattern.pattern = resolve(objectPattern.pattern)
return objectPattern
}
}
if (helper.isString(configFilePath)) {
// resolve basePath
config.basePath = path.resolve(path.dirname(configFilePath), config.basePath)
// always ignore the config file itself
config.exclude.push(configFilePath)
} else {
config.basePath = path.resolve(config.basePath || '.')
}
config.files = config.files.map(createPatternObject).map(createPatternMapper(basePathResolve))
config.exclude = config.exclude.map(basePathResolve)
config.customContextFile = config.customContextFile && basePathResolve(config.customContextFile)
config.customDebugFile = config.customDebugFile && basePathResolve(config.customDebugFile)
// normalize paths on windows
config.basePath = helper.normalizeWinPath(config.basePath)
config.files = config.files.map(createPatternMapper(helper.normalizeWinPath))
config.exclude = config.exclude.map(helper.normalizeWinPath)
config.customContextFile = helper.normalizeWinPath(config.customContextFile)
config.customDebugFile = helper.normalizeWinPath(config.customDebugFile)
// normalize urlRoot
config.urlRoot = normalizeUrlRoot(config.urlRoot)
// force protocol to end with ':'
config.protocol = (config.protocol || 'http').split(':')[0] + ':'
if (config.protocol.match(/https?:/) === null) {
log.warn('"%s" is not a supported protocol, defaulting to "http:"',
config.protocol)
config.protocol = 'http:'
}
if (config.proxies && config.proxies.hasOwnProperty(config.urlRoot)) {
log.warn('"%s" is proxied, you should probably change urlRoot to avoid conflicts',
config.urlRoot)
}
if (config.singleRun && config.autoWatch) {
log.debug('autoWatch set to false, because of singleRun')
config.autoWatch = false
}
if (helper.isString(config.reporters)) {
config.reporters = config.reporters.split(',')
}
if (config.client && config.client.args && !Array.isArray(config.client.args)) {
throw new Error('Invalid configuration: client.args must be an array of strings')
}
if (config.browsers && Array.isArray(config.browsers) === false) {
throw new TypeError('Invalid configuration: browsers option must be an array')
}
var defaultClient = config.defaultClient || {}
Object.keys(defaultClient).forEach(function (key) {
var option = config.client[key]
config.client[key] = helper.isDefined(option) ? option : defaultClient[key]
})
// normalize preprocessors
var preprocessors = config.preprocessors || {}
var normalizedPreprocessors = config.preprocessors = Object.create(null)
Object.keys(preprocessors).forEach(function (pattern) {
var normalizedPattern = helper.normalizeWinPath(basePathResolve(pattern))
normalizedPreprocessors[normalizedPattern] = helper.isString(preprocessors[pattern])
? [preprocessors[pattern]] : preprocessors[pattern]
})
// define custom launchers/preprocessors/reporters - create an inlined plugin
var module = Object.create(null)
var hasSomeInlinedPlugin = false
var types = ['launcher', 'preprocessor', 'reporter']
types.forEach(function (type) {
var definitions = config['custom' + helper.ucFirst(type) + 's'] || {}
Object.keys(definitions).forEach(function (name) {
var definition = definitions[name]
if (!helper.isObject(definition)) {
return log.warn('Can not define %s %s. Definition has to be an object.', type, name)
}
if (!helper.isString(definition.base)) {
return log.warn('Can not define %s %s. Missing base %s.', type, name, type)
}
var token = type + ':' + definition.base
var locals = {
args: ['value', definition]
}
module[type + ':' + name] = ['factory', function (injector) {
var plugin = injector.createChild([locals], [token]).get(token)
if (type === 'launcher' && helper.isDefined(definition.displayName)) {
plugin.displayName = definition.displayName
}
return plugin
}]
hasSomeInlinedPlugin = true
})
})
if (hasSomeInlinedPlugin) {
config.plugins.push(module)
}
return config
}
var Config = function () {
var config = this
this.LOG_DISABLE = constant.LOG_DISABLE
this.LOG_ERROR = constant.LOG_ERROR
this.LOG_WARN = constant.LOG_WARN
this.LOG_INFO = constant.LOG_INFO
this.LOG_DEBUG = constant.LOG_DEBUG
this.set = function (newConfig) {
Object.keys(newConfig).forEach(function (key) {
config[key] = newConfig[key]
})
}
// DEFAULT CONFIG
this.frameworks = []
this.protocol = 'http:'
this.port = constant.DEFAULT_PORT
this.hostname = constant.DEFAULT_HOSTNAME
this.httpsServerConfig = {}
this.basePath = ''
this.files = []
this.browserConsoleLogOptions = {level: 'debug', format: '%b %T: %m', terminal: true}
this.customContextFile = null
this.customDebugFile = null
this.exclude = []
this.logLevel = constant.LOG_INFO
this.colors = true
this.autoWatch = true
this.autoWatchBatchDelay = 250
this.restartOnFileChange = false
this.usePolling = process.platform === 'darwin' || process.platform === 'linux'
this.reporters = ['progress']
this.singleRun = false
this.browsers = []
this.captureTimeout = 60000
this.proxies = {}
this.proxyValidateSSL = true
this.preprocessors = {}
this.urlRoot = '/'
this.reportSlowerThan = 0
this.loggers = [constant.CONSOLE_APPENDER]
this.transports = ['polling', 'websocket']
this.forceJSONP = false
this.plugins = ['karma-*']
this.defaultClient = this.client = {
args: [],
useIframe: true,
captureConsole: true,
clearContext: true
}
this.browserDisconnectTimeout = 2000
this.browserDisconnectTolerance = 0
this.browserNoActivityTimeout = 10000
this.concurrency = Infinity
this.failOnEmptyTestSuite = true
this.retryLimit = 2
this.detached = false
}
var CONFIG_SYNTAX_HELP = ' module.exports = function(config) {\n' +
' config.set({\n' +
' // your config\n' +
' });\n' +
' };\n'
var parseConfig = function (configFilePath, cliOptions) {
var configModule
if (configFilePath) {
log.debug('Loading config %s', configFilePath)
try {
configModule = require(configFilePath)
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(configFilePath) !== -1) {
log.error('File %s does not exist!', configFilePath)
} else {
log.error('Invalid config file!\n ' + e.stack)
var extension = path.extname(configFilePath)
if (extension === '.coffee' && !COFFEE_SCRIPT_AVAILABLE) {
log.error('You need to install CoffeeScript.\n' +
' npm install coffee-script --save-dev')
} else if (extension === '.ls' && !LIVE_SCRIPT_AVAILABLE) {
log.error('You need to install LiveScript.\n' +
' npm install LiveScript --save-dev')
}
}
return process.exit(1)
}
if (!helper.isFunction(configModule)) {
log.error('Config file must export a function!\n' + CONFIG_SYNTAX_HELP)
return process.exit(1)
}
} else {
log.debug('No config file specified.')
// if no config file path is passed, we define a dummy config module.
configModule = function () {}
}
var config = new Config()
config.set(cliOptions)
try {
configModule(config)
} catch (e) {
log.error('Error in config file!\n', e)
return process.exit(1)
}
// merge the config from config file and cliOptions (precedence)
config.set(cliOptions)
// configure the logger as soon as we can
logger.setup(config.logLevel, config.colors, config.loggers)
return normalizeConfig(config, configFilePath)
}
// PUBLIC API
exports.parseConfig = parseConfig
exports.Pattern = Pattern
exports.createPatternObject = createPatternObject
exports.Config = Config