From d0f0eb9fde7efc2dff7a3aad190ded14303d3079 Mon Sep 17 00:00:00 2001 From: Julius Recep Colliander Celik Date: Mon, 29 Jun 2020 07:50:01 +0200 Subject: [PATCH] fix(load): resolve plugins from extended configs (#1976) * 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 Co-authored-by: Julius Celik Co-authored-by: Cedric van Putten --- .../commitlint.config.js | 3 +++ .../commitlint.config.js | 21 +++++++++++++++ .../config-with-local-plugin/index.js | 1 + .../extends-with-plugins/commitlint.config.js | 3 +++ .../config-with-plugins/commitlint.config.js | 3 +++ .../config-with-plugins/index.js | 1 + @commitlint/load/src/load.test.ts | 26 +++++++++++++++++++ @commitlint/load/src/load.ts | 6 +++++ 8 files changed, 64 insertions(+) create mode 100644 @commitlint/load/fixtures/extends-with-local-plugins/commitlint.config.js create mode 100644 @commitlint/load/fixtures/extends-with-local-plugins/config-with-local-plugin/commitlint.config.js create mode 100644 @commitlint/load/fixtures/extends-with-local-plugins/config-with-local-plugin/index.js create mode 100644 @commitlint/load/fixtures/extends-with-plugins/commitlint.config.js create mode 100644 @commitlint/load/fixtures/extends-with-plugins/config-with-plugins/commitlint.config.js create mode 100644 @commitlint/load/fixtures/extends-with-plugins/config-with-plugins/index.js diff --git a/@commitlint/load/fixtures/extends-with-local-plugins/commitlint.config.js b/@commitlint/load/fixtures/extends-with-local-plugins/commitlint.config.js new file mode 100644 index 0000000000..f0c973fd8c --- /dev/null +++ b/@commitlint/load/fixtures/extends-with-local-plugins/commitlint.config.js @@ -0,0 +1,3 @@ +module.exports = { + extends: ['./config-with-local-plugin'], +}; diff --git a/@commitlint/load/fixtures/extends-with-local-plugins/config-with-local-plugin/commitlint.config.js b/@commitlint/load/fixtures/extends-with-local-plugins/config-with-local-plugin/commitlint.config.js new file mode 100644 index 0000000000..2412bcd48a --- /dev/null +++ b/@commitlint/load/fixtures/extends-with-local-plugins/config-with-local-plugin/commitlint.config.js @@ -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` + ]; + } + } + }] +}; diff --git a/@commitlint/load/fixtures/extends-with-local-plugins/config-with-local-plugin/index.js b/@commitlint/load/fixtures/extends-with-local-plugins/config-with-local-plugin/index.js new file mode 100644 index 0000000000..fe16cc487b --- /dev/null +++ b/@commitlint/load/fixtures/extends-with-local-plugins/config-with-local-plugin/index.js @@ -0,0 +1 @@ +module.exports = require('./commitlint.config.js'); diff --git a/@commitlint/load/fixtures/extends-with-plugins/commitlint.config.js b/@commitlint/load/fixtures/extends-with-plugins/commitlint.config.js new file mode 100644 index 0000000000..cf6e89466d --- /dev/null +++ b/@commitlint/load/fixtures/extends-with-plugins/commitlint.config.js @@ -0,0 +1,3 @@ +module.exports = { + extends: ['./config-with-plugins'], +}; diff --git a/@commitlint/load/fixtures/extends-with-plugins/config-with-plugins/commitlint.config.js b/@commitlint/load/fixtures/extends-with-plugins/config-with-plugins/commitlint.config.js new file mode 100644 index 0000000000..1a285b7b1a --- /dev/null +++ b/@commitlint/load/fixtures/extends-with-plugins/config-with-plugins/commitlint.config.js @@ -0,0 +1,3 @@ +module.exports = { + plugins: ['example', '@scope/example'] +}; diff --git a/@commitlint/load/fixtures/extends-with-plugins/config-with-plugins/index.js b/@commitlint/load/fixtures/extends-with-plugins/config-with-plugins/index.js new file mode 100644 index 0000000000..fe16cc487b --- /dev/null +++ b/@commitlint/load/fixtures/extends-with-plugins/config-with-plugins/index.js @@ -0,0 +1 @@ +module.exports = require('./commitlint.config.js'); diff --git a/@commitlint/load/src/load.test.ts b/@commitlint/load/src/load.test.ts index 80e71147d9..1a3e4ba1f8 100644 --- a/@commitlint/load/src/load.test.ts +++ b/@commitlint/load/src/load.test.ts @@ -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( diff --git a/@commitlint/load/src/load.ts b/@commitlint/load/src/load.ts index 63fec9b908..c939e82192 100644 --- a/@commitlint/load/src/load.ts +++ b/@commitlint/load/src/load.ts @@ -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'; @@ -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 => {