Skip to content

Commit

Permalink
feat: move validation related to duplicated plugins configuration (ne…
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Apr 8, 2021
1 parent 3e5c3a3 commit df2e5d5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
7 changes: 0 additions & 7 deletions packages/config/src/merge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const { throwError } = require('./error')
const { groupBy } = require('./utils/group')
const { mergeConfigs } = require('./utils/merge')

Expand Down Expand Up @@ -37,12 +36,6 @@ const mergePluginConfigs = function (plugins) {

// TODO: use deep merging instead
const mergePluginsPair = function (pluginA, pluginB) {
// This most likely indicates that the user forgot that they already added or
// configured a plugin.
if (pluginA.origin === 'config' && pluginB.origin === 'config') {
throwError(`Plugin "${pluginA.package}" must not be specified twice in netlify.toml`)
}

return { ...pluginA, ...pluginB }
}

Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/merge_normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { normalizeConfigCase } = require('./case')
const { normalizeConfig } = require('./normalize')
const { addOrigins } = require('./origin')
const { validateIdenticalPlugins } = require('./validate/identical.js')
const {
validatePreCaseNormalize,
validatePreMergeConfig,
Expand All @@ -19,6 +20,7 @@ const normalizeBeforeConfigMerge = function (config, origin) {
const configA = normalizeConfigCase(config)
validatePreMergeConfig(configA)
const configB = addOrigins(configA, origin)
validateIdenticalPlugins(configB)
return configB
}

Expand Down
24 changes: 24 additions & 0 deletions packages/config/src/validate/identical.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const { throwError } = require('../error')

// Validate that plugin are configured only once per origin
// (`netlify.toml` or UI).
// Exception: context-specific configuration since we allow context-specific
// overrides. This does not validate them since contexts have not been merged
// yet.
const validateIdenticalPlugins = function ({ plugins = [] }) {
plugins.filter(hasIdenticalPlugin).forEach(throwIndenticalPlugin)
}

const hasIdenticalPlugin = function ({ package: packageName, origin }, index, plugins) {
return plugins.slice(index + 1).some((pluginA) => packageName === pluginA.package && origin === pluginA.origin)
}

const throwIndenticalPlugin = function ({ package: packageName, origin }) {
throwError(`Plugin "${packageName}" must not be specified twice in ${ORIGINS[origin]}`)
}

const ORIGINS = { config: 'netlify.toml', ui: 'the app' }

module.exports = { validateIdenticalPlugins }
7 changes: 7 additions & 0 deletions packages/config/tests/validate/snapshots/tests.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,13 @@ Generated by [AVA](https://ava.li).
`When resolving config file packages/config/tests/validate/fixtures/plugins_duplicate/netlify.toml:␊
Plugin "./plugin.js" must not be specified twice in netlify.toml`

## plugins: do not allow duplicates in the UI

> Snapshot 1
`When resolving config:␊
Plugin "test" must not be specified twice in the app`

## plugins: not array

> Snapshot 1
Expand Down
Binary file modified packages/config/tests/validate/snapshots/tests.js.snap
Binary file not shown.
5 changes: 5 additions & 0 deletions packages/config/tests/validate/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ test('plugins: do not allow duplicates', async (t) => {
await runFixture(t, 'plugins_duplicate')
})

test('plugins: do not allow duplicates in the UI', async (t) => {
const defaultConfig = JSON.stringify({ plugins: [{ package: 'test' }, { package: 'test' }] })
await runFixture(t, 'empty', { flags: { defaultConfig } })
})

test('plugins.any: unknown property', async (t) => {
await runFixture(t, 'plugins_unknown')
})
Expand Down

0 comments on commit df2e5d5

Please sign in to comment.