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:
parent
d99031ba46
commit
9b2e9ce6ca
@ -19,19 +19,14 @@ let Diff = require('text-diff');
|
|||||||
let mime = require('mime');
|
let mime = require('mime');
|
||||||
let PNG = require('pngjs').PNG;
|
let PNG = require('pngjs').PNG;
|
||||||
let pixelmatch = require('pixelmatch');
|
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 = {
|
module.exports = {
|
||||||
addMatchers: function(jasmine) {
|
addMatchers: function(jasmine, goldenPath, outputPath) {
|
||||||
jasmine.addMatchers(customMatchers);
|
jasmine.addMatchers({
|
||||||
},
|
toBeGolden: function(util, customEqualityTesters) {
|
||||||
|
return { compare: compare.bind(null, goldenPath, outputPath) };
|
||||||
removeOutputDir: function() {
|
}
|
||||||
if (fs.existsSync(OUTPUT_DIR))
|
});
|
||||||
rm(OUTPUT_DIR);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -84,19 +79,16 @@ function compareText(actual, expectedBuffer) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let customMatchers = {
|
/**
|
||||||
toBeGolden: function(util, customEqualityTesters) {
|
|
||||||
return {
|
|
||||||
/**
|
|
||||||
* @param {?Object} actual
|
* @param {?Object} actual
|
||||||
* @param {string} goldenName
|
* @param {string} goldenName
|
||||||
* @return {!{pass: boolean, message: (undefined|string)}}
|
* @return {!{pass: boolean, message: (undefined|string)}}
|
||||||
*/
|
*/
|
||||||
compare: function(actual, goldenName) {
|
function compare(goldenPath, outputPath, actual, goldenName) {
|
||||||
let expectedPath = path.join(GOLDEN_DIR, goldenName);
|
let expectedPath = path.join(goldenPath, goldenName);
|
||||||
let actualPath = path.join(OUTPUT_DIR, 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)) {
|
if (!fs.existsSync(expectedPath)) {
|
||||||
ensureOutputDir();
|
ensureOutputDir();
|
||||||
@ -135,13 +127,10 @@ let customMatchers = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function ensureOutputDir() {
|
function ensureOutputDir() {
|
||||||
if (!fs.existsSync(OUTPUT_DIR))
|
if (!fs.existsSync(outputPath))
|
||||||
fs.mkdirSync(OUTPUT_DIR);
|
fs.mkdirSync(outputPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} filePath
|
* @param {string} filePath
|
||||||
|
@ -15,11 +15,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
let fs = require('fs');
|
let fs = require('fs');
|
||||||
|
let rm = require('rimraf').sync;
|
||||||
let path = require('path');
|
let path = require('path');
|
||||||
let Browser = require('../lib/Browser');
|
let Browser = require('../lib/Browser');
|
||||||
let SimpleServer = require('./server/SimpleServer');
|
let SimpleServer = require('./server/SimpleServer');
|
||||||
let GoldenUtils = require('./golden-utils');
|
let GoldenUtils = require('./golden-utils');
|
||||||
|
|
||||||
|
let GOLDEN_DIR = path.join(__dirname, 'golden');
|
||||||
|
let OUTPUT_DIR = path.join(__dirname, 'output');
|
||||||
|
|
||||||
let PORT = 8907;
|
let PORT = 8907;
|
||||||
let PREFIX = 'http://localhost:' + PORT;
|
let PREFIX = 'http://localhost:' + PORT;
|
||||||
let EMPTY_PAGE = PREFIX + '/empty.html';
|
let EMPTY_PAGE = PREFIX + '/empty.html';
|
||||||
@ -50,7 +54,8 @@ describe('Puppeteer', function() {
|
|||||||
const assetsPath = path.join(__dirname, 'assets');
|
const assetsPath = path.join(__dirname, 'assets');
|
||||||
server = await SimpleServer.create(assetsPath, PORT);
|
server = await SimpleServer.create(assetsPath, PORT);
|
||||||
httpsServer = await SimpleServer.createHTTPS(assetsPath, HTTPS_PORT);
|
httpsServer = await SimpleServer.createHTTPS(assetsPath, HTTPS_PORT);
|
||||||
GoldenUtils.removeOutputDir();
|
if (fs.existsSync(OUTPUT_DIR))
|
||||||
|
rm(OUTPUT_DIR);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
afterAll(SX(async function() {
|
afterAll(SX(async function() {
|
||||||
@ -65,7 +70,7 @@ describe('Puppeteer', function() {
|
|||||||
page = await browser.newPage();
|
page = await browser.newPage();
|
||||||
server.reset();
|
server.reset();
|
||||||
httpsServer.reset();
|
httpsServer.reset();
|
||||||
GoldenUtils.addMatchers(jasmine);
|
GoldenUtils.addMatchers(jasmine, GOLDEN_DIR, OUTPUT_DIR);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user