Skip to content

Commit

Permalink
fix(config): properly translate user-agent
Browse files Browse the repository at this point in the history
This config item is a bit of a unique one, as it can be set as a
template, but then gets translated and also populated to
process.env.npm_config_user_agent.  This adds that logic back in during
the flattening.

PR-URL: #2964
Credit: @wraithgar
Close: #2964
Reviewed-by: @nlf
  • Loading branch information
wraithgar authored and ruyadorno committed Mar 25, 2021
1 parent 97b4152 commit 1415b4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,11 @@ define('user-agent', {
.replace(/\{arch\}/gi, process.arch)
.replace(/\{ci\}/gi, ciName ? `ci/${ciName}` : '')
.trim()
// user-agent is a unique kind of config item that gets set from a template
// and ends up translated. Because of this, the normal "should we set this
// to process.env also doesn't work
obj[key] = flatOptions.userAgent
process.env.npm_config_user_agent = flatOptions.userAgent
},
})

Expand Down
6 changes: 6 additions & 0 deletions test/lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,10 +733,16 @@ t.test('user-agent', t => {
`${process.platform} ${process.arch}`
definitions['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, expectNoCI)
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')

obj['ci-name'] = 'foo'
obj['user-agent'] = definitions['user-agent'].default
const expectCI = `${expectNoCI} ci/foo`
definitions['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, expectCI)
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
t.end()
})

Expand Down

0 comments on commit 1415b4b

Please sign in to comment.