forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare_pixels_test.js
More file actions
217 lines (181 loc) · 5.26 KB
/
Copy pathcompare_pixels_test.js
File metadata and controls
217 lines (181 loc) · 5.26 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
var fs = require('fs');
var getMockList = require('./assets/get_mock_list');
var getRequestOpts = require('./assets/get_image_request_options');
var getImagePaths = require('./assets/get_image_paths');
// packages inside the image server docker
var test = require('tape');
var request = require('request');
var gm = require('gm');
// pixel comparison tolerance
var TOLERANCE = 1e-6;
// wait time between each test batch
var BATCH_WAIT = 500;
// number of tests in each test batch
var BATCH_SIZE = 5;
// wait time between each test in test queue
var QUEUE_WAIT = 10;
/**
* Image pixel comparison test script.
*
* Called by `tasks/test_image.sh in `npm run test-image`.
*
* CLI arguments:
*
* 1. 'pattern' : glob determining which mock(s) are to be tested
* 2. --queue : if sent, the image will be run in queue instead of in batch.
* Makes the test run significantly longer, but is recommended on weak hardware.
*
* Examples:
*
* Run all tests in batch:
*
* npm run test-image
*
* Run the 'contour_nolines' test:
*
* npm run test-image -- contour_nolines
*
* Run all gl3d image test in queue:
*
* npm run test-image -- gl3d_* --queue
*/
var pattern = process.argv[2];
var mockList = getMockList(pattern);
var isInQueue = (process.argv[3] === '--queue');
if(mockList.length === 0) {
throw new Error('No mocks found with pattern ' + pattern);
}
// filter out untestable mocks if no pattern is specified
if(!pattern) {
console.log('Filtering out untestable mocks\n');
mockList = mockList.filter(untestableFilter);
}
// main
if(isInQueue) {
runInQueue(mockList);
}
else {
runInBatch(mockList);
}
/* Test cases:
*
* - font-wishlist
* - all gl2d
* - all mapbox
*
* don't behave consistently from run-to-run and/or
* machine-to-machine; skip over them for now.
*
*/
function untestableFilter(mockName) {
return !(
mockName === 'font-wishlist' ||
mockName.indexOf('gl2d_') !== -1 ||
mockName.indexOf('mapbox_') !== -1
);
}
function runInBatch(mockList) {
var running = 0;
// remove mapbox mocks if circle ci
test('testing mocks in batch', function(t) {
t.plan(mockList.length);
for(var i = 0; i < mockList.length; i++) {
run(mockList[i], t);
}
});
function run(mockName, t) {
if(running >= BATCH_SIZE) {
setTimeout(function() {
run(mockName, t);
}, BATCH_WAIT);
return;
}
running++;
// throttle the number of tests running concurrently
comparePixels(mockName, function(isEqual, mockName) {
running--;
t.ok(isEqual, mockName + ' should be pixel perfect');
});
}
}
function runInQueue(mockList) {
var index = 0;
test('testing mocks in queue', function(t) {
t.plan(mockList.length);
run(mockList[index], t);
});
function run(mockName, t) {
comparePixels(mockName, function(isEqual, mockName) {
t.ok(isEqual, mockName + ' should be pixel perfect');
index++;
if(index < mockList.length) {
setTimeout(function() {
run(mockList[index], t);
}, QUEUE_WAIT);
}
});
}
}
function comparePixels(mockName, cb) {
var requestOpts = getRequestOpts({ mockName: mockName }),
imagePaths = getImagePaths(mockName),
saveImageStream = fs.createWriteStream(imagePaths.test);
function checkImage() {
// baseline image must be generated first
if(!doesFileExist(imagePaths.baseline)) {
var err = new Error('baseline image not found');
return onEqualityCheck(err, false);
}
/*
* N.B. The non-zero tolerance was added in
* https://github.com/plotly/plotly.js/pull/243
* where some legend mocks started generating different png outputs
* on `npm run test-image` and `npm run test-image -- mock.json`.
*
* Note that the svg outputs for the problematic mocks were the same
* and playing around with the batch size and timeout durations
* did not seem to affect the results.
*
* With the above tolerance individual `npm run test-image` and
* `npm run test-image -- mock.json` give the same result.
*
* Further investigation is needed.
*/
var gmOpts = {
file: imagePaths.diff,
highlightColor: 'purple',
tolerance: TOLERANCE
};
gm.compare(
imagePaths.test,
imagePaths.baseline,
gmOpts,
onEqualityCheck
);
}
function onEqualityCheck(err, isEqual) {
if(err) {
touch(imagePaths.diff);
return console.error(err, mockName);
}
if(isEqual) {
fs.unlinkSync(imagePaths.diff);
}
cb(isEqual, mockName);
}
request(requestOpts)
.pipe(saveImageStream)
.on('close', checkImage);
}
function doesFileExist(filePath) {
try {
if(fs.statSync(filePath).isFile()) return true;
}
catch(e) {
return false;
}
return false;
}
function touch(filePath) {
fs.closeSync(fs.openSync(filePath, 'w'));
}