Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (152 loc) · 5.46 KB
/
Copy pathindex.js
File metadata and controls
175 lines (152 loc) · 5.46 KB
8000
div>
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
'use strict';
var url = require('url'),
path = require('path'),
format = require('util').format,
fs = require('fs'),
yaml = require('js-yaml'),
inherit = require('inherit'),
GeminiError = require('../errors/gemini-error'),
options = require('./options');
module.exports = inherit({
__constructor: function(configData, cliOptions) {
var filePath;
if (typeof configData === 'string') {
filePath = configData;
configData = readConfigData(filePath);
}
//this.projectRoot should be assigned before any options parsing
//has taken place
if (!configData.projectRoot) {
throw new GeminiError('Field "projectRoot" is required when creating config programmatically');
}
this.projectRoot = path.resolve(configData.projectRoot);
this._checkForUnknownOptions(configData);
cliOptions = cliOptions || {};
Object.keys(options).forEach(function(property) {
if (this[property]) {
return;
}
var option = options[property],
value;
if (option.env && option.env in process.env) {
value = process.env[option.env];
} else if (option.override && property in cliOptions) {
value = cliOptions[property];
} else if (property in configData) {
value = configData[property];
} else if ('default' in option) {
value = option.default;
if (typeof value === 'function') {
value = value(this);
}
} else if (option.required) {
var message = format('Field "%s" is not specified', property);
if (filePath) {
message += ' in config file ' + filePath;
}
throw new GeminiError(
message,
format('Specify "%s" field', property)
);
}
if (typeof value === 'undefined') {
return;
}
if (option.validate) {
if (typeof value !== option.validate) {
throw new GeminiError(format('Field "%s" must be %s', property, option.validate));
}
}
if (option.map) {
value = option.map(value, this);
}
this[property] = value;
}, this);
this._checkGridUrl();
},
_checkForUnknownOptions: function(configData) {
Object.keys(configData).forEach(function(property) {
if (!options.hasOwnProperty(property)) {
throw new GeminiError(
format('Unknown config option: "%s"', property),
'List of the valid options:\n' +
'https://github.com/bem/gemini#configuration'
);
}
});
},
_checkGridUrl: function() {
if (!this.gridUrl && this._requiresGrid()) {
throw new GeminiError(
'Field "gridUrl" is required for using non-phantomjs browsers',
[
'Specify selenium grid URL in your config file or use only',
'phantomjs browser.',
'Selenium server installation instructions:',
'',
'https://code.google.com/p/selenium/wiki/Grid2'
].join('\n')
);
}
},
_requiresGrid: function() {
return Object.keys(this.browsers).some(function(name) {
return this.browsers[name].browserName !== 'phantomjs';
}, this);
},
getAbsoluteUrl: function getAbsoluteUrl(relUrl) {
return url.resolve(this.rootUrl, relUrl);
},
getScreenshotsDir: function(suite, state) {
return path.resolve(this.screenshotsDir, this._getPathForSuite(suite), state);
},
getScreenshotPath: function getScrenshotPath(suite, state, browser) {
return path.join(this.getScreenshotsDir(suite, state), browser + '.png');
},
_getPathForSuite: function(suite) {
var result = '';
while (suite) {
result = path.join(suite.name, result);
suite = suite.parent;
}
return result;
}
});
function readConfigData(filePath) {
var configData = readYAMLFile(filePath),
configDir = path.dirname(filePath);
if (configData.projectRoot) {
configData.projectRoot = path.resolve(configDir, configData.projectRoot);
} else {
configData.projectRoot = configDir;
}
return configData;
}
function readYAMLFile(configPath) {
var text = readFile(configPath);
return parseYAML(text, configPath);
}
function readFile(configPath) {
try {
return fs.readFileSync(configPath, 'utf8');
} catch (e) {
if (e.code === 'ENOENT') {
throw new GeminiError(
'Config file does not exist: ' + configPath,
'Specify config file or configure your project by following\nthe instructions:\n\n' +
'https://github.com/bem/gemini#configuration'
);
}
throw e;
}
}
function parseYAML(source, filename) {
try {
return yaml.safeLoad(source);
} catch (e) {
throw new GeminiError('Error while parsing a config file: ' + filename + '\n' +
e.reason + ' ' + e.mark,
'Gemini config should be valid YAML file.'
);
}
}