Skip to content

Commit 54087b0

Browse files
committed
fix(config): user-agent properly shows ci
The way we were flattening user-agent back into itself meant that any values that were dependent on other config items would never be seen, since we have to re-flatten the item for each one it depends on. We also weren't re-flattening the user-agent when setting workspaces or workspace, which were things that affected the final result. This does change the main config value of `user-agent` but not the flattened one. We are not using the main config value anywhere (which is correct).
1 parent e4a5218 commit 54087b0

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

lib/utils/config/definitions.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -2053,10 +2053,14 @@ define('user-agent', {
20532053
.replace(/\{workspaces\}/gi, inWorkspaces)
20542054
.replace(/\{ci\}/gi, ciName ? `ci/${ciName}` : '')
20552055
.trim()
2056+
2057+
// We can't clobber the original or else subsequent flattening will fail
2058+
// (i.e. when we change the underlying config values)
2059+
// obj[key] = flatOptions.userAgent
2060+
20562061
// user-agent is a unique kind of config item that gets set from a template
20572062
// and ends up translated. Because of this, the normal "should we set this
20582063
// to process.env also doesn't work
2059-
obj[key] = flatOptions.userAgent
20602064
process.env.npm_config_user_agent = flatOptions.userAgent
20612065
},
20622066
})
@@ -2140,6 +2144,9 @@ define('workspace', {
21402144
a workspace which does not yet exist, to create the folder and set it
21412145
up as a brand new workspace within the project.
21422146
`,
2147+
flatten: (key, obj, flatOptions) => {
2148+
definitions['user-agent'].flatten('user-agent', obj, flatOptions)
2149+
},
21432150
})
21442151

21452152
define('workspaces', {
@@ -2151,6 +2158,9 @@ define('workspaces', {
21512158
Enable running a command in the context of **all** the configured
21522159
workspaces.
21532160
`,
2161+
flatten: (key, obj, flatOptions) => {
2162+
definitions['user-agent'].flatten('user-agent', obj, flatOptions)
2163+
},
21542164
})
21552165

21562166
define('yes', {

tap-snapshots/test/lib/config.js.test.cjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ exports[`test/lib/config.js TAP config list --json > output matches snapshot 1`]
146146
"unicode": false,
147147
"update-notifier": true,
148148
"usage": false,
149-
"user-agent": "npm/{NPM-VERSION} node/{NODE-VERSION} {PLATFORM} {ARCH} workspaces/false",
149+
"user-agent": "npm/{npm-version} node/{node-version} {platform} {arch} workspaces/{workspaces} {ci}",
150150
"version": false,
151151
"versions": false,
152152
"viewer": "{VIEWER}",
@@ -296,7 +296,7 @@ umask = 0
296296
unicode = false
297297
update-notifier = true
298298
usage = false
299-
user-agent = "npm/{NPM-VERSION} node/{NODE-VERSION} {PLATFORM} {ARCH} workspaces/false"
299+
user-agent = "npm/{npm-version} node/{node-version} {platform} {arch} workspaces/{workspaces} {ci}"
300300
; userconfig = "{HOME}/.npmrc" ; overridden by cli
301301
version = false
302302
versions = false

test/lib/utils/config/definitions.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -747,15 +747,15 @@ t.test('user-agent', t => {
747747
definitions['user-agent'].flatten('user-agent', obj, flat)
748748
t.equal(flat.userAgent, expectNoCI)
749749
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
750-
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
750+
t.not(obj['user-agent'], flat.userAgent, 'config user-agent template is not translated')
751751

752752
obj['ci-name'] = 'foo'
753753
obj['user-agent'] = definitions['user-agent'].default
754754
const expectCI = `${expectNoCI} ci/foo`
755755
definitions['user-agent'].flatten('user-agent', obj, flat)
756756
t.equal(flat.userAgent, expectCI)
757757
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
758-
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
758+
t.not(obj['user-agent'], flat.userAgent, 'config user-agent template is not translated')
759759

760760
delete obj['ci-name']
761761
obj.workspaces = true
@@ -764,15 +764,15 @@ t.test('user-agent', t => {
764764
definitions['user-agent'].flatten('user-agent', obj, flat)
765765
t.equal(flat.userAgent, expectWorkspaces)
766766
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
767-
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
767+
t.not(obj['user-agent'], flat.userAgent, 'config user-agent template is not translated')
768768

769769
delete obj.workspaces
770770
obj.workspace = ['foo']
771771
obj['user-agent'] = definitions['user-agent'].default
772772
definitions['user-agent'].flatten('user-agent', obj, flat)
773773
t.equal(flat.userAgent, expectWorkspaces)
774774
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
775-
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
775+
t.not(obj['user-agent'], flat.userAgent, 'config user-agent template is not translated')
776776
t.end()
777777
})
778778

@@ -853,3 +853,29 @@ t.test('package-lock-only', t => {
853853
t.strictSame(flat, { packageLock: false, packageLockOnly: false })
854854
t.end()
855855
})
856+
857+
t.test('workspaces', t => {
858+
const obj = {
859+
workspaces: true,
860+
'user-agent': definitions['user-agent'].default,
861+
'npm-version': '1.2.3',
862+
'node-version': '9.8.7',
863+
}
864+
const flat = {}
865+
definitions.workspaces.flatten('workspaces', obj, flat)
866+
t.equal(flat.userAgent, 'npm/1.2.3 node/9.8.7 darwin x64 workspaces/true')
867+
t.end()
868+
})
869+
870+
t.test('workspace', t => {
871+
const obj = {
872+
workspace: ['workspace-a'],
873+
'user-agent': definitions['user-agent'].default,
874+
'npm-version': '1.2.3',
875+
'node-version': '9.8.7',
876+
}
877+
const flat = {}
878+
definitions.workspace.flatten('workspaces', obj, flat)
879+
t.equal(flat.userAgent, 'npm/1.2.3 node/9.8.7 darwin x64 workspaces/true')
880+
t.end()
881+
})

0 commit comments

Comments
 (0)