Skip to content
Myr

Increase test coverage, including incrementNodeInspectorPort (#1428)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add tests for incrementNodeInspectorPort * Fill out incrementNodeInspectorPort * Remove unused code from optionMissingArgument * Add tests for exit and custom throw * Error if call storeOptionsAsProperties with options already stored * Add test for bad params to parse * Error if parse "from" is unsupported * Can set alias after adding external command * Add test for invalid position to addHelpText * Clarify throw rather than display error * Add special case which displays help * Add test for incorrect return type on deprecated callback * Add test for implicit electron args * Add test for help requested on unknown subcommand * Add test for help suggestion including command path

authored by John Gee · committed by GitHub · parent 5173665 · Browse files · 10 files+263 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+263 added−13 removed

  • tests:+257 added−2 removed
  • other:+6 added−11 removed

10 files+263 added−13 removed

modified:tests/command.addHelpText.test.js+7 added
75 unmodified lines
76 expect(writeSpy).toHaveBeenNthCalledWith(3, program.helpInformation());77 expect(writeSpy).toHaveBeenNthCalledWith(4, 'after\n');78 expect(writeSpy).toHaveBeenNthCalledWith(5, 'afterAll\n');79 });8081 test('when "silly" position then throw', () => {82 const program = new commander.Command();83 expect(() => {84 program.addHelpText('silly', 'text');85 }).toThrow();86 });87});8889describe('program and subcommand calls to addHelpText', () => {90 let writeSpy;
155 unmodified lines
modified:tests/command.alias.test.js+9 added
modified:tests/command.exitOverride.test.js+23 added
276 unmodified lines
277278 expectCommanderError(caughtErr, 1, 'commander.invalidOptionArgument', "error: option '--colour <shade>' argument 'green' is invalid. NO");279 });280});281282test('when no override and error then exit(1)', () => {283 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });284 const program = new commander.Command();285 program.configureOutput({ outputError: () => {} });286 program.parse(['--unknownOption'], { from: 'user' });287 expect(exitSpy).toHaveBeenCalledWith(1);288 exitSpy.mockRestore();289});290291test('when custom processing throws custom error then throw custom error', () => {292 function justSayNo(value) {293 throw new Error('custom');294 }295 const program = new commander.Command();296 program297 .exitOverride()298 .option('-s, --shade <value>', 'specify shade', justSayNo);299300 expect(() => {301 program.parse(['--shade', 'green'], { from: 'user' });302 }).toThrow('custom');303});
modified:tests/command.help.test.js+7 added
86 unmodified lines
87 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);88 writeSpy.mockClear();89});9091test('when call deprecated outputHelp(cb) with wrong callback return type then throw', () => {92 const program = new commander.Command();93 expect(() => {94 program.outputHelp((helpInformation) => 3);95 }).toThrow();96});9798test('when command sets deprecated noHelp then not displayed in helpInformation', () => {99 const program = new commander.Command();100 program101 .command('secret', 'secret description', { noHelp: true });
154 unmodified lines
modified:tests/command.helpCommand.test.js+26 added
19 unmodified lines
20 const helpInformation = program.helpInformation();21 expect(helpInformation).toMatch(/help \[command\]/);22 });2324 test('when program has subcommands and specify only unknown option then display help', () => {25 const program = new commander.Command();26 program27 .configureHelp({ formatHelp: () => '' })28 .exitOverride()29 .allowUnknownOption()30 .command('foo');31 let caughtErr;32 try {33 program.parse(['--unknown'], { from: 'user' });34 } catch (err) {35 caughtErr = err;36 }37 expect(caughtErr.code).toEqual('commander.help');38 });3940 test('when program has subcommands and suppress help command then no help command', () => {41 const program = new commander.Command();42 program.addHelpCommand(false);43 program.command('foo');
38 unmodified lines
82 program.parse('node test.js help'.split(' '));83 }).toThrow('program');84 });8586 test('when "program help unknown" then program', () => {87 const program = new commander.Command();88 program.exitOverride();89 program.command('sub1');90 program.exitOverride(() => { throw new Error('program'); });91 expect(() => {92 program.parse('node test.js help unknown'.split(' '));93 }).toThrow('program');94 });9596 test('when "program help sub1" then sub1', () => {97 const program = new commander.Command();98 program.exitOverride();99 const sub1 = program.command('sub1');
27 unmodified lines
modified:tests/command.parse.test.js+26 added-2 removed
modified:tests/command.unknownCommand.test.js+16 added
60 unmodified lines
61 caughtErr = err;62 }63 expect(caughtErr.code).toBe('commander.unknownCommand');64 });6566 test('when unknown subcommand then help suggestion includes command path', () => {67 const program = new commander.Command();68 program69 .exitOverride()70 .command('sub')71 .command('subsub');72 let caughtErr;73 try {74 program.parse('node test.js sub unknown'.split(' '));75 } catch (err) {76 caughtErr = err;77 }78 expect(caughtErr.code).toBe('commander.unknownCommand');79 expect(writeErrorSpy.mock.calls[0][0]).toMatch('test sub');80 });81});
modified:tests/commander.configureCommand.test.js+8 added
74 unmodified lines
75 .action(callback);76 program.parse(['node', 'test', 'value']);77 expect(callback).toHaveBeenCalledWith('value', program.opts(), program);78});7980test('when storeOptionsAsProperties() after adding option then throw', () => {81 const program = new commander.Command();82 program.option('--port <number>', 'port number', '80');83 expect(() => {84 program.storeOptionsAsProperties(false);85 }).toThrow();86});
added:tests/incrementNodeInspectorPort.test.js+135 added
1const childProcess = require('child_process');2const path = require('path');3const commander = require('../');45describe('incrementNodeInspectorPort', () => {6 let spawnSpy;7 let signalSpy;8 const oldExecArgv = process.execArgv;910 beforeAll(() => {11 spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {12 return {13 on: () => {}14 };15 });16 signalSpy = jest.spyOn(process, 'on').mockImplementation(() => {});17 });1819 afterEach(() => {20 spawnSpy.mockClear();21 });2223 afterAll(() => {24 spawnSpy.mockRestore();25 signalSpy.mockRestore();26 process.execArgv = oldExecArgv;27 });2829 function makeProgram() {30 const program = new commander.Command();31 const fileWhichExists = path.join(__dirname, './fixtures/pm-cache.js');32 program.command('cache', 'stand-alone command', { executableFile: fileWhichExists });33 return program;34 }3536 function extractMockExecArgs(mock) {37 return mock.mock.calls[0][1].slice(0, -1);38 }3940 test('when --inspect then bump port', () => {41 const program = makeProgram();42 process.execArgv = ['--inspect'];43 program.parse(['node', 'test', 'cache']);44 const execArgs = extractMockExecArgs(spawnSpy);45 expect(execArgs).toEqual(['--inspect=127.0.0.1:9230']);46 });4748 test('when --inspect=100 then bump port', () => {49 const program = makeProgram();50 process.execArgv = ['--inspect=100'];51 program.parse(['node', 'test', 'cache']);52 const execArgs = extractMockExecArgs(spawnSpy);53 expect(execArgs).toEqual(['--inspect=127.0.0.1:101']);54 });5556 test('when --inspect=1.2.3.4:100 then bump port', () => {57 const program = makeProgram();58 process.execArgv = ['--inspect=1.2.3.4:100'];59 program.parse(['node', 'test', 'cache']);60 const execArgs = extractMockExecArgs(spawnSpy);61 expect(execArgs).toEqual(['--inspect=1.2.3.4:101']);62 });6364 test('when --inspect=1.2.3.4 then bump port', () => {65 const program = makeProgram();66 process.execArgv = ['--inspect=1.2.3.4'];67 program.parse(['node', 'test', 'cache']);68 const execArgs = extractMockExecArgs(spawnSpy);69 expect(execArgs).toEqual(['--inspect=1.2.3.4:9230']);70 });7172 test('when --inspect-brk then bump port', () => {73 const program = makeProgram();74 process.execArgv = ['--inspect-brk'];75 program.parse(['node', 'test', 'cache']);76 const execArgs = extractMockExecArgs(spawnSpy);77 expect(execArgs).toEqual(['--inspect-brk=127.0.0.1:9230']);78 });7980 test('when --inspect-brk=100 then bump port', () => {81 const program = makeProgram();82 process.execArgv = ['--inspect-brk=100'];83 program.parse(['node', 'test', 'cache']);84 const execArgs = extractMockExecArgs(spawnSpy);85 expect(execArgs).toEqual(['--inspect-brk=127.0.0.1:101']);86 });8788 test('when --inspect-brk=1.2.3.4 then bump port', () => {89 const program = makeProgram();90 process.execArgv = ['--inspect-brk=1.2.3.4'];91 program.parse(['node', 'test', 'cache']);92 const execArgs = extractMockExecArgs(spawnSpy);93 expect(execArgs).toEqual(['--inspect-brk=1.2.3.4:9230']);94 });9596 test('when --inspect-brk=1.2.3.4:100 then bump port', () => {97 const program = makeProgram();98 process.execArgv = ['--inspect-brk=1.2.3.4:100'];99 program.parse(['node', 'test', 'cache']);100 const execArgs = extractMockExecArgs(spawnSpy);101 expect(execArgs).toEqual(['--inspect-brk=1.2.3.4:101']);102 });103104 test('when --inspect-port=100 then bump port', () => {105 const program = makeProgram();106 process.execArgv = ['--inspect-port=100'];107 program.parse(['node', 'test', 'cache']);108 const execArgs = extractMockExecArgs(spawnSpy);109 expect(execArgs).toEqual(['--inspect-port=127.0.0.1:101']);110 });111112 test('when --inspect-port=1.2.3.4:100 then bump port', () => {113 const program = makeProgram();114 process.execArgv = ['--inspect-port=1.2.3.4:100'];115 program.parse(['node', 'test', 'cache']);116 const execArgs = extractMockExecArgs(spawnSpy);117 expect(execArgs).toEqual(['--inspect-port=1.2.3.4:101']);118 });119120 test('when --inspect-unexpected then unchanged', () => {121 const program = makeProgram();122 process.execArgv = ['--inspect-unexpected'];123 program.parse(['node', 'test', 'cache']);124 const execArgs = extractMockExecArgs(spawnSpy);125 expect(execArgs).toEqual(['--inspect-unexpected']);126 });127128 test('when --frozen-intrinsics then unchanged', () => {129 const program = makeProgram();130 process.execArgv = ['--frozen-intrinsics '];131 program.parse(['node', 'test', 'cache']);132 const execArgs = extractMockExecArgs(spawnSpy);133 expect(execArgs).toEqual(['--frozen-intrinsics ']);134 });135});
modified:index.js+6 added-11 removed
1 unmodified line
2 * Module dependencies.3 */45const EventEmitter = require('events').EventEmitter;6const spawn = require('child_process').spawn;6const childProcess = require('child_process');7const path = require('path');8const fs = require('fs');910// @ts-check
1323 unmodified lines
1334 args.unshift(bin);1335 // add executable arguments to spawn1336 args = incrementNodeInspectorPort(process.execArgv).concat(args);13371338 proc = spawn(process.argv[0], args, { stdio: 'inherit' });1338 proc = childProcess.spawn(process.argv[0], args, { stdio: 'inherit' });1339 } else {1340 proc = spawn(bin, args, { stdio: 'inherit' });1340 proc = childProcess.spawn(bin, args, { stdio: 'inherit' });1341 }1342 } else {1343 args.unshift(bin);1344 // add executable arguments to spawn1345 args = incrementNodeInspectorPort(process.execArgv).concat(args);1346 proc = spawn(process.execPath, args, { stdio: 'inherit' });1346 proc = childProcess.spawn(process.execPath, args, { stdio: 'inherit' });1347 }13481349 const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];1350 signals.forEach((signal) => {
320 unmodified lines
1671 * @param {string} [flag]1672 * @api private1673 */16741675 optionMissingArgument(option, flag) {1676 let message;1677 if (flag) {1678 message = `error: option '${option.flags}' argument missing, got '${flag}'`;1679 } else {1680 message = `error: option '${option.flags}' argument missing`;1681 }1675 optionMissingArgument(option) {1676 const message = `error: option '${option.flags}' argument missing`;1677 this._displayError(1, 'commander.optionMissingArgument', message);1678 };16791680 /**
461 unmodified lines