Skip to content

Commit

Permalink
fix(load): resolve plugins from extended configs (#1976)
Browse files Browse the repository at this point in the history
* test(load): adds test for loading plugins from an extended config

* fix(load): loads plugins from extended configs

* refactor: clean up a loading test

* test(load): adds test for including a shareable config with a local plugin

* test: adds more realistic test-scenarios

* Update @commitlint/load/src/load.ts

Co-authored-by: Cedric van Putten <me@bycedric.com>

Co-authored-by: Julius Celik <julius.celik@digitalroute.com>
Co-authored-by: Cedric van Putten <me@bycedric.com>
  • Loading branch information
3 people committed Jun 29, 2020
1 parent 50ae7c1 commit d0f0eb9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 0 deletions.
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./config-with-local-plugin'],
};
@@ -0,0 +1,21 @@
module.exports = {
extends: [],
plugins: [{
rules: {
'hello-world-rule': ({ subject }) => {
const HELLO_WORLD = 'Hello World';
return [
subject.includes(HELLO_WORLD),
`Your subject should contain ${HELLO_WORLD} message`
];
},
'is-positive': ({ subject }) => {
const POSITIVE_EMOJI = ':)';
return [
subject.includes(POSITIVE_EMOJI),
`Your subject should contain ${POSITIVE_EMOJI} message`
];
}
}
}]
};
@@ -0,0 +1 @@
module.exports = require('./commitlint.config.js');
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./config-with-plugins'],
};
@@ -0,0 +1,3 @@
module.exports = {
plugins: ['example', '@scope/example']
};
@@ -0,0 +1 @@
module.exports = require('./commitlint.config.js');
26 changes: 26 additions & 0 deletions @commitlint/load/src/load.test.ts
Expand Up @@ -95,6 +95,32 @@ test('plugins should be loaded from config', async () => {
});
});

test('plugins should be loaded from shareable config', async () => {
const cwd = await gitBootstrap('fixtures/extends-with-plugins');
const actual = await load({}, {cwd});

expect(actual.plugins).toMatchObject({
example: plugin,
'@scope/example': scopedPlugin
});
});

test('local plugins should be loaded from shareable configs', async () => {
const cwd = await gitBootstrap('fixtures/extends-with-local-plugins');
const actual = await load({}, {cwd});

expect(actual.plugins).toEqual(
expect.objectContaining({
local: {
rules: {
'hello-world-rule': expect.any(Function),
'is-positive': expect.any(Function)
}
}
})
);
});

test('uses seed with parserPreset', async () => {
const cwd = await gitBootstrap('fixtures/parser-preset');
const {parserPreset: actual} = await load(
Expand Down
6 changes: 6 additions & 0 deletions @commitlint/load/src/load.ts
Expand Up @@ -3,6 +3,7 @@ import Path from 'path';
import merge from 'lodash/merge';
import mergeWith from 'lodash/mergeWith';
import pick from 'lodash/pick';
import union from 'lodash/union';
import resolveFrom from 'resolve-from';

import executeRule from '@commitlint/execute-rule';
Expand Down Expand Up @@ -84,6 +85,11 @@ export default async function load(
resolveFrom.silent(base, config.formatter) || config.formatter;
}

// Read plugins from extends
if (Array.isArray(extended.plugins)) {
config.plugins = union(config.plugins, extended.plugins || []);
}

// resolve plugins
if (Array.isArray(config.plugins)) {
config.plugins.forEach(plugin => {
Expand Down

0 comments on commit d0f0eb9

Please sign in to comment.