|
| 1 | +const childProcess = require('child_process'); |
| 2 | +const path = require('path'); |
| 3 | +const commander = require('../'); |
| 4 | + |
| 5 | +describe('incrementNodeInspectorPort', () => { |
| 6 | + let spawnSpy; |
| 7 | + let signalSpy; |
| 8 | + const oldExecArgv = process.execArgv; |
| 9 | + |
| 10 | + beforeAll(() => { |
| 11 | + spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { |
| 12 | + return { |
| 13 | + on: () => {} |
| 14 | + }; |
| 15 | + }); |
| 16 | + signalSpy = jest.spyOn(process, 'on').mockImplementation(() => {}); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + spawnSpy.mockClear(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterAll(() => { |
| 24 | + spawnSpy.mockRestore(); |
| 25 | + signalSpy.mockRestore(); |
| 26 | + process.execArgv = oldExecArgv; |
| 27 | + }); |
| 28 | + |
| 29 | + 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 | + } |
| 35 | + |
| 36 | + function extractMockExecArgs(mock) { |
| 37 | + return mock.mock.calls[0][1].slice(0, -1); |
| 38 | + } |
| 39 | + |
| 40 | + 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 | + }); |
| 47 | + |
| 48 | + 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 | + }); |
| 55 | + |
| 56 | + 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 | + }); |
| 63 | + |
| 64 | + 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 | + }); |
| 71 | + |
| 72 | + 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 | + }); |
| 79 | + |
| 80 | + 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 | + }); |
| 87 | + |
| 88 | + 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 | + }); |
| 95 | + |
| 96 | + 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 | + }); |
| 103 | + |
| 104 | + 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 | + }); |
| 111 | + |
| 112 | + 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 | + }); |
| 119 | + |
| 120 | + 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 | + }); |
| 127 | + |
| 128 | + 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 | +}); |
0 commit comments