Skip to content

Commit

Permalink
Validate config right after merging
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Sep 16, 2021
1 parent c77f2f4 commit 6119c9c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
25 changes: 23 additions & 2 deletions mochify/lib/load-config.js
Expand Up @@ -10,13 +10,20 @@ 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);
return mergeWithDefault(default_config_promise, config);
merged = await mergeWithDefault(default_config_promise, config);
} else {
merged = await mergeWithDefault(default_config_promise, options);
}

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

async function mergeWithDefault(default_config_promise, config) {
Expand All @@ -28,3 +35,17 @@ 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: 10 additions & 0 deletions mochify/lib/load-config.test.js
Expand Up @@ -154,4 +154,14 @@ 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);
});
});
28 changes: 10 additions & 18 deletions mochify/lib/resolve-bundle.js
Expand Up @@ -7,25 +7,8 @@ const { parseArgsStringToArgv } = require('string-argv');
exports.resolveBundle = resolveBundle;

async function resolveBundle(command, files) {
if (command && files) {
throw new Error(
'Cannot use a stream as input when a bundle command is given.'
);
}

if (typeof files === 'object' && typeof files.pipe === 'function') {
return new Promise((resolve, reject) => {
let buf;
files.on('data', (data) => {
buf += data;
});
files.on('error', (err) => {
reject(err);
});
files.on('end', () => {
resolve(buf);
});
});
return bufferStream(files);
}

if (!command) {
Expand All @@ -49,3 +32,12 @@ async function concatFiles(files) {
const buffers = await Promise.all(files.map((file) => fs.readFile(file)));
return Buffer.concat(buffers).toString('utf8');
}

function bufferStream(stream) {
return new Promise((resolve, reject) => {
const bs = [];
stream.on('data', (chunk) => bs.push(chunk));
stream.on('error', (err) => reject(err));
stream.on('end', () => resolve(Buffer.concat(bs).toString()));
});
}
7 changes: 7 additions & 0 deletions mochify/lib/resolve-spec.test.js
@@ -1,5 +1,6 @@
'use strict';

const fs = require('fs');
const proxyquire = require('proxyquire');
const { assert, sinon } = require('@sinonjs/referee-sinon');

Expand Down Expand Up @@ -75,4 +76,10 @@ describe('mochify/lib/resolve-spec', () => {

await assert.rejects(promise, error);
});

it('passes through streams', async () => {
const stream = fs.createReadStream(__filename);
const promise = resolveSpec(stream);
await assert.resolves(promise, stream);
});
});

0 comments on commit 6119c9c

Please sign in to comment.