This repository was archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathapp.js
More file actions
236 lines (194 loc) · 7.25 KB
/
app.js
File metadata and controls
236 lines (194 loc) · 7.25 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
232
233
234
235
236
'use strict';
const path = require('path');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs-extra'));
const temp = require('temp');
const _ = require('lodash');
const chalk = require('chalk');
const findGemini = require('./find-gemini');
const loadReuseData = require('./reuse-loader');
const reporter = require('./reporter');
const Tests = require('./tests-model');
const Index = require('./common/tests-index');
const EventSource = require('./event-source');
const Runner = require('./runner');
const filterBrowsers = _.intersection;
const checkUnknownBrowsers = (browsersFromConfig, browsersFromCli) => {
const unknownBrowsers = _.difference(browsersFromCli, browsersFromConfig);
if (!_.isEmpty(unknownBrowsers)) {
console.warn('%s Unknown browser ids: %s. Use one of the browser ids specified in the config file: %s',
chalk.yellow('WARNING:'), unknownBrowsers.join(', '), browsersFromConfig.join(', '));
}
};
module.exports = class App {
constructor(options) {
this._options = options;
this.diffDir = temp.path('gemini-gui-diff');
this.currentDir = temp.path('gemini-gui-curr');
this._failedTests = new Index();
this._eventSource = new EventSource();
const Gemini = findGemini();
this._gemini = new Gemini(this._options.config, {cli: true, env: true});
_.set(this._gemini.config, 'system.tempDir', this.currentDir);
this._gemini.on('startRunner', (runner) => this._runner = runner);
const browserIds = this._gemini.browserIds;
checkUnknownBrowsers(browserIds, this._options.browser);
this.referenceDirs = _.zipObject(
browserIds,
browserIds.map((id) => this._gemini.config.forBrowser(id).screenshotsDir)
);
}
initialize() {
return this._recreateTmpDirs()
.then(() => loadReuseData(this._options.reuse))
.then((reuseDataAndPath) => {
if (reuseDataAndPath) {
this._reusePath = reuseDataAndPath.report;
return reuseDataAndPath.data;
}
})
.then((reuseData) => this._readTests(reuseData));
}
addClient(connection) {
this._eventSource.addConnection(connection);
}
sendClientEvent(event, data) {
this._eventSource.emit(event, data);
}
getTests() {
return Promise.resolve(this._tests.data);
}
getTestsStatus() {
return Promise.resolve(this._tests.status);
}
_readTests(reuseData) {
const opts = this._options;
return Promise.resolve(
this._gemini.readTests(opts.testFiles, {
grep: opts.grep,
sets: opts.set
})
)
.then((collection) => {
this._collection = collection;
return collection.topLevelSuites();
})
.then((suites) => {
if (!this._options.browser) {
return;
}
suites.map((suite) => {
suite.browsers = filterBrowsers(suite.browsers, this._options.browser);
});
})
.then(() => {
const suites = (reuseData || {}).suites;
this._tests = new Tests(this);
return this._tests.initialize(this._collection, suites);
});
}
run(specificTests) {
return Runner.create(this._collection, specificTests)
.run((collection) => this._gemini.test(collection, {
reporters: [reporter(this), 'flat']
}));
}
_recreateTmpDirs() {
return Promise.all([fs.removeAsync(this.currentDir), fs.removeAsync(this.diffDir)])
.then(() => Promise.all([
fs.mkdirpAsync(this.currentDir),
fs.mkdirpAsync(this.diffDir)
]));
}
buildDiff(failureReport) {
return this._buildDiffFile(failureReport)
.then((diffPath) => this.diffPathToURL(diffPath));
}
_buildDiffFile(failureReport) {
const diffPath = temp.path({dir: this.diffDir, suffix: '.png'});
return Promise.resolve(failureReport.saveDiffTo(diffPath))
.thenReturn(diffPath);
}
addNoReferenceTest(test) {
//adding ref path here because gemini requires real suite to calc ref path
//and on client we have just stringified path
test.referencePath = this.getScreenshotPath(test.suite, test.state.name, test.browserId);
this.addFailedTest(test);
}
addFailedTest(test) {
this._failedTests.add(test);
}
updateReferenceImage(testData) {
const test = this._failedTests.find(testData);
if (!test) {
return Promise.reject(new Error('No such test failed'));
}
const result = {
imagePath: test.referencePath,
updated: true,
suite: test.state.suite,
state: test.state,
browserId: test.browserId
};
return fs.mkdirpAsync(path.dirname(test.referencePath))
.then(() => fs.copyAsync(test.currentPath, test.referencePath))
.then(() => {
// if haven't run tests before updating screenshot then no runner present
// this happens when we reuse result
if (this._runner) {
this._runner.emit('updateResult', result);
}
return this.refPathToURL(test.referencePath, test.browserId);
});
}
getScreenshotPath(suite, stateName, browserId) {
return this._gemini.getScreenshotPath(suite, stateName, browserId);
}
getBrowserCapabilites(browserId) {
return this._gemini.getBrowserCapabilites(browserId);
}
getRootUrlforBrowser(browserId) {
return this._gemini.config.forBrowser(browserId).rootUrl;
}
createCurrentPathFor() {
return temp.path({dir: this.currentDir, suffix: '.png'});
}
createDiffPathFor() {
return temp.path({dir: this.diffDir, suffix: '.png'});
}
refPathToURL(fullPath, browserId) {
return this._appendTimestampToURL(
this._pathToURL(this.referenceDirs[browserId], fullPath, App.refPrefix + '/' + browserId)
);
}
currentPathToURL(fullPath) {
return this._appendTimestampToURL(
this._pathToURL(this.currentDir, fullPath, App.currentPrefix)
);
}
// add query string timestamp to avoid caching on client
_appendTimestampToURL(URL) {
return URL + '?t=' + new Date().valueOf();
}
diffPathToURL(fullPath) {
return this._pathToURL(this.diffDir, fullPath, App.diffPrefix);
}
_pathToURL(rootDir, fullPath, prefix) {
const relPath = path.relative(rootDir, fullPath);
return prefix + '/' + encodeURI(relPath);
}
copyImage(srcRelPath, dstPath) {
const srcPath = path.resolve(this._reusePath, srcRelPath);
// it is ok that image can be defunct
return fs.copyAsync(srcPath, dstPath).catch(() => {});
}
static get refPrefix() {
return '/ref';
}
static get currentPrefix() {
return '/curr';
}
static get diffPrefix() {
return '/diff';
}
};