Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cli.js « lib « test « npm « deps - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c0b6c0ba727e83c49c036b62cd33cbfd64b972d (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
const t = require('tap')

const { real: mockNpm } = require('../fixtures/mock-npm.js')

const unsupportedMock = {
  checkForBrokenNode: () => {},
  checkForUnsupportedNode: () => {},
}

let exitHandlerCalled = null
let exitHandlerNpm = null
let exitHandlerCb
const exitHandlerMock = (...args) => {
  exitHandlerCalled = args
  if (exitHandlerCb)
    exitHandlerCb()
}
exitHandlerMock.setNpm = npm => {
  exitHandlerNpm = npm
}

const logs = []
const npmlogMock = {
  pause: () => logs.push('pause'),
  verbose: (...msg) => logs.push(['verbose', ...msg]),
  info: (...msg) => logs.push(['info', ...msg]),
}

const cliMock = (npm) => t.mock('../../lib/cli.js', {
  '../../lib/npm.js': npm,
  '../../lib/utils/update-notifier.js': async () => null,
  '../../lib/utils/unsupported.js': unsupportedMock,
  '../../lib/utils/exit-handler.js': exitHandlerMock,
  npmlog: npmlogMock,
})

const processMock = (proc) => {
  const mocked = {
    ...process,
    on: () => {},
    ...proc,
  }
  // nopt looks at process directly
  process.argv = mocked.argv
  return mocked
}

const { argv } = process

t.afterEach(() => {
  logs.length = 0
  process.argv = argv
  exitHandlerCalled = null
  exitHandlerNpm = null
})

t.test('print the version, and treat npm_g as npm -g', async t => {
  const proc = processMock({
    argv: ['node', 'npm_g', '-v'],
    version: process.version,
  })

  const { npm, outputs } = mockNpm(t)
  const cli = cliMock(npm)
  await cli(proc)

  t.strictSame(proc.argv, ['node', 'npm', '-g', '-v'], 'npm process.argv was rewritten')
  t.strictSame(process.argv, ['node', 'npm', '-g', '-v'], 'system process.argv was rewritten')
  t.strictSame(logs, [
    'pause',
    ['verbose', 'cli', proc.argv],
    ['info', 'using', 'npm@%s', npm.version],
    ['info', 'using', 'node@%s', process.version],
  ])
  t.strictSame(outputs, [[npm.version]])
  t.strictSame(exitHandlerCalled, [])
})

t.test('calling with --versions calls npm version with no args', async t => {
  const proc = processMock({
    argv: ['node', 'npm', 'install', 'or', 'whatever', '--versions'],
  })
  const { npm, outputs } = mockNpm(t)
  const cli = cliMock(npm)

  let versionArgs
  npm.commands.version = (args, cb) => {
    versionArgs = args
    cb()
  }

  await cli(proc)
  t.strictSame(versionArgs, [])
  t.equal(proc.title, 'npm')
  t.strictSame(npm.argv, [])
  t.strictSame(logs, [
    'pause',
    ['verbose', 'cli', proc.argv],
    ['info', 'using', 'npm@%s', npm.version],
    ['info', 'using', 'node@%s', process.version],
  ])

  t.strictSame(outputs, [])
  t.strictSame(exitHandlerCalled, [])
})

t.test('logged argv is sanitized', async t => {
  const proc = processMock({
    argv: ['node', 'npm', 'testcommand', 'https://username:password@npmjs.org/test_url_with_a_password'],
  })
  const { npm } = mockNpm(t)
  const cli = cliMock(npm)

  npm.commands.testcommand = (args, cb) => {
    cb()
  }

  await cli(proc)
  t.equal(proc.title, 'npm')
  t.strictSame(logs, [
    'pause',
    ['verbose', 'cli', [
      'node',
      'npm',
      'testcommand',
      'https://username:***@npmjs.org/test_url_with_a_password',
    ]],
    ['info', 'using', 'npm@%s', npm.version],
    ['info', 'using', 'node@%s', process.version],
  ])
})

t.test('print usage if no params provided', async t => {
  const proc = processMock({
    argv: ['node', 'npm'],
  })

  const { npm, outputs } = mockNpm(t)
  const cli = cliMock(npm)
  await cli(proc)
  t.match(outputs[0][0], 'Usage:', 'outputs npm usage')
  t.match(exitHandlerCalled, [], 'should call exitHandler with no args')
  t.ok(exitHandlerNpm, 'exitHandler npm is set')
  t.match(proc.exitCode, 1)
})

t.test('print usage if non-command param provided', async t => {
  const proc = processMock({
    argv: ['node', 'npm', 'tset'],
  })

  const { npm, outputs } = mockNpm(t)
  const cli = cliMock(npm)
  await cli(proc)
  t.match(outputs[0][0], 'Unknown command: "tset"')
  t.match(outputs[0][0], 'Did you mean this?')
  t.match(exitHandlerCalled, [], 'should call exitHandler with no args')
  t.ok(exitHandlerNpm, 'exitHandler npm is set')
  t.match(proc.exitCode, 1)
})

t.test('load error calls error handler', async t => {
  const proc = processMock({
    argv: ['node', 'npm', 'asdf'],
  })

  const { npm } = mockNpm(t)
  const cli = cliMock(npm)
  const er = new Error('test load error')
  npm.load = () => Promise.reject(er)
  await cli(proc)
  t.strictSame(exitHandlerCalled, [er])
})