From e87481f814086640fa3789e04f7d92d575ae1055 Mon Sep 17 00:00:00 2001 From: milesillsley Date: Thu, 13 Jun 2019 10:18:08 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20add=20archiveRemoteKeys=20function=20to?= =?UTF-8?q?=20remoteActions=20and=20archive=20opt=E2=80=A6=20(#184)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add archiveRemoteKeys function to remoteActions and archive option to update baseline command * fix: typo in archive option * testing npm link console logs * feat: format date string for archive * revert: date formatting, remove console logs * test: remoteActions archiveRemoteKeys --- src/bin/run.js | 4 +++- src/remoteActions.js | 44 +++++++++++++++++++++++++++++++++++++++ src/remoteActions.test.js | 34 +++++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/bin/run.js b/src/bin/run.js index be25be6..979be83 100644 --- a/src/bin/run.js +++ b/src/bin/run.js @@ -8,7 +8,7 @@ import SnapShotter from '../snapshotter'; import getScreenshots from '../getScreenshots'; import updateBaselineShots from '../updateBaselineShots'; import { generateLocalReport, generateRemoteReport } from '../generateReport'; -import { uploadRemoteKeys } from '../remoteActions'; +import { uploadRemoteKeys, archiveRemoteKeys } from '../remoteActions'; import { createBucket, createComparisons, @@ -80,6 +80,7 @@ program .option('c, --config [config]', 'Path to your config') .option('--run [optional]', 'Filter scenarios based on label name') .option('r, --remote', 'Upload new baseline to remote storage') + .option('a, --archive', 'add updated baseline images to an archive folder') .action(async options => { try { const config = require(path.resolve(options.config)); // eslint-disable-line import/no-dynamic-require @@ -96,6 +97,7 @@ program logger.error('run', error); }); if (options.remote) await uploadRemoteKeys('baseline', config); + if (options.archive) await archiveRemoteKeys('baseline', config); } catch (err) { handleError(err); } diff --git a/src/remoteActions.js b/src/remoteActions.js index 9800b7a..31befba 100755 --- a/src/remoteActions.js +++ b/src/remoteActions.js @@ -197,6 +197,49 @@ const uploadRemoteKeys = async (key, config) => { ).catch(err => logger.error('remote-actions', err)); }; +const archiveRemoteKeys = async (key, config) => { + const imageDir = await resolveImagePath(key, config); + AWS.config.update({ + region: config.remoteRegion + }); + const s3 = new AWS.S3(); + const files = fs.readdirSync(imageDir).map(file => `${imageDir}/${file}`); + const date = new Date(); + + if (files.length !== 0) { + logger.info( + 'remote-actions', + `${files.length} images to be archived to bucket: archive/${date}/${key}` + ); + } + + return Promise.all( + files.map(file => { + const fileStream = fs.createReadStream(file); + + fileStream.on('error', err => { + logger.error('remote-actions', err); + }); + + const dir = `${config.browser}/default/archive/${date}`; + + logger.info( + 'remote-actions', + `Uploading to S3: ${dir}/${key}/${path.basename(file)}` + ); + + const uploadParams = { + Bucket: config.remoteBucketName, + Key: `${dir}/${key}/${path.basename(file)}`, + Body: fileStream, + ContentType: 'image/png' + }; + + return s3.putObject(uploadParams).promise(); + }) + ).catch(err => logger.error('remote-actions', err)); +}; + export { createRemote, deleteRemoteKeys, @@ -205,5 +248,6 @@ export { listRemoteKeys, resolveImagePath, uploadRemoteKeys, + archiveRemoteKeys, updateRemotePolicy }; diff --git a/src/remoteActions.test.js b/src/remoteActions.test.js index c81cb29..9d4bd9e 100644 --- a/src/remoteActions.test.js +++ b/src/remoteActions.test.js @@ -5,7 +5,8 @@ import { listRemoteKeys, deleteRemoteKeys, fetchRemoteKeys, - uploadRemoteKeys + uploadRemoteKeys, + archiveRemoteKeys } from './remoteActions'; jest.mock('fs'); @@ -173,4 +174,35 @@ describe('Remote interactions', () => { }); } }); + + it('archives the remote Keys', async () => { + const mockedDate = new Date(2017, 11, 10); + global.Date = jest.fn(() => mockedDate); + const keyValue = { + baseline: + 'chrome/default/archive/Sun Dec 10 2017 00:00:00 GMT+0000 (Greenwich Mean Time)/baseline/mock/resolved/path/file1' + }; + + const config = { + remoteRegion: 'region', + browser: 'chrome', + baseline: './e2eTests/baseline', + latest: './e2eTests/latest', + generatedDiffs: './e2eTests/generatedDiffs', + branch: 'default' + }; + const file = ['file1']; + fs.readdirSync.mockReturnValue(file); + fs.createReadStream.mockReturnValue({ + on: () => {} + }); + for (const [key, value] of Object.entries(keyValue)) { + await archiveRemoteKeys(key, config) + .then(promises => promises[0]) + .then(obj => obj.Key) + .then(name => { + expect(name).toEqual(value); + }); + } + }); });