diff --git a/test/golden-utils.js b/test/golden-utils.js index f0d81fd5027..7b86eeab8f2 100644 --- a/test/golden-utils.js +++ b/test/golden-utils.js @@ -19,19 +19,14 @@ let Diff = require('text-diff'); let mime = require('mime'); let PNG = require('pngjs').PNG; let pixelmatch = require('pixelmatch'); -let rm = require('rimraf').sync; - -let GOLDEN_DIR = path.join(__dirname, 'golden'); -let OUTPUT_DIR = path.join(__dirname, 'output'); module.exports = { - addMatchers: function(jasmine) { - jasmine.addMatchers(customMatchers); - }, - - removeOutputDir: function() { - if (fs.existsSync(OUTPUT_DIR)) - rm(OUTPUT_DIR); + addMatchers: function(jasmine, goldenPath, outputPath) { + jasmine.addMatchers({ + toBeGolden: function(util, customEqualityTesters) { + return { compare: compare.bind(null, goldenPath, outputPath) }; + } + }); }, }; @@ -84,64 +79,58 @@ function compareText(actual, expectedBuffer) { }; } -let customMatchers = { - toBeGolden: function(util, customEqualityTesters) { +/** + * @param {?Object} actual + * @param {string} goldenName + * @return {!{pass: boolean, message: (undefined|string)}} + */ +function compare(goldenPath, outputPath, actual, goldenName) { + let expectedPath = path.join(goldenPath, goldenName); + let actualPath = path.join(outputPath, goldenName); + + let messageSuffix = 'Output is saved in "' + path.basename(outputPath + '" directory'); + + if (!fs.existsSync(expectedPath)) { + ensureOutputDir(); + fs.writeFileSync(actualPath, actual); return { - /** - * @param {?Object} actual - * @param {string} goldenName - * @return {!{pass: boolean, message: (undefined|string)}} - */ - compare: function(actual, goldenName) { - let expectedPath = path.join(GOLDEN_DIR, goldenName); - let actualPath = path.join(OUTPUT_DIR, goldenName); - - let messageSuffix = 'Output is saved in "' + path.basename(OUTPUT_DIR + '" directory'); - - if (!fs.existsSync(expectedPath)) { - ensureOutputDir(); - fs.writeFileSync(actualPath, actual); - return { - pass: false, - message: goldenName + ' is missing in golden results. ' + messageSuffix - }; - } - let expected = fs.readFileSync(expectedPath); - let comparator = GoldenComparators[mime.lookup(goldenName)]; - if (!comparator) { - return { - pass: false, - message: 'Failed to find comparator with type ' + mime.lookup(goldenName) + ': ' + goldenName - }; - } - let result = comparator(actual, expected); - if (!result) - return { pass: true }; - ensureOutputDir(); - fs.writeFileSync(actualPath, actual); - // Copy expected to the output/ folder for convenience. - fs.writeFileSync(addSuffix(actualPath, '-expected'), expected); - if (result.diff) { - let diffPath = addSuffix(actualPath, '-diff', result.diffExtension); - fs.writeFileSync(diffPath, result.diff); - } - - let message = goldenName + ' mismatch!'; - if (result.errorMessage) - message += ' ' + result.errorMessage; - return { - pass: false, - message: message + ' ' + messageSuffix - }; - - function ensureOutputDir() { - if (!fs.existsSync(OUTPUT_DIR)) - fs.mkdirSync(OUTPUT_DIR); - } - } + pass: false, + message: goldenName + ' is missing in golden results. ' + messageSuffix }; - }, -}; + } + let expected = fs.readFileSync(expectedPath); + let comparator = GoldenComparators[mime.lookup(goldenName)]; + if (!comparator) { + return { + pass: false, + message: 'Failed to find comparator with type ' + mime.lookup(goldenName) + ': ' + goldenName + }; + } + let result = comparator(actual, expected); + if (!result) + return { pass: true }; + ensureOutputDir(); + fs.writeFileSync(actualPath, actual); + // Copy expected to the output/ folder for convenience. + fs.writeFileSync(addSuffix(actualPath, '-expected'), expected); + if (result.diff) { + let diffPath = addSuffix(actualPath, '-diff', result.diffExtension); + fs.writeFileSync(diffPath, result.diff); + } + + let message = goldenName + ' mismatch!'; + if (result.errorMessage) + message += ' ' + result.errorMessage; + return { + pass: false, + message: message + ' ' + messageSuffix + }; + + function ensureOutputDir() { + if (!fs.existsSync(outputPath)) + fs.mkdirSync(outputPath); + } +} /** * @param {string} filePath diff --git a/test/test.js b/test/test.js index f96d2325b6d..d5c10930c94 100644 --- a/test/test.js +++ b/test/test.js @@ -15,11 +15,15 @@ */ let fs = require('fs'); +let rm = require('rimraf').sync; let path = require('path'); let Browser = require('../lib/Browser'); let SimpleServer = require('./server/SimpleServer'); let GoldenUtils = require('./golden-utils'); +let GOLDEN_DIR = path.join(__dirname, 'golden'); +let OUTPUT_DIR = path.join(__dirname, 'output'); + let PORT = 8907; let PREFIX = 'http://localhost:' + PORT; let EMPTY_PAGE = PREFIX + '/empty.html'; @@ -50,7 +54,8 @@ describe('Puppeteer', function() { const assetsPath = path.join(__dirname, 'assets'); server = await SimpleServer.create(assetsPath, PORT); httpsServer = await SimpleServer.createHTTPS(assetsPath, HTTPS_PORT); - GoldenUtils.removeOutputDir(); + if (fs.existsSync(OUTPUT_DIR)) + rm(OUTPUT_DIR); })); afterAll(SX(async function() { @@ -65,7 +70,7 @@ describe('Puppeteer', function() { page = await browser.newPage(); server.reset(); httpsServer.reset(); - GoldenUtils.addMatchers(jasmine); + GoldenUtils.addMatchers(jasmine, GOLDEN_DIR, OUTPUT_DIR); })); afterEach(function() {