Newer
Older
diosmosis
a validé
/*!
* Piwik - free/libre analytics platform
diosmosis
a validé
*
* Image diff & HTML diff viewer generation.
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
diosmosis
a validé
var fs = require('fs'),
path = require('./path');
diosmosis
a validé
var DiffViewerGenerator = function (diffDir) {
this.diffDir = diffDir;
this.outputPath = path.join(diffDir, 'diffviewer.html');
this.failures = [];
};
DiffViewerGenerator.prototype.getDiffPath = function (testInfo) {
var baseDir = path.join(PIWIK_INCLUDE_PATH, 'tests/UI');
return path.resolve(path.join(baseDir, config.screenshotDiffDir, testInfo.name));
diosmosis
a validé
};
// TODO: diff output path shouldn't be stored in piwik repo
diosmosis
a validé
DiffViewerGenerator.prototype.getUrlForPath = function (path) {
return fs.relpath(path, this.diffDir);
diosmosis
a validé
};
DiffViewerGenerator.prototype.generate = function (callback) {
if (this.failures.length == 0) {
return callback();
}
Thomas Steur
a validé
console.log("Generating diff file");
diosmosis
a validé
Thomas Steur
a validé
var diffViewerContent = "<html>\
diosmosis
a validé
<head></head>\
<body>\
<h1>Screenshot Test Failures</h1>\
<table>\
<tr>\
<th>Name</th>\
<th>Expected</th>\
diosmosis
a validé
<th>Processed</th>\
<th>Difference</th>\
diosmosis
a validé
Thomas Steur
a validé
for (var i = 0; i != this.failures.length; ++i) {
var entry = this.failures[i];
var expectedUrl = null;
diosmosis
a validé
if (entry.expected) {
Thomas Steur
a validé
if (options['assume-artifacts']) {
require('child_process').spawn('cp', [entry.expected, this.getDiffPath(entry)]);
}
Thomas Steur
a validé
expectedUrl = filename,
screenshotRepo = options['screenshot-repo'] || 'piwik/piwik',
pathPrefix = options['screenshot-repo'] ? '/Test/UI' : '/tests/UI',
expectedUrlGithub = 'https://raw.githubusercontent.com/' + screenshotRepo + '/master' + pathPrefix
+ '/expected-screenshots/' + filename;
diosmosis
a validé
var expectedHtml = '';
Thomas Steur
a validé
if (!options['assume-artifacts']) {
expectedUrl = this.getUrlForPath(entry.expected);
Thomas Steur
a validé
expectedHtml += '<a href="' + expectedUrl + '">Expected</a> ';
githubUrl = '<a href="' + expectedUrlGithub + '">GitHub</a>';
diosmosis
a validé
} else {
var expectedHtml = '<em>Not found</em>';
diosmosis
a validé
}
if (entry.processed) {
if (options['assume-artifacts']) {
entry.processedUrl = path.join("../processed-ui-screenshots", path.basename(entry.processed));
} else {
Thomas Steur
a validé
entry.processedUrl = this.getUrlForPath(entry.processed);
}
diosmosis
a validé
}
var entryLocationHint = '',
diosmosis
a validé
hintSource = entry.expected || entry.processed,
m = hintSource ? hintSource.match(/\/plugins\/([^\/]*)\//) : null;
if (m) {
entryLocationHint = ' <em>(for ' + m[1] + ' plugin)</em>';
}
var processedEntryPath = '';
if (entry.processed) {
processedEntryPath = path.basename(entry.processed);
}
diffViewerContent += "\n\
<tr>\n\
<td>" + entry.name + entryLocationHint + "</td>\n\
<td>" + expectedHtml + "</td>\n\
<td>" + githubUrl + "</td>\n\
<td>" + (entry.processed ? ("<a href='" + entry.processedUrl + "'>Processed</a>") : "<em>Not found</em>") + "</td>\n\
<td>" + (expectedUrl ? ("<a href='singlediff.html?processed=" + entry.processedUrl + "&expected=" + expectedUrl + "&github=" + processedEntryPath + "'>Difference</a>") : "<em>Could not create diff.</em>") + "</td>\n\
</tr>\n";
diosmosis
a validé
}
diffViewerContent += '\
</table>\
</body>\
</html>';
Thomas Steur
a validé
fs.write(this.outputPath, diffViewerContent, "w");
diosmosis
a validé
Thomas Steur
a validé
console.log("Failures encountered. View all diffs at: " + this.outputPath);
diosmosis
a validé
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();
};
exports.DiffViewerGenerator = DiffViewerGenerator;