Skip to content

Commit

Permalink
Factor out config validation into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Sep 17, 2021
1 parent 36777d0 commit 6d939ec
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 49 deletions.
31 changes: 15 additions & 16 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,19 @@ if (opts['server-option']) {
opts.server_options = opts['server-option'];
}

(async () => {
if (opts._.length) {
if (opts._[0] === '-') {
opts.spec = process.stdin;
} else {
opts.spec = opts._;
}
if (opts._.length) {
if (opts._[0] === '-') {
opts.spec = process.stdin;
} else {
opts.spec = opts._;
}
delete opts._;
try {
const { exit_code } = await mochify(opts);
process.exitCode = exit_code; // eslint-disable-line require-atomic-updates
} catch (e) {
console.error(e.stack);
process.exitCode = 1; // eslint-disable-line require-atomic-updates
}
})();
}
delete opts._;
mochify(opts)
.catch((err) => {
console.error(err.stack);
return { exit_code: 1 };
})
.then(({ exit_code }) => {
process.exitCode = exit_code;
});
6 changes: 6 additions & 0 deletions mochify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { readFile } = require('fs').promises;
const { loadConfig } = require('./lib/load-config');
const { validateConfig } = require('./lib/validate-config');
const { setupClient } = require('./lib/setup-client');
const { createMochaRunner } = require('./lib/mocha-runner');
const { resolveBundle } = require('./lib/resolve-bundle');
Expand All @@ -14,6 +15,11 @@ exports.mochify = mochify;
async function mochify(options = {}) {
const config = await loadConfig(options);

const validation_error = validateConfig(config);
if (validation_error) {
throw validation_error;
}

// Create runner early to verify the reporter exists:
const mocha_runner = createMochaRunner(config.reporter || 'spec');
const { mochifyDriver } = resolveMochifyDriver(config.driver);
Expand Down
25 changes: 2 additions & 23 deletions mochify/lib/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@ async function loadConfig(options) {

const default_config_promise = explorer.search();

let merged;
if (options.config) {
const specified = await explorer.load(options.config);
const config = Object.assign(specified.config, options);
merged = await mergeWithDefault(default_config_promise, config);
} else {
merged = await mergeWithDefault(default_config_promise, options);
return mergeWithDefault(default_config_promise, config);
}

const validation_error = validate(merged);
if (validation_error) {
throw validation_error;
}
return merged;
return mergeWithDefault(default_config_promise, options);
}

async function mergeWithDefault(default_config_promise, config) {
Expand All @@ -35,17 +28,3 @@ async function mergeWithDefault(default_config_promise, config) {
}
return config;
}

function validate(config) {
if (config.esm && config.bundle) {
return new Error('`esm` cannot be used in conjunction with `bundle`');
}
if (
config.bundle &&
typeof config.spec === 'object' &&
typeof config.spec.pipe === 'function'
) {
return new Error('`bundle` cannot be used when `spec` is a stream.');
}
return null;
}
10 changes: 0 additions & 10 deletions mochify/lib/load-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,4 @@ describe('mochify/lib/load-config', () => {
reporter: 'nyan'
});
});

it('rejects on invalid configuration', async () => {
setDefaultConfig({ esm: true });
setSpecifiedConfig({ bundle: 'browserify' });
const loadConfig = requireLoadConfig();

const promise = loadConfig({ config: 'some.config.js' });

await assert.rejects(promise);
});
});
17 changes: 17 additions & 0 deletions mochify/lib/validate-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

exports.validateConfig = validateConfig;

function validateConfig(config) {
if (config.esm && config.bundle) {
return new Error('`esm` cannot be used in conjunction with `bundle`');
}
if (
config.bundle &&
typeof config.spec === 'object' &&
typeof config.spec.pipe === 'function'
) {
return new Error('`bundle` cannot be used when `spec` is a stream.');
}
return null;
}
36 changes: 36 additions & 0 deletions mochify/lib/validate-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const fs = require('fs');
const { assert } = require('@sinonjs/referee-sinon');
const { validateConfig } = require('./validate-config');

describe('mochify/lib/validate-config', () => {
it('returns an error when esm and bundle are given', () => {
assert.isError(
validateConfig({ esm: true, bundle: 'browserify', spec: './test.js' })
);
});

it('returns an error when bundle and a stream spec are given', () => {
assert.isError(
validateConfig({
bundle: 'browserify',
spec: fs.createReadStream(__filename)
})
);
});

it('returns null on an empty config', () => {
assert.isNull(validateConfig({}));
});

it('returns null on a valid config', () => {
assert.isNull(
validateConfig({
bundle: 'browserify -t babelify',
spec: './test.js',
driver: 'puppeteer'
})
);
});
});

0 comments on commit 6d939ec

Please sign in to comment.