feat: allow to define plugin options globally

This commit is contained in:
Pierre Vanduynslager
2017-12-22 14:22:30 -05:00
parent d28b7e3e07
commit f707b1a90a
6 changed files with 64 additions and 35 deletions
+19 -13
View File
@@ -10,27 +10,27 @@ test.beforeEach(t => {
});
test('Normalize and load plugin from string', t => {
const plugin = normalize('verifyConditions', './test/fixtures/plugin-noop', t.context.logger);
const plugin = normalize('verifyConditions', {}, './test/fixtures/plugin-noop', t.context.logger);
t.is(typeof plugin, 'function');
t.deepEqual(t.context.log.args[0], ['Load plugin %s from %s', 'verifyConditions', './test/fixtures/plugin-noop']);
});
test('Normalize and load plugin from object', t => {
const plugin = normalize('publish', {path: './test/fixtures/plugin-noop'}, t.context.logger);
const plugin = normalize('publish', {}, {path: './test/fixtures/plugin-noop'}, t.context.logger);
t.is(typeof plugin, 'function');
t.deepEqual(t.context.log.args[0], ['Load plugin %s from %s', 'publish', './test/fixtures/plugin-noop']);
});
test('Normalize and load plugin from function', t => {
const plugin = normalize('', () => {}, t.context.logger);
const plugin = normalize('', {}, () => {}, t.context.logger);
t.is(typeof plugin, 'function');
});
test('Normalize and load plugin that retuns multiple functions', t => {
const plugin = normalize('verifyConditions', './test/fixtures/multi-plugin', t.context.logger);
const plugin = normalize('verifyConditions', {}, './test/fixtures/multi-plugin', t.context.logger);
t.is(typeof plugin, 'function');
t.deepEqual(t.context.log.args[0], ['Load plugin %s from %s', 'verifyConditions', './test/fixtures/multi-plugin']);
@@ -38,7 +38,7 @@ test('Normalize and load plugin that retuns multiple functions', t => {
test('Wrap plugin in a function that validate the output of the plugin', async t => {
const pluginFunction = stub().resolves(1);
const plugin = normalize('', pluginFunction, t.context.logger, {
const plugin = normalize('', {}, pluginFunction, t.context.logger, {
validator: output => output === 1,
message: 'The output must be 1.',
});
@@ -50,13 +50,14 @@ test('Wrap plugin in a function that validate the output of the plugin', async t
t.is(error.message, 'The output must be 1. Received: 2');
});
test('Plugin is called with "pluginConfig" (omitting "path") and input', async t => {
test('Plugin is called with "pluginConfig" (omitting "path", adding global config) and input', async t => {
const pluginFunction = stub().resolves();
const conf = {path: pluginFunction, conf: 'confValue'};
const plugin = normalize('', conf, t.context.logger);
const globalConf = {global: 'globalValue'};
const plugin = normalize('', globalConf, conf, t.context.logger);
await plugin('param');
t.true(pluginFunction.calledWith({conf: 'confValue'}, 'param'));
t.true(pluginFunction.calledWith({conf: 'confValue', global: 'globalValue'}, 'param'));
});
test('Prevent plugins to modify "pluginConfig"', async t => {
@@ -64,10 +65,12 @@ test('Prevent plugins to modify "pluginConfig"', async t => {
pluginConfig.conf.subConf = 'otherConf';
});
const conf = {path: pluginFunction, conf: {subConf: 'originalConf'}};
const plugin = normalize('', conf, t.context.logger);
const globalConf = {globalConf: {globalSubConf: 'originalGlobalConf'}};
const plugin = normalize('', globalConf, conf, t.context.logger);
await plugin();
t.is(conf.conf.subConf, 'originalConf');
t.is(globalConf.globalConf.globalSubConf, 'originalGlobalConf');
});
test('Prevent plugins to modify its input', async t => {
@@ -75,7 +78,7 @@ test('Prevent plugins to modify its input', async t => {
options.param.subParam = 'otherParam';
});
const input = {param: {subParam: 'originalSubParam'}};
const plugin = normalize('', pluginFunction, t.context.logger);
const plugin = normalize('', {}, pluginFunction, t.context.logger);
await plugin(input);
t.is(input.param.subParam, 'originalSubParam');
@@ -89,7 +92,7 @@ test('Return noop if the plugin is not defined', t => {
test('Always pass a defined "pluginConfig" for plugin defined with string', async t => {
// Call the normalize function with the path of a plugin that returns its config
const plugin = normalize('', './test/fixtures/plugin-result-config', t.context.logger);
const plugin = normalize('', {}, './test/fixtures/plugin-result-config', t.context.logger);
const pluginResult = await plugin();
t.deepEqual(pluginResult.pluginConfig, {});
@@ -97,14 +100,17 @@ test('Always pass a defined "pluginConfig" for plugin defined with string', asyn
test('Always pass a defined "pluginConfig" for plugin defined with path', async t => {
// Call the normalize function with the path of a plugin that returns its config
const plugin = normalize('', {path: './test/fixtures/plugin-result-config'}, t.context.logger);
const plugin = normalize('', {}, {path: './test/fixtures/plugin-result-config'}, t.context.logger);
const pluginResult = await plugin();
t.deepEqual(pluginResult.pluginConfig, {});
});
test('Throws an error if the plugin return an object without the expected plugin function', t => {
const error = t.throws(() => normalize('inexistantPlugin', './test/fixtures/multi-plugin', t.context.logger), Error);
const error = t.throws(
() => normalize('inexistantPlugin', {}, './test/fixtures/multi-plugin', t.context.logger),
Error
);
t.is(
error.message,
+15
View File
@@ -48,6 +48,21 @@ test('Use default when only options are passed for a single plugin', t => {
t.is(typeof plugins.analyzeCommits, 'function');
});
test('Merge global options with plugin options', async t => {
const plugins = getPlugins(
{
globalOpt: 'global',
otherOpt: 'globally-defined',
getLastRelease: {path: './test/fixtures/plugin-result-config', localOpt: 'local', otherOpt: 'locally-defined'},
},
t.context.logger
);
const result = await plugins.getLastRelease();
t.deepEqual(result.pluginConfig, {localOpt: 'local', globalOpt: 'global', otherOpt: 'locally-defined'});
});
test('Throw an error if plugin configuration is missing a path for plugin pipeline', t => {
const error = t.throws(() => getPlugins({verifyConditions: {}}, t.context.logger), Error);