-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
diff-viewer.js
159 lines (127 loc) · 4.68 KB
/
diff-viewer.js
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
/*!
* Piwik - Web Analytics
*
* Image diff & HTML diff viewer generation.
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
var fs = require('fs'),
path = require('./path');
var DiffViewerGenerator = function (diffDir) {
this.diffDir = diffDir;
this.outputPath = path.join(diffDir, 'diffviewer.html');
this.failures = [];
this.isCompareAvailable = true;
};
DiffViewerGenerator.prototype.checkImageMagickCompare = function (callback) {
var self = this;
var child = require('child_process').spawn('compare', '--help');
child.on("exit", function (code) {
self.isCompareAvailable = code == 0 || code == 1;
if (!self.isCompareAvailable) {
console.log("Cannot find ImageMagick compare utility, no diffs will be created.");
}
callback();
});
};
DiffViewerGenerator.prototype.getDiffPath = function (testInfo) {
return path.resolve(path.join(this.diffDir, testInfo.name + '.png'));
};
// TODO: diff output path shouldn't be stored in piwik-ui-tests repo
DiffViewerGenerator.prototype.getUrlForPath = function (path) {
return fs.relpath(path, this.diffDir);
};
DiffViewerGenerator.prototype.generate = function (callback) {
if (this.failures.length == 0) {
return callback();
}
console.log("Generating diffs...");
var self = this;
this.generateDiffs(function () {
var diffViewerContent = "<html>\
<head></head>\
<body>\
<h1>Screenshot Test Failures</h1>\
<table>\
<tr>\
<th>Name</th>\
<th>Expected</th>\
<th>Processed</th>\
<th>Difference</th>\
</tr>";
for (var i = 0; i != self.failures.length; ++i) {
var entry = self.failures[i];
if (entry.expected) {
var expectedUrl = self.getUrlForPath(entry.expected);
var expectedUrlGithub = 'https://raw.github.com/piwik/piwik-ui-tests/master/expected-ui-screenshots/'
+ entry.name + '.png';
var expectedHtml = '<a href="' + expectedUrl + '">Expected</a> <a href="' + expectedUrlGithub
+ '">[Github]</a>';
} else {
var expectedHtml = '<em>Not found</em>';
}
if (entry.processed) {
entry.processedUrl = self.getUrlForPath(entry.processed);
}
var entryLocationHint = '',
m = entry.expected.match(/\/plugins\/([^\/]*)\//);
if (m) {
entryLocationHint = ' <em>(for ' + m[1] + ' plugin)</em>';
}
diffViewerContent += '\
<tr>\
<td>' + entry.name + entryLocationHint + '</td>\
<td>' + expectedHtml + '</td>\
<td>' + (entry.processed ? ('<a href="' + entry.processedUrl + '">Processed</a>') : '<em>Not found</em>') + '</td>\
<td>' + (entry.diffUrl ? ('<a href="' + entry.diffUrl + '">Difference</a>') : '<em>Could not create diff.</em>') + '</td>\
</tr>';
}
diffViewerContent += '\
</table>\
</body>\
</html>';
fs.write(self.outputPath, diffViewerContent, "w");
console.log("Failures encountered. View all diffs at: " + self.outputPath);
console.log();
console.log("If processed screenshots are correct, you can copy the generated screenshots to the expected "
+ "screenshot folder.");
console.log();
console.log("*** IMPORTANT *** In your commit message, explain the cause of the difference in rendering so other "
+ "Piwik developers will be aware of it.");
callback();
});
};
DiffViewerGenerator.prototype.generateDiffs = function (callback, i) {
i = i || 0;
if (i >= this.failures.length
|| !this.isCompareAvailable
) {
callback();
return;
}
var entry = this.failures[i];
if (entry.expected
&& entry.processed
) {
var diffPath = this.getDiffPath(entry);
var child = require('child_process').spawn('compare', [entry.expected, entry.processed, diffPath]);
child.stdout.on("data", function (data) {
fs.write("/dev/stdout", data, "w");
});
child.stderr.on("data", function (data) {
fs.write("/dev/stderr", data, "w");
});
var self = this;
child.on("exit", function (code) {
if (!code) {
console.log("Saved diff to " + diffPath);
entry.diffUrl = entry.name + '.png';
}
self.generateDiffs(callback, i + 1);
});
} else {
this.generateDiffs(callback, i + 1);
}
};
exports.DiffViewerGenerator = DiffViewerGenerator;