This repository was archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathgemini.js
More file actions
231 lines (182 loc) · 6.91 KB
/
gemini.js
File metadata and controls
231 lines (182 loc) · 6.91 KB
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
'use strict';
const debug = require('debug');
const chalk = require('chalk');
const _ = require('lodash');
const PassthroughEmitter = require('./passthrough-emitter');
const Promise = require('bluebird');
const pluginsLoader = require('plugins-loader');
const gracefulFs = require('graceful-fs');
const Config = require('./config');
const GeminiError = require('./errors/gemini-error');
const readTests = require('./test-reader');
const Runner = require('./runner');
const Events = require('./constants/events');
const StateProcessor = require('./state-processor');
const SuiteCollection = require('./suite-collection');
const {temp} = require('gemini-core');
const PREFIX = require('../package').name + '-';
Promise.promisifyAll(require('fs-extra'));
// patch fs module prototype for preventing EMFILE error (too many open files)
gracefulFs.gracefulify(require('fs'));
const parseBrowsers = (browsers) => {
return browsers && browsers.replace(/\s/g, '').split(',');
};
module.exports = class Gemini extends PassthroughEmitter {
static create(config, allowOverrides) {
return new Gemini(config, allowOverrides);
}
static readRawConfig(filePath) {
return Config.readRawConfig(filePath);
}
constructor(config, allowOverrides) {
super();
this.config = new Config(config, allowOverrides);
this.events = Events;
this.SuiteCollection = SuiteCollection;
setupLog(this.config.system.debug);
this._loadPlugins();
}
extendCli(parser) {
this.emit(Events.CLI, parser);
}
getScreenshotPath(suite, stateName, browserId) {
return this.config.forBrowser(browserId).getScreenshotPath(suite, stateName);
}
getBrowserCapabilites(browserId) {
return this.config.forBrowser(browserId).desiredCapabilities;
}
getValidBrowsers(browsers) {
return _.intersection(browsers, this.browserIds);
}
checkUnknownBrowsers(browsers) {
const browsersFromConfig = this.browserIds;
const unknownBrowsers = _.difference(browsers, browsersFromConfig);
if (unknownBrowsers.length) {
console.warn(
`${chalk.yellow('WARNING:')} Unknown browsers id: ${unknownBrowsers.join(', ')}.\n` +
`Use one of the browser ids specified in config file: ${browsersFromConfig.join(', ')}`
);
}
}
get browserIds() {
return this.config.getBrowserIds();
}
update(paths, options) {
return this._exec(() => this._run(StateProcessor.createScreenUpdater(options), paths, options));
}
test(paths, options) {
return this._exec(() => this._run(StateProcessor.createTester(this.config), paths, options));
}
readTests(paths, options = {}) {
return this._exec(() => this._readTests(paths, options));
}
_exec(fn) {
return this._init().then(() => fn());
}
_init() {
this._init = () => Promise.resolve(); // init only once
return this.emitAndWait(Events.INIT);
}
_loadPlugins() {
pluginsLoader.load(this, this.config.system.plugins, PREFIX);
}
_readTests(paths, options) {
options = _.assignIn(options, {paths});
return readTests(this, this.config, options)
.then((rootSuite) => {
if (options.grep) {
applyGrep_(options.grep, rootSuite);
}
const suiteCollection = new SuiteCollection(rootSuite.children);
this.emit(Events.AFTER_TESTS_READ, {suiteCollection});
return suiteCollection;
});
function applyGrep_(grep, suite) {
if (!suite.hasStates) {
_.clone(suite.children).forEach((child) => {
applyGrep_(grep, child, suite);
});
} else {
if (!grep.test(suite.fullName)) {
suite.parent.removeChild(suite);
}
}
if (!suite.hasStates && !suite.children.length && suite.parent) {
suite.parent.removeChild(suite);
}
}
}
_run(stateProcessor, paths, options) {
if (!options) {
//if there are only two arguments, they are
//(stateProcessor, options) and paths are
//the default.
options = paths;
paths = undefined;
}
options = options || {};
options.reporters = options.reporters || [];
temp.init(this.config.system.tempDir);
const runner = Runner.create(this.config, stateProcessor);
const envBrowsers = parseBrowsers(process.env.GEMINI_BROWSERS);
const envSkipBrowsers = parseBrowsers(process.env.GEMINI_SKIP_BROWSERS);
options.browsers = options.browsers || envBrowsers;
this._passThroughEvents(runner);
// it is important to require signal handler here in order to guarantee subscribing to "INTERRUPT" event
require('./signal-handler').on(Events.INTERRUPT, (data) => {
this.emit(Events.INTERRUPT, data);
runner.cancel();
});
if (options.browsers) {
this.checkUnknownBrowsers(options.browsers);
}
const getTests = (source, options) => {
return source instanceof SuiteCollection
? Promise.resolve(source)
: this._readTests(source, options);
};
return getTests(paths, options)
.then((suiteCollection) => {
this.checkUnknownBrowsers(envSkipBrowsers);
const validSkippedBrowsers = this.getValidBrowsers(envSkipBrowsers);
suiteCollection.skipBrowsers(validSkippedBrowsers);
options.reporters.forEach((reporter) => applyReporter(runner, reporter));
let testsStatistic;
runner.on(Events.END, (stats) => testsStatistic = stats);
return runner.run(suiteCollection)
.then(() => testsStatistic);
});
}
_passThroughEvents(runner) {
this.passthroughEvent(runner, _.values(Events));
}
};
function applyReporter(runner, reporter) {
if (typeof reporter === 'string') {
reporter = {name: reporter};
}
if (typeof reporter === 'object') {
const reporterPath = reporter.path;
try {
reporter = require('./reporters/' + reporter.name);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new GeminiError('No such reporter: ' + reporter.name);
}
throw e;
}
return reporter(runner, reporterPath);
}
if (typeof reporter !== 'function') {
throw new TypeError('Reporter must be a string, an object or a function');
}
reporter(runner);
}
function setupLog(isDebug) {
if (isDebug) {
Promise.config({
longStackTraces: true
});
debug.enable('gemini:*');
}
}