From 8ce618ba87a26766eb01c7d6cd11ac77820e5516 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 20 Feb 2020 22:12:26 -0800 Subject: [PATCH] fix golden matchers --- test/golden-utils.js | 21 +++++++++++++++++---- utils/testrunner/Matchers.js | 8 ++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/test/golden-utils.js b/test/golden-utils.js index ebb152f0e9..7ae32399d8 100644 --- a/test/golden-utils.js +++ b/test/golden-utils.js @@ -20,6 +20,7 @@ const Diff = require('text-diff'); const PNG = require('pngjs').PNG; const jpeg = require('jpeg-js'); const pixelmatch = require('pixelmatch'); +const c = require('colors/safe'); module.exports = {compare}; @@ -51,7 +52,7 @@ function compareImages(actualBuffer, expectedBuffer, mimeType) { const expected = mimeType === 'image/png' ? PNG.sync.read(expectedBuffer) : jpeg.decode(expectedBuffer); if (expected.width !== actual.width || expected.height !== actual.height) { 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}); @@ -110,23 +111,34 @@ function compare(goldenPath, outputPath, actual, goldenName) { if (!comparator) { return { 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); if (!result) return { pass: true }; 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) { - fs.writeFileSync(addSuffix(actualPath, '-actual'), actual); + const filepath = addSuffix(actualPath, '-actual'); + fs.writeFileSync(filepath, actual); + output.push(`Received: ${c.yellow(filepath)}`); } else { fs.writeFileSync(actualPath, actual); // Copy expected to the output/ folder for convenience. fs.writeFileSync(addSuffix(actualPath, '-expected'), expected); + output.push(`Received: ${c.yellow(actualPath)}`); } if (result.diff) { const diffPath = addSuffix(actualPath, '-diff', result.diffExtension); fs.writeFileSync(diffPath, result.diff); + output.push(` Diff: ${c.yellow(diffPath)}`); } let message = goldenName + ' mismatch!'; @@ -134,7 +146,8 @@ function compare(goldenPath, outputPath, actual, goldenName) { message += ' ' + result.errorMessage; return { pass: false, - message: message + ' ' + messageSuffix + message: message + ' ' + messageSuffix, + formatter: () => output.join('\n'), }; function ensureOutputDir() { diff --git a/utils/testrunner/Matchers.js b/utils/testrunner/Matchers.js index d50ab6375b..55976230dd 100644 --- a/utils/testrunner/Matchers.js +++ b/utils/testrunner/Matchers.js @@ -76,9 +76,9 @@ function stringFormatter(received, expected) { diff.cleanupSemantic(result); const highlighted = result.map(([type, text]) => { if (type === -1) - return c.red(text); + return c.bgRed(text); if (type === 1) - return c.green(text); + return c.bgGreen.black(text); return text; }).join(''); const output = [ @@ -133,9 +133,9 @@ function objectFormatter(received, expected) { const highlighted = result.map(([type, text]) => { const lines = doDecodeLines(text); if (type === -1) - return lines.map(line => '- ' + c.red(line)); + return lines.map(line => '- ' + c.bgRed(line)); if (type === 1) - return lines.map(line => '+ ' + c.green(line)); + return lines.map(line => '+ ' + c.bgGreen.black(line)); return lines.map(line => ' ' + line); }).flat().join('\n'); return `Received:\n${highlighted}`;