Make test/golden-utils.js reusable

This patch makes test/golden-utils.js configurable with
output directory and the golden directory.

The plan is to reuse golden-utils for doclint tests.
This commit is contained in:
Andrey Lushnikov 2017-07-13 10:07:24 -07:00
parent d99031ba46
commit 9b2e9ce6ca
2 changed files with 63 additions and 69 deletions

View File

@ -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,19 +79,16 @@ function compareText(actual, expectedBuffer) {
};
}
let customMatchers = {
toBeGolden: function(util, customEqualityTesters) {
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);
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(OUTPUT_DIR + '" directory');
let messageSuffix = 'Output is saved in "' + path.basename(outputPath + '" directory');
if (!fs.existsSync(expectedPath)) {
ensureOutputDir();
@ -135,13 +127,10 @@ let customMatchers = {
};
function ensureOutputDir() {
if (!fs.existsSync(OUTPUT_DIR))
fs.mkdirSync(OUTPUT_DIR);
if (!fs.existsSync(outputPath))
fs.mkdirSync(outputPath);
}
}
};
},
};
/**
* @param {string} filePath

View File

@ -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() {