summaryrefslogtreecommitdiff
path: root/deps/npm/test/lib/help.js
blob: fc4a32e07beaaf164d5cb10ead50713563de70bd (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
const { test } = require('tap')
const requireInject = require('require-inject')
const { EventEmitter } = require('events')

let npmUsageArg = null
const npmUsage = (arg) => {
  npmUsageArg = arg
}

const npmConfig = {
  usage: false,
  viewer: undefined,
  loglevel: undefined,
}

let helpSearchArgs = null
const npm = {
  config: {
    get: (key) => npmConfig[key],
    set: (key, value) => {
      npmConfig[key] = value
    },
    parsedArgv: {
      cooked: [],
    },
  },
  commands: {
    'help-search': (args, cb) => {
      helpSearchArgs = args
      return cb()
    },
    help: {
      usage: 'npm help <term>',
    },
  },
  deref: (cmd) => {},
}

const OUTPUT = []
const output = (msg) => {
  OUTPUT.push(msg)
}

const globDefaults = [
  '/root/man/man1/npm-whoami.1',
  '/root/man/man5/npmrc.5',
  '/root/man/man7/disputes.7',
]

let globErr = null
let globResult = globDefaults
const glob = (p, cb) => {
  return cb(globErr, globResult)
}

let spawnBin = null
let spawnArgs = null
let spawnCode = 0
const spawn = (bin, args) => {
  spawnBin = bin
  spawnArgs = args
  const spawnEmitter = new EventEmitter()
  process.nextTick(() => {
    spawnEmitter.emit('exit', spawnCode)
  })
  return spawnEmitter
}

let openUrlArg = null
const openUrl = (url, msg, cb) => {
  openUrlArg = url
  return cb()
}

const help = requireInject('../../lib/help.js', {
  '../../lib/npm.js': npm,
  '../../lib/utils/npm-usage.js': npmUsage,
  '../../lib/utils/open-url.js': openUrl,
  '../../lib/utils/output.js': output,
  child_process: {
    spawn,
  },
  glob,
})

test('npm help', t => {
  t.teardown(() => {
    npmUsageArg = null
  })

  return help([], (err) => {
    if (err)
      throw err

    t.equal(npmUsageArg, false, 'called npmUsage')
    t.end()
  })
})

test('npm help completion', async t => {
  t.teardown(() => {
    globErr = null
  })

  const noArgs = await help.completion({ conf: { argv: { remain: [] } } })
  t.strictSame(noArgs, ['help', 'whoami', 'npmrc', 'disputes'], 'outputs available help pages')
  const threeArgs = await help.completion({ conf: { argv: { remain: ['one', 'two', 'three'] } } })
  t.strictSame(threeArgs, [], 'outputs no results when more than 2 args are provided')
  globErr = new Error('glob failed')
  t.rejects(help.completion({ conf: { argv: { remain: [] } } }), /glob failed/, 'glob errors propagate')
})

test('npm help -h', t => {
  npmConfig.usage = true
  t.teardown(() => {
    npmConfig.usage = false
    OUTPUT.length = 0
  })

  return help(['help'], (err) => {
    if (err)
      throw err

    t.strictSame(OUTPUT, ['npm help <term>'], 'outputs usage information for command')
    t.end()
  })
})

test('npm help multiple args calls search', t => {
  t.teardown(() => {
    helpSearchArgs = null
  })

  return help(['run', 'script'], (err) => {
    if (err)
      throw err

    t.strictSame(helpSearchArgs, ['run', 'script'], 'passed the args to help-search')
    t.end()
  })
})

test('npm help no matches calls search', t => {
  globResult = []
  t.teardown(() => {
    helpSearchArgs = null
    globResult = globDefaults
  })

  return help(['asdfasdf'], (err) => {
    if (err)
      throw err

    t.strictSame(helpSearchArgs, ['asdfasdf'], 'passed the args to help-search')
    t.end()
  })
})

test('npm help glob errors propagate', t => {
  globErr = new Error('glob failed')
  t.teardown(() => {
    globErr = null
    spawnBin = null
    spawnArgs = null
  })

  return help(['whoami'], (err) => {
    t.match(err, /glob failed/, 'glob error propagates')
    t.end()
  })
})

test('npm help whoami', t => {
  globResult = ['/root/man/man1/npm-whoami.1.xz']
  t.teardown(() => {
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['whoami'], (err) => {
    if (err)
      throw err

    t.equal(spawnBin, 'man', 'calls man by default')
    t.strictSame(spawnArgs, ['1', 'npm-whoami'], 'passes the correct arguments')
    t.end()
  })
})

test('npm help 1 install', t => {
  npmConfig.viewer = 'browser'
  globResult = [
    '/root/man/man5/install.5',
    '/root/man/man1/npm-install.1',
  ]

  t.teardown(() => {
    npmConfig.viewer = undefined
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['1', 'install'], (err) => {
    if (err)
      throw err

    t.match(openUrlArg, /commands(\/|\\)npm-install.html$/, 'attempts to open the correct url')
    t.end()
  })
})

test('npm help 5 install', t => {
  npmConfig.viewer = 'browser'
  globResult = [
    '/root/man/man5/install.5',
    '/root/man/man1/npm-install.1',
  ]

  t.teardown(() => {
    npmConfig.viewer = undefined
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['5', 'install'], (err) => {
    if (err)
      throw err

    t.match(openUrlArg, /configuring-npm(\/|\\)install.html$/, 'attempts to open the correct url')
    t.end()
  })
})

test('npm help 7 config', t => {
  npmConfig.viewer = 'browser'
  globResult = [
    '/root/man/man1/npm-config.1',
    '/root/man/man7/config.7',
  ]
  t.teardown(() => {
    npmConfig.viewer = undefined
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['7', 'config'], (err) => {
    if (err)
      throw err

    t.match(openUrlArg, /using-npm(\/|\\)config.html$/, 'attempts to open the correct url')
    t.end()
  })
})

test('npm help with browser viewer and invalid section throws', t => {
  npmConfig.viewer = 'browser'
  globResult = [
    '/root/man/man1/npm-config.1',
    '/root/man/man7/config.7',
    '/root/man/man9/config.9',
  ]
  t.teardown(() => {
    npmConfig.viewer = undefined
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['9', 'config'], (err) => {
    t.match(err, /invalid man section: 9/, 'throws appropriate error')
    t.end()
  })
})

test('npm help global redirects to folders', t => {
  globResult = ['/root/man/man5/folders.5']
  t.teardown(() => {
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['global'], (err) => {
    if (err)
      throw err

    t.equal(spawnBin, 'man', 'calls man by default')
    t.strictSame(spawnArgs, ['5', 'folders'], 'passes the correct arguments')
    t.end()
  })
})

test('npm help package.json redirects to package-json', t => {
  globResult = ['/root/man/man5/package-json.5']
  t.teardown(() => {
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['package.json'], (err) => {
    if (err)
      throw err

    t.equal(spawnBin, 'man', 'calls man by default')
    t.strictSame(spawnArgs, ['5', 'package-json'], 'passes the correct arguments')
    t.end()
  })
})

test('npm help ?(un)star', t => {
  npmConfig.viewer = 'woman'
  globResult = [
    '/root/man/man1/npm-star.1',
    '/root/man/man1/npm-unstar.1',
  ]
  t.teardown(() => {
    npmConfig.viewer = undefined
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['?(un)star'], (err) => {
    if (err)
      throw err

    t.equal(spawnBin, 'emacsclient', 'maps woman to emacs correctly')
    t.strictSame(spawnArgs, ['-e', `(woman-find-file '/root/man/man1/npm-unstar.1')`], 'passes the correct arguments')
    t.end()
  })
})

test('npm help - woman viewer propagates errors', t => {
  npmConfig.viewer = 'woman'
  spawnCode = 1
  globResult = [
    '/root/man/man1/npm-star.1',
    '/root/man/man1/npm-unstar.1',
  ]
  t.teardown(() => {
    npmConfig.viewer = undefined
    spawnCode = 0
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['?(un)star'], (err) => {
    t.match(err, /help process exited with code: 1/, 'received the correct error')
    t.equal(spawnBin, 'emacsclient', 'maps woman to emacs correctly')
    t.strictSame(spawnArgs, ['-e', `(woman-find-file '/root/man/man1/npm-unstar.1')`], 'passes the correct arguments')
    t.end()
  })
})

test('npm help un*', t => {
  globResult = [
    '/root/man/man1/npm-unstar.1',
    '/root/man/man1/npm-uninstall.1',
    '/root/man/man1/npm-unpublish.1',
  ]
  t.teardown(() => {
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['un*'], (err) => {
    if (err)
      throw err

    t.equal(spawnBin, 'man', 'calls man by default')
    t.strictSame(spawnArgs, ['1', 'npm-unstar'], 'passes the correct arguments')
    t.end()
  })
})

test('npm help - man viewer propagates errors', t => {
  spawnCode = 1
  globResult = [
    '/root/man/man1/npm-unstar.1',
    '/root/man/man1/npm-uninstall.1',
    '/root/man/man1/npm-unpublish.1',
  ]
  t.teardown(() => {
    spawnCode = 0
    globResult = globDefaults
    spawnBin = null
    spawnArgs = null
  })

  return help(['un*'], (err) => {
    t.match(err, /help process exited with code: 1/, 'received correct error')
    t.equal(spawnBin, 'man', 'calls man by default')
    t.strictSame(spawnArgs, ['1', 'npm-unstar'], 'passes the correct arguments')
    t.end()
  })
})