diff options
Diffstat (limited to 'test/lib/cli.js')
-rw-r--r-- | test/lib/cli.js | 182 |
1 files changed, 79 insertions, 103 deletions
diff --git a/test/lib/cli.js b/test/lib/cli.js index d762943b4..f02c57d8c 100644 --- a/test/lib/cli.js +++ b/test/lib/cli.js @@ -1,176 +1,153 @@ 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() +const mockGlobals = require('../fixtures/mock-globals.js') +const { load: loadMockNpm } = require('../fixtures/mock-npm.js') + +const cliMock = async (t, mocks) => { + let exitHandlerArgs = null + let npm = null + const exitHandlerMock = (...args) => { + exitHandlerArgs = args + npm.unload() } -} -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]), -} + exitHandlerMock.setNpm = _npm => npm = _npm -const cliMock = Npm => - t.mock('../../lib/cli.js', { + const { Npm, outputs, logMocks, logs } = await loadMockNpm(t, { mocks, init: false }) + const cli = t.mock('../../lib/cli.js', { '../../lib/npm.js': Npm, '../../lib/utils/update-notifier.js': async () => null, - '../../lib/utils/unsupported.js': unsupportedMock, + '../../lib/utils/unsupported.js': { + checkForBrokenNode: () => {}, + checkForUnsupportedNode: () => {}, + }, '../../lib/utils/exit-handler.js': exitHandlerMock, - npmlog: npmlogMock, + ...logMocks, }) -const processMock = proc => { - const mocked = { - ...process, - on: () => {}, - ...proc, + return { + Npm, + cli, + outputs, + exitHandlerCalled: () => exitHandlerArgs, + exitHandlerNpm: () => npm, + logs, } - // 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 + delete process.exitCode }) 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, + mockGlobals(t, { + 'process.argv': ['node', 'npm_g', '-v'], }) - const { Npm, outputs } = mockNpm(t) - const cli = cliMock(Npm) - await cli(proc) + const { logs, cli, Npm, outputs, exitHandlerCalled } = await cliMock(t) + await cli(process) - 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(logs.verbose.filter(([p]) => p !== 'logfile'), [ + ['cli', process.argv], + ]) + t.strictSame(logs.info, [ + ['using', 'npm@%s', Npm.version], + ['using', 'node@%s', process.version], ]) t.strictSame(outputs, [[Npm.version]]) - t.strictSame(exitHandlerCalled, []) + t.strictSame(exitHandlerCalled(), []) }) t.test('calling with --versions calls npm version with no args', async t => { - t.plan(5) - const proc = processMock({ - argv: ['node', 'npm', 'install', 'or', 'whatever', '--versions'], + t.plan(6) + mockGlobals(t, { + 'process.argv': ['node', 'npm', 'install', 'or', 'whatever', '--versions'], }) - const { Npm, outputs } = mockNpm(t, { + const { logs, cli, Npm, outputs, exitHandlerCalled } = await cliMock(t, { '../../lib/commands/version.js': class Version { async exec (args) { t.strictSame(args, []) } }, }) - const cli = cliMock(Npm) - await cli(proc) - t.equal(proc.title, 'npm') - t.strictSame(logs, [ - 'pause', - ['verbose', 'cli', proc.argv], - ['info', 'using', 'npm@%s', Npm.version], - ['info', 'using', 'node@%s', process.version], + + await cli(process) + t.equal(process.title, 'npm install or whatever') + t.strictSame(logs.verbose.filter(([p]) => p !== 'logfile'), [ + ['cli', process.argv], + ]) + t.strictSame(logs.info, [ + ['using', 'npm@%s', Npm.version], + ['using', 'node@%s', process.version], ]) t.strictSame(outputs, []) - t.strictSame(exitHandlerCalled, []) + t.strictSame(exitHandlerCalled(), []) }) t.test('logged argv is sanitized', async t => { - const proc = processMock({ - argv: [ + mockGlobals(t, { + 'process.argv': [ 'node', 'npm', 'version', 'https://username:password@npmjs.org/test_url_with_a_password', ], }) - const { Npm } = mockNpm(t, { + const { logs, cli, Npm } = await cliMock(t, { '../../lib/commands/version.js': class Version { async exec (args) {} }, }) - const cli = cliMock(Npm) - - await cli(proc) - t.equal(proc.title, 'npm') - t.strictSame(logs, [ - 'pause', + await cli(process) + t.ok(process.title.startsWith('npm version https://username:***@npmjs.org')) + t.strictSame(logs.verbose.filter(([p]) => p !== 'logfile'), [ [ - 'verbose', 'cli', ['node', 'npm', 'version', 'https://username:***@npmjs.org/test_url_with_a_password'], ], - ['info', 'using', 'npm@%s', Npm.version], - ['info', 'using', 'node@%s', process.version], + ]) + t.strictSame(logs.info, [ + ['using', 'npm@%s', Npm.version], + ['using', 'node@%s', process.version], ]) }) t.test('print usage if no params provided', async t => { - const proc = processMock({ - argv: ['node', 'npm'], + mockGlobals(t, { + 'process.argv': ['node', 'npm'], }) - const { Npm, outputs } = mockNpm(t) - const cli = cliMock(Npm) - await cli(proc) + const { cli, outputs, exitHandlerCalled, exitHandlerNpm } = await cliMock(t) + 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(proc.exitCode, 1) + 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 proc = processMock({ - argv: ['node', 'npm', 'tset'], + mockGlobals(t, { + 'process.argv': ['node', 'npm', 'tset'], }) - const { Npm, outputs } = mockNpm(t) - const cli = cliMock(Npm) - await cli(proc) + const { cli, outputs, exitHandlerCalled, exitHandlerNpm } = await cliMock(t) + 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(proc.exitCode, 1) + 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 proc = processMock({ - argv: ['node', 'npm', 'asdf'], + mockGlobals(t, { + 'process.argv': ['node', 'npm', 'asdf'], }) const err = new Error('test load error') - const { Npm } = mockNpm(t, { + const { cli, exitHandlerCalled } = await cliMock(t, { '../../lib/utils/config/index.js': { definitions: null, flatten: null, @@ -182,7 +159,6 @@ t.test('load error calls error handler', async t => { } }, }) - const cli = cliMock(Npm) - await cli(proc) - t.strictSame(exitHandlerCalled, [err]) + await cli(process) + t.strictSame(exitHandlerCalled(), [err]) }) |