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
174 lines (143 loc) · 5.06 KB
/
Copy pathapp.js
File metadata and controls
174 lines (143 loc) · 5.06 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
'use strict';
var path = require('path'),
q = require('q'),
fs = require('q-io/fs'),
temp = require('temp'),
_ = require('lodash'),
findGemini = require('./find-gemini'),
reporter = require('./reporter'),
Tests = require('./tests-model'),
Index = require('./common/tests-index'),
EventSource = require('./event-source'),
Runner = require('./runner');
function removeTreeIfExists(path) {
return fs.exists(path)
.then(function(exists) {
if (exists) {
return fs.removeTree(path);
}
});
}
function App(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();
var Gemini = findGemini();
this._gemini = new Gemini(this._options.configFile, {cli: true, env: true});
var browserIds = this._gemini.browserIds;
this.referenceDirs = _.zipObject(
browserIds,
browserIds.map(function(browserId) {
return this._gemini.config.forBrowser(browserId).screenshotsDir;
}.bind(this))
);
}
App.prototype.initialize = function() {
return this._recreateTmpDirs()
.then(this._readTests.bind(this));
};
App.prototype.addClient = function(connection) {
this._eventSource.addConnection(connection);
};
App.prototype.sendClientEvent = function(event, data) {
this._eventSource.emit(event, data);
};
App.prototype.getTests = function() {
return q.resolve(this._tests.data);
};
App.prototype._readTests = function() {
var _this = this;
return this._gemini.readTests(this._options.testFiles, this._options.grep)
.then(function(collection) {
_this._collection = collection;
_this._tests = new Tests(_this, collection);
});
};
App.prototype.run = function(specificTests) {
var _this = this;
return Runner.create(this._collection, specificTests)
.run(function(collection) {
return _this._gemini.test(collection, {
reporters: [reporter(_this), 'flat'],
tempDir: _this.currentDir
});
});
};
App.prototype._recreateTmpDirs = function() {
var _this = this;
return q.all([removeTreeIfExists(this.currentDir), removeTreeIfExists(this.diffDir)])
.then(function() {
return q.all([
fs.makeDirectory(_this.currentDir),
fs.makeDirectory(_this.diffDir)
]);
});
};
App.prototype.buildDiff = function(failureReport) {
var _this = this;
return this._buildDiffFile(failureReport)
.then(function(diffPath) {
return _this.diffPathToURL(diffPath);
});
};
App.prototype._buildDiffFile = function(failureReport) {
var diffPath = temp.path({dir: this.diffDir, suffix: '.png'});
return failureReport.saveDiffTo(diffPath).thenResolve(diffPath);
};
App.prototype.addNoReferenceTest = function(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);
};
App.prototype.addFailedTest = function(test) {
this._failedTests.add(test);
};
App.prototype.updateReferenceImage = function(testData) {
var _this = this,
test = this._failedTests.find(testData);
if (!test) {
return q.reject(new Error('No such test failed'));
}
return fs.makeTree(path.dirname(test.referencePath))
.then(function() {
return fs.copy(test.currentPath, test.referencePath);
})
.then(function() {
console.log('Reference image %s has been updated.', test.referencePath);
return _this.refPathToURL(test.referencePath, test.browserId);
});
};
App.prototype.getScreenshotPath = function(suite, stateName, browserId) {
return this._gemini.getScreenshotPath(suite, stateName, browserId);
};
App.prototype.getBrowserCapabilites = function(browserId) {
return this._gemini.getBrowserCapabilites(browserId);
};
App.prototype.refPathToURL = function(fullPath, browserId) {
return this._appendTimestampToURL(
this._pathToURL(this.referenceDirs[browserId], fullPath, App.refPrefix + '/' + browserId)
);
};
App.prototype.currentPathToURL = function(fullPath) {
return this._appendTimestampToURL(
this._pathToURL(this.currentDir, fullPath, App.currentPrefix)
);
};
// add query string timestamp to avoid caching on client
App.prototype._appendTimestampToURL = function(URL) {
return URL + '?t=' + new Date().valueOf();
};
App.prototype.diffPathToURL = function(fullPath) {
return this._pathToURL(this.diffDir, fullPath, App.diffPrefix);
};
App.prototype._pathToURL = function(rootDir, fullPath, prefix) {
var relPath = path.relative(rootDir, fullPath);
return prefix + '/' + encodeURI(relPath);
};
App.refPrefix = '/ref';
App.currentPrefix = '/curr';
App.diffPrefix = '/diff';
module.exports = App;