Skip to content

Commit

Permalink
chore: apply JAdshead's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayc0 committed Nov 16, 2023
1 parent 8fd7916 commit e3a7c86
Showing 1 changed file with 60 additions and 30 deletions.
90 changes: 60 additions & 30 deletions src/diff-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,38 @@ function composeDiff(options) {
return composer;
}

function writeFileWithHooks({
pathToFile,
content,
runtimeHooksPath,
testPath,
currentTestName,
}) {
let finalContent = content;
if (runtimeHooksPath) {
let runtimeHooks;
try {
// As `diffImageToSnapshot` can be called in a worker, and as we cannot pass a function
// to a worker, we need to use an external file path that can be imported
// eslint-disable-next-line import/no-dynamic-require, global-require
runtimeHooks = require(runtimeHooksPath);
} catch (e) {
throw new Error(`Couldn't import ${runtimeHooksPath}: ${e.message}`);
}
try {
finalContent = runtimeHooks.onBeforeWriteToDisc({
buffer: content,
destination: pathToFile,
testPath,
currentTestName,
});
} catch (e) {
throw new Error(`Couldn't execute onBeforeWriteToDisc: ${e.message}`);
}
}
fs.writeFileSync(pathToFile, finalContent);
}

function diffImageToSnapshot(options) {
const {
receivedImageBuffer,
Expand All @@ -224,38 +256,18 @@ function diffImageToSnapshot(options) {
runtimeHooksPath,
} = options;

const writeFileSync = (pathToFile, content) => {
let finalContent = content;
if (runtimeHooksPath) {
let runtimeHooks;
try {
// As `diffImageToSnapshot` can be called in a worker, and as we cannot pass a function
// to a worker, we need to use an external file path that can be imported
// eslint-disable-next-line import/no-dynamic-require, global-require
runtimeHooks = require(runtimeHooksPath);
} catch (e) {
throw new Error(`Couldn't import ${runtimeHooksPath}: ${e.message}`);
}
try {
finalContent = runtimeHooks.onBeforeWriteToDisc({
buffer: content,
destination: pathToFile,
testPath,
currentTestName,
});
} catch (e) {
throw new Error(`Couldn't execute onBeforeWriteToDisc: ${e.message}`);
}
}
fs.writeFileSync(pathToFile, finalContent);
};

const comparisonFn = comparisonMethod === 'ssim' ? ssimMatch : pixelmatch;
let result = {};
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}.png`);
if (!fs.existsSync(baselineSnapshotPath)) {
fs.mkdirSync(path.dirname(baselineSnapshotPath), { recursive: true });
writeFileSync(baselineSnapshotPath, receivedImageBuffer);
writeFileWithHooks({
pathToFile: baselineSnapshotPath,
content: receivedImageBuffer,
runtimeHooksPath,
testPath,
currentTestName,
});
result = { added: true };
} else {
const receivedSnapshotPath = path.join(receivedDir, `${snapshotIdentifier}${receivedPostfix}.png`);
Expand Down Expand Up @@ -323,7 +335,13 @@ function diffImageToSnapshot(options) {
if (isFailure({ pass, updateSnapshot })) {
if (storeReceivedOnFailure) {
fs.mkdirSync(path.dirname(receivedSnapshotPath), { recursive: true });
writeFileSync(receivedSnapshotPath, receivedImageBuffer);
writeFileWithHooks({
pathToFile: receivedSnapshotPath,
content: receivedImageBuffer,
runtimeHooksPath,
testPath,
currentTestName,
});
result = { receivedSnapshotPath };
}

Expand All @@ -349,7 +367,13 @@ function diffImageToSnapshot(options) {
// Set filter type to Paeth to avoid expensive auto scanline filter detection
// For more information see https://www.w3.org/TR/PNG-Filters.html
const pngBuffer = PNG.sync.write(compositeResultImage, { filterType: 4 });
writeFileSync(diffOutputPath, pngBuffer);
writeFileWithHooks({
pathToFile: diffOutputPath,
content: pngBuffer,
runtimeHooksPath,
testPath,
currentTestName,
});

result = {
...result,
Expand All @@ -363,7 +387,13 @@ function diffImageToSnapshot(options) {
};
} else if (shouldUpdate({ pass, updateSnapshot, updatePassedSnapshot })) {
fs.mkdirSync(path.dirname(baselineSnapshotPath), { recursive: true });
writeFileSync(baselineSnapshotPath, receivedImageBuffer);
writeFileWithHooks({
pathToFile: baselineSnapshotPath,
content: receivedImageBuffer,
runtimeHooksPath,
testPath,
currentTestName,
});
result = { updated: true };
} else {
result = {
Expand Down

0 comments on commit e3a7c86

Please sign in to comment.