-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigparser.js
223 lines (199 loc) · 5.82 KB
/
configparser.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
var path = require('path'),
glob = require('glob')
//log = require('./logger'),
//helper = require('./act');
// Coffee is required here to enable config files written in coffee-script.
try {
require('coffee-script').register();
} catch (e) {
// Intentionally blank - ignore if coffee-script is not available.
}
// LiveScript is required here to enable config files written in LiveScript.
try {
require('LiveScript');
} catch (e) {
// Intentionally blank - ignore if LiveScript is not available.
}
var ConfigParser = function() {
// Default configuration.
this.config_ = {
specs: [],
multiCapabilities: [],
rootElement: 'body',
allScriptsTimeout: 11000,
getPageTimeout: 10000,
params: {},
seleniumArgs: [],
chromeDriver: null,
//skipSourceMapSupport: false,
plugins: []
};
};
/**
* Merge config objects together.
*
* @private
* @param {Object} into
* @param {Object} from
*
* @return {Object} The 'into' config.
*/
var merge_ = function(into, from) {
for (var key in from) {
if (into[key] instanceof Object &&
!(into[key] instanceof Array) &&
!(into[key] instanceof Function)) {
merge_(into[key], from[key]);
} else {
into[key] = from[key];
}
}
return into;
};
/**
* Returns the item if it's an array or puts the item in an array
* if it was not one already.
*/
var makeArray = function(item) {
return Array.isArray(item) ? item : [item];
};
/**
* Adds to an array all the elements in another array without adding any
* duplicates
*
* @param {Array<string>} dest The array to add to
* @param {Array<string>} src The array to copy from
*/
var union = function(dest, src) {
var elems = {};
for (var key in dest) {
elems[key] = true;
}
for (key in src) {
if (!elems[key]) {
dest.push(key);
elems[key] = true;
}
}
};
/**
* Resolve a list of file patterns into a list of individual file paths.
*
* @param {Array.<string> | string} patterns
* @param {boolean} opt_omitWarnings Whether to omit did not match warnings
* @param {string} opt_relativeTo Path to resolve patterns against
*
* @return {Array} The resolved file paths.
*/
ConfigParser.resolveFilePatterns =
function(patterns, opt_omitWarnings, opt_relativeTo) {
var resolvedFiles = [];
var cwd = opt_relativeTo || process.cwd();
patterns = (typeof patterns === 'string') ?
[patterns] : patterns;
if (patterns) {
for (var i = 0; i < patterns.length; ++i) {
var fileName = patterns[i];
var matches = glob.sync(fileName, {cwd: cwd});
if (!matches.length && !opt_omitWarnings) {
console.log('pattern ' + patterns[i] + ' did not match any files.');
}
for (var j = 0; j < matches.length; ++j) {
var resolvedPath = path.resolve(cwd, matches[j]);
resolvedFiles.push(resolvedPath);
}
}
}
return resolvedFiles;
};
/**
* Returns only the specs that should run currently based on `config.suite`
*
* @return {Array} An array of globs locating the spec files
*/
ConfigParser.getSpecs = function(config) {
var specs = [];
if (config.suite) {
config.suite.split(',').forEach(function(suite) {
var suiteList = config.suites[suite];
if (suiteList == null) {
throw new Error('Unknown test suite: ' + suite);
}
union(specs, makeArray(suiteList));
});
return specs;
}
if (config.specs.length > 0) {
return config.specs;
}
config.suites.forEach(function(suite) {
union(specs, makeArray(suite));
});
return specs;
};
/**
* Add the options in the parameter config to this runner instance.
*
* @private
* @param {Object} additionalConfig
* @param {string} relativeTo the file path to resolve paths against
*/
ConfigParser.prototype.addConfig_ = function(additionalConfig, relativeTo) {
// All filepaths should be kept relative to the current config location.
// This will not affect absolute paths.
['seleniumServerJar', 'chromeDriver', 'onPrepare', 'firefoxPath',
'runnerPath'].
forEach(function(name) {
if (additionalConfig[name] &&
typeof additionalConfig[name] === 'string') {
additionalConfig[name] =
path.resolve(relativeTo, additionalConfig[name]);
}
});
merge_(this.config_, additionalConfig);
};
/**
* Public function specialized towards merging in a file's config
*
* @public
* @param {String} filename
*/
ConfigParser.prototype.addFileConfig = function(filename) {
try {
if (!filename) {
return this;
}
var filePath = path.resolve(process.cwd(), filename);
var fileConfig = require(filePath).config;
if (!fileConfig) {
console.log('configuration file ' + filename + ' did not export a config ' +
'object');
}
fileConfig.configDir = path.dirname(filePath);
this.addConfig_(fileConfig, fileConfig.configDir);
} catch (e) {
console.log('failed loading configuration file ' + filename);
throw e;
}
return this;
};
/**
* Public function specialized towards merging in config from argv
*
* @public
* @param {Object} argv
*/
ConfigParser.prototype.addConfig = function(argv) {
this.addConfig_(argv, process.cwd());
return this;
};
/**
* Public getter for the final, computed config object
*
* @public
* @return {Object} config
*/
ConfigParser.prototype.getConfig = function() {
return this.config_;
};
module.exports = ConfigParser;