Skip to content

Commit

Permalink
feat: exit process if config is bad
Browse files Browse the repository at this point in the history
  • Loading branch information
L0wry committed Jun 26, 2018
1 parent 2eaaa9c commit 29e1d65
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
25 changes: 21 additions & 4 deletions src/configValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function isValid(missingConfigFields) {

return true;
}

function isRemoteConfigValid(config) {
const missingConfigFields = ['remoteBucketName', 'remoteRegion'].filter(
field => !config[field]
Expand All @@ -23,6 +24,7 @@ function isRemoteConfigValid(config) {

return isValid(missingConfigFields);
}

function isLocalConfigValid(config) {
const missingConfigFields = [
'gridUrl',
Expand All @@ -36,11 +38,26 @@ function isLocalConfigValid(config) {
return isValid(missingConfigFields);
}

const validateConfig = (config, isRemote) => {
if (isRemote) isRemoteConfigValid(config);
const validateConfig = (config, isRemote) =>
new Promise(resolve => {
let isRemoteConfigCorrect = true;

if (isRemote) {
isRemoteConfigCorrect = isRemoteConfigValid(config);
}

isLocalConfigValid(config);
};
if (isLocalConfigValid(config) && isRemoteConfigCorrect) {
logger.info('configValidator', 'Config validated ✅');
resolve();
}

logger.info(
'config Validator',
'❗️ Please update your config to be valid \n Exiting Aye Spy'
);
process.exitCode = 1;
process.exit();
});

export default validateConfig;
export { isLocalConfigValid, isRemoteConfigValid };
50 changes: 47 additions & 3 deletions src/configValidator.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isLocalConfigValid,isRemoteConfigValid } from './configValidator';
/* globals expect jest */

import validateConfig, {
isLocalConfigValid,
isRemoteConfigValid
} from './configValidator';

describe('The Config Validator', () => {
it('local config returns false for invalid configs', () => {
Expand Down Expand Up @@ -33,12 +38,51 @@ describe('The Config Validator', () => {
});

it('remote config returns true for valid configs', () => {
process.env.AWS_SECRET_ACCESS_KEY = 'test'
process.env.AWS_ACCESS_KEY_ID = 'test'
process.env.AWS_SECRET_ACCESS_KEY = 'test';
process.env.AWS_ACCESS_KEY_ID = 'test';
const config = {
remoteBucketName: 'aye-spy',
remoteRegion: 'eu-west-1'
};
expect(isRemoteConfigValid(config)).toBe(true);
});

it('exits if the config is invalid', () => {
const invalidConfig = {
gridUrl: 'http://selenium.com:4444/wd/hub',
baseline: './e2eTests/generateHtmlReport/baseline',
latest: './e2eTests/generateHtmlReport/latest',
generatedDiffs: './e2eTests/generateHtmlReport/generatedDiffs',
report: './e2eTests/generateHtmlReport/reports',
scenarios: [
{
url: 'http:/google.com/',
label: 'homepage'
}
]
};

process.exit = jest.fn();
validateConfig(invalidConfig, true);
expect(process.exit.mock.calls.length).toBe(1);
});

it('resolves a correct config', done => {
const config = {
remoteBucketName: 'test',
remoteRegion: 'test',
gridUrl: 'http://selenium.com:4444/wd/hub',
baseline: './e2eTests/generateHtmlReport/baseline',
latest: './e2eTests/generateHtmlReport/latest',
generatedDiffs: './e2eTests/generateHtmlReport/generatedDiffs',
report: './e2eTests/generateHtmlReport/reports',
scenarios: [
{
url: 'http:/google.com/',
label: 'homepage'
}
]
};
validateConfig(config, true).then(done);
});
});

0 comments on commit 29e1d65

Please sign in to comment.