From 6c84fd47744cc818976c917cc269b5cd3910d93d Mon Sep 17 00:00:00 2001 From: Kurt Tomlinson Date: Fri, 9 Oct 2020 10:02:43 -0400 Subject: [PATCH 1/3] create diff output folder before trying to create diff output file --- packages/diff-looks-same/src/create-looks-same-differ.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/diff-looks-same/src/create-looks-same-differ.js b/packages/diff-looks-same/src/create-looks-same-differ.js index 033a2e6e..1aa501ba 100644 --- a/packages/diff-looks-same/src/create-looks-same-differ.js +++ b/packages/diff-looks-same/src/create-looks-same-differ.js @@ -20,12 +20,13 @@ function createLooksSameDiffer(config) { return reject(new Error('Current image is empty')); } - return looksSame(reference, current, instanceConfig, (err, isSame) => { + return looksSame(reference, current, instanceConfig, async (err, isSame) => { if (err) { reject(err); } else if (isSame) { resolve(isSame); } else { + await fs.ensureFile(diffPath); looksSame.createDiff( { ...instanceConfig, From 8b9661466318643350558b5b961b69e1fc87bf31 Mon Sep 17 00:00:00 2001 From: Kurt Tomlinson Date: Fri, 9 Oct 2020 10:08:16 -0400 Subject: [PATCH 2/3] use sync fs function instead --- packages/diff-looks-same/src/create-looks-same-differ.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/diff-looks-same/src/create-looks-same-differ.js b/packages/diff-looks-same/src/create-looks-same-differ.js index 1aa501ba..7ba7f5e7 100644 --- a/packages/diff-looks-same/src/create-looks-same-differ.js +++ b/packages/diff-looks-same/src/create-looks-same-differ.js @@ -20,13 +20,13 @@ function createLooksSameDiffer(config) { return reject(new Error('Current image is empty')); } - return looksSame(reference, current, instanceConfig, async (err, isSame) => { + return looksSame(reference, current, instanceConfig, (err, isSame) => { if (err) { reject(err); } else if (isSame) { resolve(isSame); } else { - await fs.ensureFile(diffPath); + fs.ensureFileSync(diffPath); looksSame.createDiff( { ...instanceConfig, From f72172ea3242221ce94839cf06b4ace1e153fd27 Mon Sep 17 00:00:00 2001 From: Kurt Tomlinson Date: Sat, 14 Nov 2020 12:58:22 -0500 Subject: [PATCH 3/3] add a spec to verify that deeply nested diff files can be created --- .../src/create-looks-same-differ.spec.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 packages/diff-looks-same/src/create-looks-same-differ.spec.js diff --git a/packages/diff-looks-same/src/create-looks-same-differ.spec.js b/packages/diff-looks-same/src/create-looks-same-differ.spec.js new file mode 100644 index 00000000..7083a0a4 --- /dev/null +++ b/packages/diff-looks-same/src/create-looks-same-differ.spec.js @@ -0,0 +1,51 @@ +const fs = require('fs-extra'); +const path = require('path'); +const createLooksSameDiffer = require('./create-looks-same-differ'); + +const workingDirectory = './fwtaajxnfeuzedzu'; +const darkGrayPath = path.join(workingDirectory, 'dark-dray.png'); +const lightGrayPath = path.join(workingDirectory, 'light-dray.png'); + +function writeBase64Image({ outputPath, base64String }) { + const imageData = base64String.split(';base64,').pop(); + + fs.ensureFileSync(outputPath); + fs.writeFileSync(outputPath, imageData, { encoding: 'base64' }); +} + +describe('createLooksSameDiffer', () => { + beforeEach(() => { + const darkGrayBase64String = + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQYV2NISUn5DwAEiAIshLJN6AAAAABJRU5ErkJggg=='; + writeBase64Image({ + outputPath: darkGrayPath, + base64String: darkGrayBase64String, + }); + const lightGrayBase64String = + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQYV2M4ceLEfwAIDANYMDoQswAAAABJRU5ErkJggg=='; + writeBase64Image({ + outputPath: lightGrayPath, + base64String: lightGrayBase64String, + }); + }); + + afterEach(() => { + fs.rmdirSync(workingDirectory, { recursive: true }); + }); + + it('creates diff files in deeply nested directories', async () => { + const config = {}; + const looksSameDiffer = createLooksSameDiffer(config); + + const diffPath = path.join( + workingDirectory, + 'deeply', + 'nested', + 'diff.png' + ); + const tolerance = 0; + await looksSameDiffer(darkGrayPath, lightGrayPath, diffPath, tolerance); + + expect(fs.existsSync(diffPath)).toEqual(true); + }); +});