Skip to content

Commit

Permalink
fix: avoid overwriting the default values of options with undefined (#…
Browse files Browse the repository at this point in the history
…1018)

* fix(conventional-changelog-core): avoid overwriting the default values of options with undefined

* test(conventional-changelog-core): add tests for merge-config

* refactor(conventional-changelog-core): optimize omitUndefinedValueProps to one loop
  • Loading branch information
ryamaguchi0220 committed Jun 28, 2023
1 parent 11195f2 commit 71b0c40
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/conventional-changelog-core/lib/merge-config.js
Expand Up @@ -53,10 +53,27 @@ function guessNextTag (previousTag, version) {
return version
}

function omitUndefinedValueProps (obj) {
if (!obj) {
return {}
}

const omittedObj = {}

for (const key in obj) {
if (obj[key] !== undefined) {
omittedObj[key] = obj[key]
}
}

return omittedObj
}

async function mergeConfig (options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) {
let configPromise
let pkgPromise

options = omitUndefinedValueProps(options)
context = context || {}
gitRawCommitsOpts = gitRawCommitsOpts || {}
gitRawExecOpts = gitRawExecOpts || {}
Expand Down
61 changes: 61 additions & 0 deletions packages/conventional-changelog-core/test/merge-config.spec.js
@@ -0,0 +1,61 @@
'use strict'
const mergeConfig = require('../lib/merge-config')
const expect = require('chai').expect
const describe = require('mocha').describe

const defaultOptions = {
append: false,
releaseCount: 1,
skipUnstable: false,
lernaPackage: null,
outputUnreleased: true
}

describe('merge-config', function () {
it('should return passed options', async function () {
const options = {
append: true,
releaseCount: 0,
skipUnstable: true,
debug: function () {},
warn: function () {},
transform: function () {},
lernaPackage: 'foo',
tagPrefix: 'bar',
outputUnreleased: true,
pkg: {
path: 'baz',
transform: function () {}
}
}
const config = await mergeConfig(options)
expect(config.options).to.deep.include(options)
})

it('should return default options if empty options is passed', async function () {
const { options } = await mergeConfig({})
expect(options).to.include(defaultOptions)
})

it('should return default options when no options is passed', async function () {
const { options } = await mergeConfig()
expect(options).to.include(defaultOptions)
})

it('should return default options when null is passed', async function () {
const { options } = await mergeConfig(null)
expect(options).to.include(defaultOptions)
})

it('should return default options when undefined value is passed', async function () {
const options = {
append: undefined,
releaseCount: undefined,
skipUnstable: undefined,
lernaPackage: undefined,
outputUnreleased: undefined
}
const config = await mergeConfig(options)
expect(config.options).to.include(defaultOptions)
})
})

0 comments on commit 71b0c40

Please sign in to comment.