1
1
var path = require ( 'path' ) ,
2
2
glob = require ( 'glob' ) ,
3
- config_ = {
4
- configDir : './' ,
5
- jasmineNodeOpts : { }
6
- } ,
7
- //this allows for ease of maintaining public apis of the config while still
8
- // allowing for easy variable renames or future config api changes while
9
- // supported backwards compatibility
10
- configMap_ = {
3
+ util = require ( 'util' ) ,
4
+ protractor = require ( './protractor.js' ) ;
5
+
11
6
12
- //structure is internal name -> supported config apis
13
- 'specs' : [ 'specs' ] ,
14
- 'exclude' : [ 'exclude' ] ,
15
- 'capabilities' : [ 'capabilities' ] ,
16
- 'seleniumHost' : [ 'seleniumAddress' ] ,
17
- 'rootElement' : [ 'rootElement' ] ,
18
- 'baseUrl' : [ 'baseUrl' ] ,
19
- 'timeout' : [ 'allScriptsTimeout' ] ,
20
- 'browserParams' : [ 'params' ] ,
21
- 'framework' : [ 'framework' ] ,
22
- 'jasmineOpts' : [ 'jasmineNodeOpts' ] ,
23
- 'mochaOpts' : [ 'mochaOpts' ] ,
24
- 'seleniumLocal.jar' : [ 'seleniumServerJar' ] ,
25
- 'seleniumLocal.args' : [ 'seleniumArgs' ] ,
26
- 'seleniumLocal.port' : [ 'seleniumPort' ] ,
27
- 'sauceAccount.user' : [ 'sauceUser' ] ,
28
- 'sauceAccount.key' : [ 'sauceKey' ] ,
29
- 'chromeDriver' : [ 'chromeDriver' ] ,
30
- 'chromeOnly' : [ 'chromeOnly' ] ,
31
- 'configDir' : [ 'configDir' ] ,
32
- 'cucumberOpts.require' : [ 'cucumberOpts.require' ] ,
33
- 'cucumberOpts.format' : [ 'cucumberOpts.format' ] ,
34
- 'cucumberOpts.tags' : [ 'cucumberOpts.tags' ]
35
- } ;
7
+ module . exports = ConfigParser = function ( ) {
8
+ // Default configuration.
9
+ this . config_ = {
10
+ specs : [ ] ,
11
+ capabilities : {
12
+ browserName : 'chrome'
13
+ } ,
14
+ multiCapabilities : [ ] ,
15
+ rootElement : 'body' ,
16
+ allScriptsTimeout : 11000 ,
17
+ params : { } ,
18
+ framework : 'jasmine' ,
19
+ jasmineNodeOpts : {
20
+ isVerbose : false ,
21
+ showColors : true ,
22
+ includeStackTrace : true ,
23
+ stackFilter : protractor . filterStackTrace ,
24
+ defaultTimeoutInterval : ( 30 * 1000 )
25
+ } ,
26
+ seleniumArgs : [ ] ,
27
+ cucumberOpts : { } ,
28
+ mochaOpts : {
29
+ ui : 'bdd' ,
30
+ reporter : 'list'
31
+ } ,
32
+ chromeDriver : null ,
33
+ configDir : './'
34
+ } ;
35
+ } ;
36
36
37
37
/**
38
38
* Merge config objects together.
@@ -57,145 +57,76 @@ var merge_ = function(into, from) {
57
57
/**
58
58
* Resolve a list of file patterns into a list of individual file paths.
59
59
*
60
- * @param {Array/String } patterns
61
- * @param {Boolean } opt_omitWarnings whether to omit did not match warnings
60
+ * @param {Array.<string> | string } patterns
61
+ * @param {=boolean } opt_omitWarnings Whether to omit did not match warnings
62
+ * @param {=string } opt_relativeTo Path to resolve patterns against
62
63
*
63
64
* @return {Array } The resolved file paths.
64
65
*/
65
- var resolveFilePatterns = function ( patterns , opt_omitWarnings ) {
66
+ ConfigParser . resolveFilePatterns =
67
+ function ( patterns , opt_omitWarnings , opt_relativeTo ) {
66
68
var resolvedFiles = [ ] ;
69
+ var cwd = opt_relativeTo || process . cwd ( ) ;
67
70
68
71
patterns = ( typeof patterns === 'string' ) ?
69
72
[ patterns ] : patterns ;
70
73
71
74
if ( patterns ) {
72
75
for ( var i = 0 ; i < patterns . length ; ++ i ) {
73
- var matches = glob . sync ( patterns [ i ] , { cwd : config_ . configDir } ) ;
76
+ var matches = glob . sync ( patterns [ i ] , { cwd : cwd } ) ;
74
77
if ( ! matches . length && ! opt_omitWarnings ) {
75
78
util . puts ( 'Warning: pattern ' + patterns [ i ] + ' did not match any files.' ) ;
76
79
}
77
80
for ( var j = 0 ; j < matches . length ; ++ j ) {
78
- resolvedFiles . push ( path . resolve ( config_ . configDir , matches [ j ] ) ) ;
81
+ resolvedFiles . push ( path . resolve ( cwd , matches [ j ] ) ) ;
79
82
}
80
83
}
81
84
}
82
85
return resolvedFiles ;
83
86
} ;
84
87
85
- /**
86
- * Helper to resolve file pattern strings relative to the cwd
87
- *
88
- * @private
89
- * @param {Array } list
90
- */
91
- var processFilePatterns_ = function ( list ) {
92
- var patterns = list . split ( ',' ) ;
93
- patterns . forEach ( function ( spec , index , arr ) {
94
- arr [ index ] = path . resolve ( process . cwd ( ) , spec ) ;
95
- } ) ;
96
- return patterns ;
97
- } ;
98
88
99
89
/**
100
90
* Add the options in the parameter config to this runner instance.
101
91
*
102
92
* @private
103
93
* @param {Object } additionalConfig
94
+ * @param {string } relativeTo the file path to resolve paths against
104
95
*/
105
- var addConfig_ = function ( additionalConfig ) {
96
+ ConfigParser . prototype . addConfig_ = function ( additionalConfig , relativeTo ) {
106
97
// All filepaths should be kept relative to the current config location.
107
98
// This will not affect absolute paths.
108
99
[ 'seleniumServerJar' , 'chromeDriver' , 'onPrepare' ] . forEach ( function ( name ) {
109
- if ( additionalConfig [ name ] && additionalConfig . configDir &&
110
- typeof additionalConfig [ name ] === 'string' ) {
100
+ if ( additionalConfig [ name ] && typeof additionalConfig [ name ] === 'string' ) {
111
101
additionalConfig [ name ] =
112
- path . resolve ( additionalConfig . configDir , additionalConfig [ name ] ) ;
102
+ path . resolve ( relativeTo , additionalConfig [ name ] ) ;
113
103
}
114
104
} ) ;
115
105
116
- // Make sure they're not trying to add in deprecated config vals
106
+ // Make sure they're not trying to add in deprecated config vals.
117
107
if ( additionalConfig . jasmineNodeOpts &&
118
108
additionalConfig . jasmineNodeOpts . specFolders ) {
119
109
throw new Error ( 'Using config.jasmineNodeOpts.specFolders is deprecated ' +
120
110
'since Protractor 0.6.0. Please switch to config.specs.' ) ;
121
111
}
122
- merge_ ( config_ , additionalConfig ) ;
112
+ merge_ ( this . config_ , additionalConfig ) ;
123
113
} ;
124
114
125
-
126
- /**
127
- * Merges in passed in configuration data with existing class defaults
128
- * @public
129
- * @param {Object } config - A set of properties collected that will be merged
130
- * with AbstractTestRunner defaults
131
- */
132
- var loadConfig = function ( configObj , configToLoad ) {
133
-
134
- if ( ! configToLoad || ! configObj ) {
135
- return ;
136
- }
137
-
138
- /* helper to set the correct value for string dot notation */
139
- function setConfig_ ( obj , str , val ) {
140
- str = str . split ( '.' ) ;
141
- while ( str . length > 1 ) {
142
- obj = obj [ str . shift ( ) ] ;
143
- }
144
- obj [ str . shift ( ) ] = val ;
145
- }
146
-
147
- /* helper to retrieve the correct value for string dot notation */
148
- function getConfig_ ( obj , str ) {
149
- var arr = str . split ( "." ) ;
150
- while ( arr . length && ( obj = obj [ arr . shift ( ) ] ) ) ;
151
- return obj ;
152
- }
153
-
154
- /* helper to determine whether a config value is empty based on type */
155
- function isEmpty_ ( val ) {
156
- return ( val !== null &&
157
- val !== '' &&
158
- val !== undefined &&
159
- ! ( val instanceof Array &&
160
- ! val . length ) &&
161
- ! ( val instanceof Object &&
162
- ! Object . keys ( val ) . length )
163
- ) ;
164
- }
165
-
166
- //object definition driven merging
167
- var key , configDef , configAlias , i ;
168
- for ( key in configMap_ ) {
169
-
170
- configDef = configMap_ [ key ] ;
171
- for ( i = 0 ; i < configDef . length ; i ++ ) {
172
- configAlias = configDef [ i ] ;
173
- var configVal = getConfig_ ( configToLoad , configAlias ) ;
174
- if ( isEmpty_ ( configVal ) ) {
175
- //override config default w/ passed in config
176
- setConfig_ ( configObj , key , configVal ) ;
177
- }
178
- }
179
- }
180
- } ;
181
-
182
-
183
-
184
-
185
115
/**
186
116
* Public function specialized towards merging in a file's config
187
117
*
188
118
* @public
189
119
* @param {String } filename
190
120
*/
191
- var addFileConfig = function ( filename ) {
121
+ ConfigParser . prototype . addFileConfig = function ( filename ) {
192
122
if ( ! filename ) {
193
- return ;
123
+ return ;
194
124
}
195
125
var filePath = path . resolve ( process . cwd ( ) , filename ) ;
196
126
var fileConfig = require ( filePath ) . config ;
197
127
fileConfig . configDir = path . dirname ( filePath ) ;
198
- addConfig_ ( fileConfig ) ;
128
+ this . addConfig_ ( fileConfig , fileConfig . configDir ) ;
129
+ return this ;
199
130
} ;
200
131
201
132
@@ -205,19 +136,9 @@ var addFileConfig = function(filename) {
205
136
* @public
206
137
* @param {Object } argv
207
138
*/
208
- var addArgvConfig = function ( argv ) {
209
- if ( ! argv ) {
210
- return ;
211
- }
212
- // Interpret/parse spec include/exclude patterns
213
- if ( argv . specs ) {
214
- argv . specs = processFilePatterns_ ( argv . specs ) ;
215
- }
216
- if ( argv . exclude ) {
217
- argv . exclude = processFilePatterns ( argv . exclude ) ;
218
- }
219
-
220
- addConfig_ ( argv ) ;
139
+ ConfigParser . prototype . addConfig = function ( argv ) {
140
+ this . addConfig_ ( argv , process . cwd ( ) ) ;
141
+ return this ;
221
142
} ;
222
143
223
144
@@ -227,13 +148,6 @@ var addArgvConfig = function(argv) {
227
148
* @public
228
149
* @return {Object } config
229
150
*/
230
- var getConfig = function ( ) {
231
- return config_ ;
151
+ ConfigParser . prototype . getConfig = function ( ) {
152
+ return this . config_ ;
232
153
} ;
233
-
234
-
235
- exports . addArgvConfig = addArgvConfig ;
236
- exports . addFileConfig = addFileConfig ;
237
- exports . getConfig = getConfig ;
238
- exports . loadConfig = loadConfig ;
239
- exports . resolveFilePatterns = resolveFilePatterns ;
0 commit comments