1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
const t = require('tap')
let LOAD_ERROR = null
const npmock = {
version: '99.99.99',
load: cb => cb(LOAD_ERROR),
argv: [],
config: {
settings: {},
get: (k) => npmock.config.settings[k],
set: (k, v) => {
npmock.config.settings[k] = v
},
},
commands: {},
}
const unsupportedMock = {
checkForBrokenNode: () => {},
checkForUnsupportedNode: () => {},
}
let errorHandlerCalled = null
const errorHandlerMock = (...args) => {
errorHandlerCalled = args
}
let errorHandlerExitCalled = null
errorHandlerMock.exit = code => {
errorHandlerExitCalled = code
}
const logs = []
const npmlogMock = {
pause: () => logs.push('pause'),
verbose: (...msg) => logs.push(['verbose', ...msg]),
info: (...msg) => logs.push(['info', ...msg]),
}
const requireInject = require('require-inject')
const cli = requireInject.installGlobally('../../lib/cli.js', {
'../../lib/npm.js': npmock,
'../../lib/utils/unsupported.js': unsupportedMock,
'../../lib/utils/error-handler.js': errorHandlerMock,
npmlog: npmlogMock,
})
t.test('print the version, and treat npm_g to npm -g', t => {
const { log } = console
const consoleLogs = []
console.log = (...msg) => consoleLogs.push(msg)
const { argv } = process
const proc = {
argv: ['node', 'npm_g', '-v'],
version: '420.69.lol',
on: () => {},
}
process.argv = proc.argv
npmock.config.settings.version = true
cli(proc)
t.strictSame(npmock.argv, [])
t.strictSame(proc.argv, ['node', 'npm', '-g', '-v'])
t.strictSame(logs, [
'pause',
['verbose', 'cli', ['node', 'npm', '-g', '-v']],
['info', 'using', 'npm@%s', '99.99.99'],
['info', 'using', 'node@%s', '420.69.lol'],
])
t.strictSame(consoleLogs, [['99.99.99']])
t.strictSame(errorHandlerExitCalled, 0)
delete npmock.config.settings.version
process.argv = argv
console.log = log
npmock.argv.length = 0
proc.argv.length = 0
logs.length = 0
consoleLogs.length = 0
errorHandlerExitCalled = null
t.end()
})
t.test('calling with --versions calls npm version with no args', t => {
const { log } = console
const consoleLogs = []
console.log = (...msg) => consoleLogs.push(msg)
const processArgv = process.argv
const proc = {
argv: ['node', 'npm', 'install', 'or', 'whatever', '--versions'],
on: () => {},
}
process.argv = proc.argv
npmock.config.set('versions', true)
t.teardown(() => {
delete npmock.config.settings.versions
process.argv = processArgv
console.log = log
npmock.argv.length = 0
proc.argv.length = 0
logs.length = 0
consoleLogs.length = 0
errorHandlerExitCalled = null
delete npmock.commands.version
})
npmock.commands.version = (args, cb) => {
t.equal(proc.title, 'npm')
t.strictSame(npmock.argv, [])
t.strictSame(proc.argv, ['node', 'npm', 'install', 'or', 'whatever', '--versions'])
t.strictSame(logs, [
'pause',
['verbose', 'cli', ['node', 'npm', 'install', 'or', 'whatever', '--versions']],
['info', 'using', 'npm@%s', '99.99.99'],
['info', 'using', 'node@%s', undefined],
])
t.strictSame(consoleLogs, [])
t.strictSame(errorHandlerExitCalled, null)
t.strictSame(args, [])
t.end()
}
cli(proc)
})
t.test('print usage if -h provided', t => {
const { log } = console
const consoleLogs = []
console.log = (...msg) => consoleLogs.push(msg)
const proc = {
argv: ['node', 'npm', 'asdf'],
on: () => {},
}
npmock.argv = ['asdf']
t.teardown(() => {
console.log = log
npmock.argv.length = 0
proc.argv.length = 0
logs.length = 0
consoleLogs.length = 0
errorHandlerExitCalled = null
delete npmock.commands.help
})
npmock.commands.help = (args, cb) => {
delete npmock.commands.help
t.equal(proc.title, 'npm')
t.strictSame(args, ['asdf'])
t.strictSame(npmock.argv, ['asdf'])
t.strictSame(proc.argv, ['node', 'npm', 'asdf'])
t.strictSame(logs, [
'pause',
['verbose', 'cli', ['node', 'npm', 'asdf']],
['info', 'using', 'npm@%s', '99.99.99'],
['info', 'using', 'node@%s', undefined],
])
t.strictSame(consoleLogs, [])
t.strictSame(errorHandlerExitCalled, null)
t.end()
}
cli(proc)
})
t.test('load error calls error handler', t => {
const er = new Error('poop')
LOAD_ERROR = er
const proc = {
argv: ['node', 'npm', 'asdf'],
on: () => {},
}
cli(proc)
t.strictSame(errorHandlerCalled, [er])
LOAD_ERROR = null
t.end()
})
|