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

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

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

const cliMock = async (t, opts) => {
  let exitHandlerArgs = null
  let npm = null
  const exitHandlerMock = (...args) => {
    exitHandlerArgs = args
    npm.unload()
  }
  exitHandlerMock.setNpm = _npm => npm = _npm

  const { Npm, outputs, logMocks, logs } = await loadMockNpm(t, { ...opts, init: false })
  const cli = t.mock('../../lib/cli.js', {
    '../../lib/npm.js': Npm,
    '../../lib/utils/exit-handler.js': exitHandlerMock,
    ...logMocks,
  })

  return {
    Npm,
    cli,
    outputs,
    exitHandlerCalled: () => exitHandlerArgs,
    exitHandlerNpm: () => npm,
    logs,
    logsBy: (title) => logs.verbose.filter(([p]) => p === title).map(([p, ...rest]) => rest),
  }
}

t.afterEach(() => {
  delete process.exitCode
})

t.test('print the version, and treat npm_g as npm -g', async t => {
  const { logsBy, logs, cli, Npm, outputs, exitHandlerCalled } = await cliMock(t, {
    globals: { 'process.argv': ['node', 'npm_g', '-v'] },
  })
  await cli(process)

  t.strictSame(process.argv, ['node', 'npm', '-g', '-v'], 'system process.argv was rewritten')
  t.strictSame(logsBy('cli'), [['node npm']])
  t.strictSame(logsBy('title'), [['npm']])
  t.strictSame(logsBy('argv'), [['"--global" "--version"']])
  t.strictSame(logs.info, [
    ['using', 'npm@%s', Npm.version],
    ['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 { logsBy, cli, outputs, exitHandlerCalled } = await cliMock(t, {
    mocks: {
      '../../lib/commands/version.js': class Version {
        async exec (args) {
          t.strictSame(args, [])
        }
      },
    },
    globals: {
      'process.argv': ['node', 'npm', 'install', 'or', 'whatever', '--versions'],
    },
  })
  await cli(process)

  t.equal(process.title, 'npm install or whatever')
  t.strictSame(logsBy('cli'), [['node npm']])
  t.strictSame(logsBy('title'), [['npm install or whatever']])
  t.strictSame(logsBy('argv'), [['"install" "or" "whatever" "--versions"']])
  t.strictSame(outputs, [])
  t.strictSame(exitHandlerCalled(), [])
})

t.test('logged argv is sanitized', async t => {
  const { logsBy, cli } = await cliMock(t, {
    mocks: {
      '../../lib/commands/version.js': class Version {
        async exec () {}
      },
    },
    globals: {
      'process.argv': [
        'node',
        'npm',
        'version',
        '--registry',
        'https://u:password@npmjs.org/password',
      ],
    },
  })

  await cli(process)
  t.equal(process.title, 'npm version')
  t.strictSame(logsBy('cli'), [['node npm']])
  t.strictSame(logsBy('title'), [['npm version']])
  t.strictSame(logsBy('argv'), [['"version" "--registry" "https://u:***@npmjs.org/password"']])
})

t.test('logged argv is sanitized with equals', async t => {
  const { logsBy, cli } = await cliMock(t, {
    mocks: {
      '../../lib/commands/version.js': class Version {
        async exec () {}
      },
    },
    globals: {
      'process.argv': [
        'node',
        'npm',
        'version',
        '--registry=https://u:password@npmjs.org',
      ],
    },
  })
  await cli(process)

  t.strictSame(logsBy('argv'), [['"version" "--registry" "https://u:***@npmjs.org"']])
})

t.test('print usage if no params provided', async t => {
  const { cli, outputs, exitHandlerCalled, exitHandlerNpm } = await cliMock(t, {
    globals: {
      'process.argv': ['node', 'npm'],
    },
  })
  await cli(process)

  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(process.exitCode, 1)
})

t.test('print usage if non-command param provided', async t => {
  const { cli, outputs, exitHandlerCalled, exitHandlerNpm } = await cliMock(t, {
    globals: {
      'process.argv': ['node', 'npm', 'tset'],
    },
  })
  await cli(process)

  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(process.exitCode, 1)
})

t.test('load error calls error handler', async t => {
  const err = new Error('test load error')
  const { cli, exitHandlerCalled } = await cliMock(t, {
    mocks: {
      '../../lib/utils/config/index.js': {
        definitions: null,
        flatten: null,
        shorthands: null,
      },
      '@npmcli/config': class BadConfig {
        async load () {
          throw err
        }
      },
    },
    globals: {
      'process.argv': ['node', 'npm', 'asdf'],
    },
  })
  await cli(process)
  t.strictSame(exitHandlerCalled(), [err])
})

t.test('known broken node version', async t => {
  const errors = []
  let exitCode
  const { cli } = await cliMock(t, {
    globals: {
      'console.error': (msg) => errors.push(msg),
      'process.version': '6.0.0',
      'process.exit': e => exitCode = e,
    },
  })
  await cli(process)
  t.match(errors, [
    'ERROR: npm is known not to run on Node.js 6.0.0',
    'You\'ll need to upgrade to a newer Node.js version in order to use this',
    'version of npm. You can find the latest version at https://nodejs.org/',
  ])
  t.match(exitCode, 1)
})

t.test('unsupported node version', async t => {
  const errors = []
  const { cli } = await cliMock(t, {
    globals: {
      'console.error': (msg) => errors.push(msg),
      'process.version': '12.6.0',
    },
  })
  await cli(process)
  t.match(errors, [
    'npm does not support Node.js 12.6.0',
    'You should probably upgrade to a newer version of node as we',
    'can\'t make any promises that npm will work with this version.',
  ])
})