Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 46 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const bodyParser = require('body-parser');
const { PNG } = require('pngjs');
/* eslint-enable node/no-extraneous-require */

const IMAGE_SIZE_ERROR = 'Image sizes do not match.';

module.exports = {
name: require('./package').name,

Expand Down Expand Up @@ -206,30 +208,44 @@ module.exports = {
}

const diff = new PNG({ width: baseImg.width, height: baseImg.height });
const errorPixelCount = pixelmatch(baseImg.data, tmpImg.data, diff.data, baseImg.width, baseImg.height, {
threshold: options.imageMatchThreshold,
includeAA: options.includeAA
});

if (errorPixelCount <= options.imageMatchAllowedFailures) {
return resolve();
try {
const errorPixelCount = pixelmatch(baseImg.data, tmpImg.data, diff.data, baseImg.width, baseImg.height, {
threshold: options.imageMatchThreshold,
includeAA: options.includeAA
});

if (errorPixelCount <= options.imageMatchAllowedFailures) {
return resolve();
}

let diffPath = path.join(options.imageDiffDirectory, fileName);

await fs.outputFile(diffPath, PNG.sync.write(diff));

Promise.all([
_this._tryUploadToImgur(imgPath),
_this._tryUploadToImgur(diffPath)
]).then(([urlTmp, urlDiff]) => {
reject({
errorPixelCount,
allowedErrorPixelCount: options.imageMatchAllowedFailures,
diffPath: urlDiff || diffPath,
tmpPath: urlTmp || imgPath
});
}).catch(reject);
} catch (e) {
if(e.message === IMAGE_SIZE_ERROR) {
reject({
errorPixelCount: Math.abs((baseImg.data.length - tmpImg.data.length) / 4),
allowedErrorPixelCount: options.imageMatchAllowedFailures,
diffPath: null,
tmpPath: imgPath
});
} else {
console.error(e);
reject(e);
}
}

let diffPath = path.join(options.imageDiffDirectory, fileName);

await fs.outputFile(diffPath, PNG.sync.write(diff));

Promise.all([
_this._tryUploadToImgur(imgPath),
_this._tryUploadToImgur(diffPath)
]).then(([urlTmp, urlDiff]) => {
reject({
errorPixelCount,
allowedErrorPixelCount: options.imageMatchAllowedFailures,
diffPath: urlDiff || diffPath,
tmpPath: urlTmp || imgPath
});
}).catch(reject);
}
});
},
Expand Down Expand Up @@ -299,9 +315,13 @@ module.exports = {

data.status = 'ERROR';
data.diffPath = diffPath;
data.fullDiffPath = path.join(__dirname, diffPath);
data.error = `${errorPixelCount} pixels differ - diff: ${diffPath}, img: ${tmpPath}`;

data.fullDiffPath = diffPath? path.join(__dirname, diffPath) : null;

if(reason.diffPath === null){
data.error = `Image sizes do not match ${errorPixelCount} pixels differ - img: ${tmpPath}`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data.error = `Image sizes do not match ${errorPixelCount} pixels differ - img: ${tmpPath}`;
data.error = `Image sizes do not match - ${errorPixelCount} pixels differ - img: ${tmpPath}`;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just copying the formatting of the existing error message

} else {
data.error = `${errorPixelCount} pixels differ - diff: ${diffPath}, img: ${tmpPath}`;
}
res.send(data);
});
});
Expand Down