mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: add Prettier (#5825)
This commit is contained in:
@@ -21,15 +21,14 @@ const PNG = require('pngjs').PNG;
|
||||
const jpeg = require('jpeg-js');
|
||||
const pixelmatch = require('pixelmatch');
|
||||
|
||||
module.exports = {compare};
|
||||
module.exports = { compare };
|
||||
|
||||
const GoldenComparators = {
|
||||
'image/png': compareImages,
|
||||
'image/jpeg': compareImages,
|
||||
'text/plain': compareText
|
||||
'text/plain': compareText,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {?Object} actualBuffer
|
||||
* @param {!Buffer} expectedBuffer
|
||||
@@ -38,18 +37,31 @@ const GoldenComparators = {
|
||||
*/
|
||||
function compareImages(actualBuffer, expectedBuffer, mimeType) {
|
||||
if (!actualBuffer || !(actualBuffer instanceof Buffer))
|
||||
return {errorMessage: 'Actual result should be Buffer.'};
|
||||
return { errorMessage: 'Actual result should be Buffer.' };
|
||||
|
||||
const actual = mimeType === 'image/png' ? PNG.sync.read(actualBuffer) : jpeg.decode(actualBuffer);
|
||||
const expected = mimeType === 'image/png' ? PNG.sync.read(expectedBuffer) : jpeg.decode(expectedBuffer);
|
||||
const actual =
|
||||
mimeType === 'image/png'
|
||||
? PNG.sync.read(actualBuffer)
|
||||
: jpeg.decode(actualBuffer);
|
||||
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});
|
||||
const count = pixelmatch(expected.data, actual.data, diff.data, expected.width, expected.height, {threshold: 0.1});
|
||||
return count > 0 ? {diff: PNG.sync.write(diff)} : null;
|
||||
const diff = new PNG({ width: expected.width, height: expected.height });
|
||||
const count = pixelmatch(
|
||||
expected.data,
|
||||
actual.data,
|
||||
diff.data,
|
||||
expected.width,
|
||||
expected.height,
|
||||
{ threshold: 0.1 }
|
||||
);
|
||||
return count > 0 ? { diff: PNG.sync.write(diff) } : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,10 +71,9 @@ function compareImages(actualBuffer, expectedBuffer, mimeType) {
|
||||
*/
|
||||
function compareText(actual, expectedBuffer) {
|
||||
if (typeof actual !== 'string')
|
||||
return {errorMessage: 'Actual result should be string'};
|
||||
return { errorMessage: 'Actual result should be string' };
|
||||
const expected = expectedBuffer.toString('utf-8');
|
||||
if (expected === actual)
|
||||
return null;
|
||||
if (expected === actual) return null;
|
||||
const diff = new Diff();
|
||||
const result = diff.main(expected, actual);
|
||||
diff.cleanupSemantic(result);
|
||||
@@ -71,7 +82,7 @@ function compareText(actual, expectedBuffer) {
|
||||
html = `<link rel="stylesheet" href="file://${diffStylePath}">` + html;
|
||||
return {
|
||||
diff: html,
|
||||
diffExtension: '.html'
|
||||
diffExtension: '.html',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -86,14 +97,15 @@ function compare(goldenPath, outputPath, actual, goldenName) {
|
||||
const expectedPath = path.join(goldenPath, goldenName);
|
||||
const actualPath = path.join(outputPath, goldenName);
|
||||
|
||||
const messageSuffix = 'Output is saved in "' + path.basename(outputPath + '" directory');
|
||||
const messageSuffix =
|
||||
'Output is saved in "' + path.basename(outputPath + '" directory');
|
||||
|
||||
if (!fs.existsSync(expectedPath)) {
|
||||
ensureOutputDir();
|
||||
fs.writeFileSync(actualPath, actual);
|
||||
return {
|
||||
pass: false,
|
||||
message: goldenName + ' is missing in golden results. ' + messageSuffix
|
||||
message: goldenName + ' is missing in golden results. ' + messageSuffix,
|
||||
};
|
||||
}
|
||||
const expected = fs.readFileSync(expectedPath);
|
||||
@@ -102,12 +114,12 @@ 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};
|
||||
if (!result) return { pass: true };
|
||||
ensureOutputDir();
|
||||
if (goldenPath === outputPath) {
|
||||
fs.writeFileSync(addSuffix(actualPath, '-actual'), actual);
|
||||
@@ -122,16 +134,14 @@ function compare(goldenPath, outputPath, actual, goldenName) {
|
||||
}
|
||||
|
||||
let message = goldenName + ' mismatch!';
|
||||
if (result.errorMessage)
|
||||
message += ' ' + result.errorMessage;
|
||||
if (result.errorMessage) message += ' ' + result.errorMessage;
|
||||
return {
|
||||
pass: false,
|
||||
message: message + ' ' + messageSuffix
|
||||
message: message + ' ' + messageSuffix,
|
||||
};
|
||||
|
||||
function ensureOutputDir() {
|
||||
if (!fs.existsSync(outputPath))
|
||||
fs.mkdirSync(outputPath);
|
||||
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user