11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../fixtures/mock-npm.js') 31cb0ef41Sopenharmony_ciconst tmock = require('../fixtures/tmock.js') 41cb0ef41Sopenharmony_ciconst validateEngines = require('../../lib/es6/validate-engines.js') 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst cliMock = async (t, opts) => { 71cb0ef41Sopenharmony_ci let exitHandlerArgs = null 81cb0ef41Sopenharmony_ci let npm = null 91cb0ef41Sopenharmony_ci const exitHandlerMock = (...args) => { 101cb0ef41Sopenharmony_ci exitHandlerArgs = args 111cb0ef41Sopenharmony_ci npm.unload() 121cb0ef41Sopenharmony_ci } 131cb0ef41Sopenharmony_ci exitHandlerMock.setNpm = _npm => npm = _npm 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci const { Npm, outputs, logMocks, logs } = await loadMockNpm(t, { ...opts, init: false }) 161cb0ef41Sopenharmony_ci const cli = tmock(t, '{LIB}/cli-entry.js', { 171cb0ef41Sopenharmony_ci '{LIB}/npm.js': Npm, 181cb0ef41Sopenharmony_ci '{LIB}/utils/exit-handler.js': exitHandlerMock, 191cb0ef41Sopenharmony_ci ...logMocks, 201cb0ef41Sopenharmony_ci }) 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci return { 231cb0ef41Sopenharmony_ci Npm, 241cb0ef41Sopenharmony_ci cli: (p) => validateEngines(p, () => cli), 251cb0ef41Sopenharmony_ci outputs, 261cb0ef41Sopenharmony_ci exitHandlerCalled: () => exitHandlerArgs, 271cb0ef41Sopenharmony_ci exitHandlerNpm: () => npm, 281cb0ef41Sopenharmony_ci logs, 291cb0ef41Sopenharmony_ci logsBy: (title) => logs.verbose.filter(([p]) => p === title).map(([p, ...rest]) => rest), 301cb0ef41Sopenharmony_ci } 311cb0ef41Sopenharmony_ci} 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_cit.test('print the version, and treat npm_g as npm -g', async t => { 341cb0ef41Sopenharmony_ci const { logsBy, logs, cli, Npm, outputs, exitHandlerCalled } = await cliMock(t, { 351cb0ef41Sopenharmony_ci globals: { 'process.argv': ['node', 'npm_g', '-v'] }, 361cb0ef41Sopenharmony_ci }) 371cb0ef41Sopenharmony_ci await cli(process) 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci t.strictSame(process.argv, ['node', 'npm', '-g', '-v'], 'system process.argv was rewritten') 401cb0ef41Sopenharmony_ci t.strictSame(logsBy('cli'), [['node npm']]) 411cb0ef41Sopenharmony_ci t.strictSame(logsBy('title'), [['npm']]) 421cb0ef41Sopenharmony_ci t.match(logsBy('argv'), [['"--global" "--version"']]) 431cb0ef41Sopenharmony_ci t.strictSame(logs.info, [ 441cb0ef41Sopenharmony_ci ['using', 'npm@%s', Npm.version], 451cb0ef41Sopenharmony_ci ['using', 'node@%s', process.version], 461cb0ef41Sopenharmony_ci ]) 471cb0ef41Sopenharmony_ci t.equal(outputs.length, 1) 481cb0ef41Sopenharmony_ci t.strictSame(outputs, [[Npm.version]]) 491cb0ef41Sopenharmony_ci t.strictSame(exitHandlerCalled(), []) 501cb0ef41Sopenharmony_ci}) 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cit.test('calling with --versions calls npm version with no args', async t => { 531cb0ef41Sopenharmony_ci const { logsBy, cli, outputs, exitHandlerCalled } = await cliMock(t, { 541cb0ef41Sopenharmony_ci globals: { 551cb0ef41Sopenharmony_ci 'process.argv': ['node', 'npm', 'install', 'or', 'whatever', '--versions'], 561cb0ef41Sopenharmony_ci }, 571cb0ef41Sopenharmony_ci }) 581cb0ef41Sopenharmony_ci await cli(process) 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci t.equal(process.title, 'npm install or whatever') 611cb0ef41Sopenharmony_ci t.strictSame(logsBy('cli'), [['node npm']]) 621cb0ef41Sopenharmony_ci t.strictSame(logsBy('title'), [['npm install or whatever']]) 631cb0ef41Sopenharmony_ci t.match(logsBy('argv'), [['"install" "or" "whatever" "--versions"']]) 641cb0ef41Sopenharmony_ci t.equal(outputs.length, 1) 651cb0ef41Sopenharmony_ci t.match(outputs[0][0], { npm: String, node: String, v8: String }) 661cb0ef41Sopenharmony_ci t.strictSame(exitHandlerCalled(), []) 671cb0ef41Sopenharmony_ci}) 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_cit.test('logged argv is sanitized', async t => { 701cb0ef41Sopenharmony_ci const { logsBy, cli } = await cliMock(t, { 711cb0ef41Sopenharmony_ci globals: { 721cb0ef41Sopenharmony_ci 'process.argv': [ 731cb0ef41Sopenharmony_ci 'node', 741cb0ef41Sopenharmony_ci 'npm', 751cb0ef41Sopenharmony_ci 'version', 761cb0ef41Sopenharmony_ci '--registry', 771cb0ef41Sopenharmony_ci 'https://u:password@npmjs.org/password', 781cb0ef41Sopenharmony_ci ], 791cb0ef41Sopenharmony_ci }, 801cb0ef41Sopenharmony_ci }) 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci await cli(process) 831cb0ef41Sopenharmony_ci t.equal(process.title, 'npm version') 841cb0ef41Sopenharmony_ci t.strictSame(logsBy('cli'), [['node npm']]) 851cb0ef41Sopenharmony_ci t.strictSame(logsBy('title'), [['npm version']]) 861cb0ef41Sopenharmony_ci t.match(logsBy('argv'), [['"version" "--registry" "https://u:***@npmjs.org/password"']]) 871cb0ef41Sopenharmony_ci}) 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_cit.test('logged argv is sanitized with equals', async t => { 901cb0ef41Sopenharmony_ci const { logsBy, cli } = await cliMock(t, { 911cb0ef41Sopenharmony_ci globals: { 921cb0ef41Sopenharmony_ci 'process.argv': [ 931cb0ef41Sopenharmony_ci 'node', 941cb0ef41Sopenharmony_ci 'npm', 951cb0ef41Sopenharmony_ci 'version', 961cb0ef41Sopenharmony_ci '--registry=https://u:password@npmjs.org', 971cb0ef41Sopenharmony_ci ], 981cb0ef41Sopenharmony_ci }, 991cb0ef41Sopenharmony_ci }) 1001cb0ef41Sopenharmony_ci await cli(process) 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci t.match(logsBy('argv'), [['"version" "--registry" "https://u:***@npmjs.org/"']]) 1031cb0ef41Sopenharmony_ci}) 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_cit.test('print usage if no params provided', async t => { 1061cb0ef41Sopenharmony_ci const { cli, outputs, exitHandlerCalled, exitHandlerNpm } = await cliMock(t, { 1071cb0ef41Sopenharmony_ci globals: { 1081cb0ef41Sopenharmony_ci 'process.argv': ['node', 'npm'], 1091cb0ef41Sopenharmony_ci }, 1101cb0ef41Sopenharmony_ci }) 1111cb0ef41Sopenharmony_ci await cli(process) 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci t.match(outputs[0][0], 'Usage:', 'outputs npm usage') 1141cb0ef41Sopenharmony_ci t.match(exitHandlerCalled(), [], 'should call exitHandler with no args') 1151cb0ef41Sopenharmony_ci t.ok(exitHandlerNpm(), 'exitHandler npm is set') 1161cb0ef41Sopenharmony_ci t.match(process.exitCode, 1) 1171cb0ef41Sopenharmony_ci}) 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_cit.test('print usage if non-command param provided', async t => { 1201cb0ef41Sopenharmony_ci const { cli, outputs, exitHandlerCalled, exitHandlerNpm } = await cliMock(t, { 1211cb0ef41Sopenharmony_ci globals: { 1221cb0ef41Sopenharmony_ci 'process.argv': ['node', 'npm', 'tset'], 1231cb0ef41Sopenharmony_ci }, 1241cb0ef41Sopenharmony_ci }) 1251cb0ef41Sopenharmony_ci await cli(process) 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci t.match(outputs[0][0], 'Unknown command: "tset"') 1281cb0ef41Sopenharmony_ci t.match(outputs[0][0], 'Did you mean this?') 1291cb0ef41Sopenharmony_ci t.match(exitHandlerCalled(), [], 'should call exitHandler with no args') 1301cb0ef41Sopenharmony_ci t.ok(exitHandlerNpm(), 'exitHandler npm is set') 1311cb0ef41Sopenharmony_ci t.match(process.exitCode, 1) 1321cb0ef41Sopenharmony_ci}) 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_cit.test('load error calls error handler', async t => { 1351cb0ef41Sopenharmony_ci const err = new Error('test load error') 1361cb0ef41Sopenharmony_ci const { cli, exitHandlerCalled } = await cliMock(t, { 1371cb0ef41Sopenharmony_ci mocks: { 1381cb0ef41Sopenharmony_ci '@npmcli/config': class BadConfig { 1391cb0ef41Sopenharmony_ci async load () { 1401cb0ef41Sopenharmony_ci throw err 1411cb0ef41Sopenharmony_ci } 1421cb0ef41Sopenharmony_ci }, 1431cb0ef41Sopenharmony_ci }, 1441cb0ef41Sopenharmony_ci globals: { 1451cb0ef41Sopenharmony_ci 'process.argv': ['node', 'npm', 'asdf'], 1461cb0ef41Sopenharmony_ci }, 1471cb0ef41Sopenharmony_ci }) 1481cb0ef41Sopenharmony_ci await cli(process) 1491cb0ef41Sopenharmony_ci t.strictSame(exitHandlerCalled(), [err]) 1501cb0ef41Sopenharmony_ci}) 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_cit.test('unsupported node version', async t => { 1531cb0ef41Sopenharmony_ci const { cli, logs } = await cliMock(t, { 1541cb0ef41Sopenharmony_ci globals: { 1551cb0ef41Sopenharmony_ci 'process.version': '12.6.0', 1561cb0ef41Sopenharmony_ci }, 1571cb0ef41Sopenharmony_ci }) 1581cb0ef41Sopenharmony_ci await cli(process) 1591cb0ef41Sopenharmony_ci t.match( 1601cb0ef41Sopenharmony_ci logs.warn[0][1], 1611cb0ef41Sopenharmony_ci /npm v.* does not support Node\.js 12\.6\.0\./ 1621cb0ef41Sopenharmony_ci ) 1631cb0ef41Sopenharmony_ci}) 164