fix golden matchers

This commit is contained in:
Andrey Lushnikov 2020-02-20 22:12:26 -08:00
parent 53078bf891
commit 8ce618ba87
2 changed files with 21 additions and 8 deletions

View file

@ -20,6 +20,7 @@ const Diff = require('text-diff');
const PNG = require('pngjs').PNG; const PNG = require('pngjs').PNG;
const jpeg = require('jpeg-js'); const jpeg = require('jpeg-js');
const pixelmatch = require('pixelmatch'); const pixelmatch = require('pixelmatch');
const c = require('colors/safe');
module.exports = {compare}; module.exports = {compare};
@ -51,7 +52,7 @@ function compareImages(actualBuffer, expectedBuffer, mimeType) {
const expected = mimeType === 'image/png' ? PNG.sync.read(expectedBuffer) : jpeg.decode(expectedBuffer); const expected = mimeType === 'image/png' ? PNG.sync.read(expectedBuffer) : jpeg.decode(expectedBuffer);
if (expected.width !== actual.width || expected.height !== actual.height) { if (expected.width !== actual.width || expected.height !== actual.height) {
return { return {
errorMessage: `Sizes differ: expected image ${expected.width}px X ${expected.height}px, but got ${actual.width}px X ${actual.height}px. ` errorMessage: `Sizes differ; expected image ${expected.width}px X ${expected.height}px, but got ${actual.width}px X ${actual.height}px. `
}; };
} }
const diff = new PNG({width: expected.width, height: expected.height}); const diff = new PNG({width: expected.width, height: expected.height});
@ -110,23 +111,34 @@ function compare(goldenPath, outputPath, actual, goldenName) {
if (!comparator) { if (!comparator) {
return { return {
pass: false, pass: false,
message: 'Failed to find comparator with type ' + mimeType + ': ' + goldenName message: 'Failed to find comparator with type ' + mimeType + ': ' + goldenName,
}; };
} }
const result = comparator(actual, expected, mimeType); const result = comparator(actual, expected, mimeType);
if (!result) if (!result)
return { pass: true }; return { pass: true };
ensureOutputDir(); ensureOutputDir();
const output = [
c.red(`GOLDEN FAILED: `) + c.yellow('"' + goldenName + '"'),
];
if (result.errorMessage)
output.push(' ' + result.errorMessage);
output.push('');
output.push(`Expected: ${c.yellow(expectedPath)}`);
if (goldenPath === outputPath) { if (goldenPath === outputPath) {
fs.writeFileSync(addSuffix(actualPath, '-actual'), actual); const filepath = addSuffix(actualPath, '-actual');
fs.writeFileSync(filepath, actual);
output.push(`Received: ${c.yellow(filepath)}`);
} else { } else {
fs.writeFileSync(actualPath, actual); fs.writeFileSync(actualPath, actual);
// Copy expected to the output/ folder for convenience. // Copy expected to the output/ folder for convenience.
fs.writeFileSync(addSuffix(actualPath, '-expected'), expected); fs.writeFileSync(addSuffix(actualPath, '-expected'), expected);
output.push(`Received: ${c.yellow(actualPath)}`);
} }
if (result.diff) { if (result.diff) {
const diffPath = addSuffix(actualPath, '-diff', result.diffExtension); const diffPath = addSuffix(actualPath, '-diff', result.diffExtension);
fs.writeFileSync(diffPath, result.diff); fs.writeFileSync(diffPath, result.diff);
output.push(` Diff: ${c.yellow(diffPath)}`);
} }
let message = goldenName + ' mismatch!'; let message = goldenName + ' mismatch!';
@ -134,7 +146,8 @@ function compare(goldenPath, outputPath, actual, goldenName) {
message += ' ' + result.errorMessage; message += ' ' + result.errorMessage;
return { return {
pass: false, pass: false,
message: message + ' ' + messageSuffix message: message + ' ' + messageSuffix,
formatter: () => output.join('\n'),
}; };
function ensureOutputDir() { function ensureOutputDir() {

View file

@ -76,9 +76,9 @@ function stringFormatter(received, expected) {
diff.cleanupSemantic(result); diff.cleanupSemantic(result);
const highlighted = result.map(([type, text]) => { const highlighted = result.map(([type, text]) => {
if (type === -1) if (type === -1)
return c.red(text); return c.bgRed(text);
if (type === 1) if (type === 1)
return c.green(text); return c.bgGreen.black(text);
return text; return text;
}).join(''); }).join('');
const output = [ const output = [
@ -133,9 +133,9 @@ function objectFormatter(received, expected) {
const highlighted = result.map(([type, text]) => { const highlighted = result.map(([type, text]) => {
const lines = doDecodeLines(text); const lines = doDecodeLines(text);
if (type === -1) if (type === -1)
return lines.map(line => '- ' + c.red(line)); return lines.map(line => '- ' + c.bgRed(line));
if (type === 1) if (type === 1)
return lines.map(line => '+ ' + c.green(line)); return lines.map(line => '+ ' + c.bgGreen.black(line));
return lines.map(line => ' ' + line); return lines.map(line => ' ' + line);
}).flat().join('\n'); }).flat().join('\n');
return `Received:\n${highlighted}`; return `Received:\n${highlighted}`;