Skip to content

Commit

Permalink
feat: add archiveRemoteKeys function to remoteActions and archive opt… (
Browse files Browse the repository at this point in the history
#184)

* 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
  • Loading branch information
milesillsley authored and MarkConway94 committed Jun 13, 2019
1 parent 582359f commit e87481f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
44 changes: 44 additions & 0 deletions src/remoteActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -205,5 +248,6 @@ export {
listRemoteKeys,
resolveImagePath,
uploadRemoteKeys,
archiveRemoteKeys,
updateRemotePolicy
};
34 changes: 33 additions & 1 deletion src/remoteActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
listRemoteKeys,
deleteRemoteKeys,
fetchRemoteKeys,
uploadRemoteKeys
uploadRemoteKeys,
archiveRemoteKeys
} from './remoteActions';

jest.mock('fs');
Expand Down Expand Up @@ -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);
});
}
});
});

0 comments on commit e87481f

Please sign in to comment.