Skip to content

Commit

Permalink
fix(config): flatten savePrefix properly
Browse files Browse the repository at this point in the history
It needs to take save-exact into consideration

Closes: #2932

PR-URL: #2937
Credit: @wraithgar
Close: #2937
Reviewed-by: @ruyadorno
  • Loading branch information
wraithgar authored and ruyadorno committed Mar 24, 2021
1 parent 543b0e3 commit dce4960
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/utils/config/definitions.js
Expand Up @@ -1581,7 +1581,9 @@ define('save-prefix', {
\`npm config set save-prefix='~'\` it would be set to \`~1.2.3\` which
only allows patch upgrades.
`,
flatten,
flatten (key, obj, flatOptions) {
flatOptions.savePrefix = obj['save-exact'] ? '' : obj['save-prefix']
},
})

define('save-prod', {
Expand Down
15 changes: 15 additions & 0 deletions test/lib/utils/config/definitions.js
Expand Up @@ -695,3 +695,18 @@ t.test('user-agent', t => {
t.equal(flat.userAgent, expectCI)
t.end()
})

t.test('save-prefix', t => {
const obj = {
'save-exact': true,
'save-prefix': '~1.2.3',

This comment has been minimized.

Copy link
@isaacs

isaacs Mar 24, 2021

Contributor

Shouldn't save-prefix just be something like ~ or ^?

}
const flat = {}
definitions['save-prefix']
.flatten('save-prefix', { ...obj, 'save-exact': true }, flat)
t.strictSame(flat, { savePrefix: '' })
definitions['save-prefix']
.flatten('save-prefix', { ...obj, 'save-exact': false }, flat)
t.strictSame(flat, { savePrefix: '~1.2.3' })
t.end()
})
6 changes: 6 additions & 0 deletions test/lib/utils/config/flatten.js
Expand Up @@ -6,6 +6,8 @@ delete process.env.NODE
process.execPath = '/path/to/node'

const obj = {
'save-exact': true,
'save-prefix': 'ignored',
'save-dev': true,
'@foobar:registry': 'https://foo.bar.com/',
'//foo.bar.com:_authToken': 'foobarbazquuxasdf',
Expand All @@ -15,6 +17,8 @@ const obj = {
const flat = flatten(obj)
t.strictSame(flat, {
saveType: 'dev',
saveExact: true,
savePrefix: '',
'@foobar:registry': 'https://foo.bar.com/',
'//foo.bar.com:_authToken': 'foobarbazquuxasdf',
npmBin: '/path/to/npm',
Expand All @@ -26,6 +30,8 @@ t.strictSame(flat, {
process.env.NODE = '/usr/local/bin/node.exe'
flatten({ 'save-dev': false }, flat)
t.strictSame(flat, {
saveExact: true,
savePrefix: '',
'@foobar:registry': 'https://foo.bar.com/',
'//foo.bar.com:_authToken': 'foobarbazquuxasdf',
npmBin: '/path/to/npm',
Expand Down

0 comments on commit dce4960

Please sign in to comment.