Skip to content

Commit b38f68a

Browse files
committed
ensure npm-command header is sent properly
Reported by @lumaxis
1 parent d159a31 commit b38f68a

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

lib/utils/flat-options.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ const flatOptions = npm => npm.flatOptions || Object.freeze({
5151
projectScope: npm.projectScope,
5252
npmVersion: npm.version,
5353
nodeVersion: npm.config.get('node-version'),
54-
npmCommand: npm.command,
54+
// npm.command is not set until AFTER flatOptions are defined
55+
// so we need to make this a getter.
56+
get npmCommand () {
57+
return npm.command
58+
},
5559

5660
tmp: npm.tmp,
5761
cache: join(npm.config.get('cache'), '_cacache'),

tap-snapshots/test-lib-utils-flat-options.js-TAP.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Object {
6767
"nodeBin": "/path/to/some/node",
6868
"nodeVersion": "1.2.3",
6969
"npmBin": "/path/to/npm/bin.js",
70-
"npmCommand": "test",
70+
"npmCommand": null,
7171
"npmSession": "12345",
7272
"npmVersion": "7.6.5",
7373
"offline": "offline",

test/lib/utils/flat-options.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Mocknpm {
1717
this.color = true
1818
this.projectScope = '@npmcli'
1919
this.tmp = '/tmp'
20-
this.command = 'test'
20+
this.command = null
2121
this.globalPrefix = '/usr/local'
2222
this.localPrefix = '/path/to/npm/cli'
2323
this.prefix = this.localPrefix
@@ -138,6 +138,9 @@ t.test('basic', t => {
138138
cache: generatedFlat.cache.replace(/\\/g, '/')
139139
}
140140
t.matchSnapshot(clean, 'flat options')
141+
t.equal(generatedFlat.npmCommand, null, 'command not set yet')
142+
npm.command = 'view'
143+
t.equal(generatedFlat.npmCommand, 'view', 'command updated via getter')
141144
t.equal(generatedFlat.npmBin, require.main.filename)
142145
// test the object is frozen
143146
generatedFlat.newField = 'asdf'

0 commit comments

Comments
 (0)