incrementNodeInspectorPort.test.cjs
This file
Path
tests/incrementNodeInspectorPort.test.cjs
146 lines · 5.2 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–146why, one step back1const { test, describe, after } = require('node:test');2const assert = require('node:assert/strict');3const childProcess = require('child_process');4const path = require('path');5const commander = require('../');67// Using mocking of child_process.spawn. Not fully supported yet in esm by node:test so run as cjs.89describe('increment node inspector port in executable subcommands', () => {10 function makeSpies(t) {11 const spawnSpy = t.mock.method(childProcess, 'spawn', () => {12 return {13 on: () => {},14 };15 });16 const signalSpy = test.mock.method(process, 'on', () => {});17 return { spawnSpy, signalSpy };18 }1920 const oldExecArgv = process.execArgv;2122 after(() => {23 process.execArgv = oldExecArgv;24 });2526 function makeProgram() {27 const program = new commander.Command();28 const fileWhichExists = path.join(__dirname, './fixtures/pm-cache.js');29 program.command('cache', 'stand-alone command', {30 executableFile: fileWhichExists,31 });32 return program;33 }3435 function extractMockExecArgs(spawnSpy) {36 return spawnSpy.mock.calls[0].arguments[1].slice(0, -1);37 }3839 test('when --inspect then bump port', (t) => {40 const { spawnSpy } = makeSpies(t);41 const program = makeProgram();42 process.execArgv = ['--inspect'];43 program.parse(['node', 'test', 'cache']);44 const execArgs = extractMockExecArgs(spawnSpy);45 assert.deepEqual(execArgs, ['--inspect=127.0.0.1:9230']);46 });4748 test('when --inspect=100 then bump port', (t) => {49 const { spawnSpy } = makeSpies(t);50 const program = makeProgram();51 process.execArgv = ['--inspect=100'];52 program.parse(['node', 'test', 'cache']);53 const execArgs = extractMockExecArgs(spawnSpy);54 assert.deepEqual(execArgs, ['--inspect=127.0.0.1:101']);55 });5657 test('when --inspect=1.2.3.4:100 then bump port', (t) => {58 const { spawnSpy } = makeSpies(t);59 const program = makeProgram();60 process.execArgv = ['--inspect=1.2.3.4:100'];61 program.parse(['node', 'test', 'cache']);62 const execArgs = extractMockExecArgs(spawnSpy);63 assert.deepEqual(execArgs, ['--inspect=1.2.3.4:101']);64 });6566 test('when --inspect=1.2.3.4 then bump port', (t) => {67 const { spawnSpy } = makeSpies(t);68 const program = makeProgram();69 process.execArgv = ['--inspect=1.2.3.4'];70 program.parse(['node', 'test', 'cache']);71 const execArgs = extractMockExecArgs(spawnSpy);72 assert.deepEqual(execArgs, ['--inspect=1.2.3.4:9230']);73 });7475 test('when --inspect-brk then bump port', (t) => {76 const { spawnSpy } = makeSpies(t);77 const program = makeProgram();78 process.execArgv = ['--inspect-brk'];79 program.parse(['node', 'test', 'cache']);80 const execArgs = extractMockExecArgs(spawnSpy);81 assert.deepEqual(execArgs, ['--inspect-brk=127.0.0.1:9230']);82 });8384 test('when --inspect-brk=100 then bump port', (t) => {85 const { spawnSpy } = makeSpies(t);86 const program = makeProgram();87 process.execArgv = ['--inspect-brk=100'];88 program.parse(['node', 'test', 'cache']);89 const execArgs = extractMockExecArgs(spawnSpy);90 assert.deepEqual(execArgs, ['--inspect-brk=127.0.0.1:101']);91 });9293 test('when --inspect-brk=1.2.3.4 then bump port', (t) => {94 const { spawnSpy } = makeSpies(t);95 const program = makeProgram();96 process.execArgv = ['--inspect-brk=1.2.3.4'];97 program.parse(['node', 'test', 'cache']);98 const execArgs = extractMockExecArgs(spawnSpy);99 assert.deepEqual(execArgs, ['--inspect-brk=1.2.3.4:9230']);100 });101102 test('when --inspect-brk=1.2.3.4:100 then bump port', (t) => {103 const { spawnSpy } = makeSpies(t);104 const program = makeProgram();105 process.execArgv = ['--inspect-brk=1.2.3.4:100'];106 program.parse(['node', 'test', 'cache']);107 const execArgs = extractMockExecArgs(spawnSpy);108 assert.deepEqual(execArgs, ['--inspect-brk=1.2.3.4:101']);109 });110111 test('when --inspect-port=100 then bump port', (t) => {112 const { spawnSpy } = makeSpies(t);113 const program = makeProgram();114 process.execArgv = ['--inspect-port=100'];115 program.parse(['node', 'test', 'cache']);116 const execArgs = extractMockExecArgs(spawnSpy);117 assert.deepEqual(execArgs, ['--inspect-port=127.0.0.1:101']);118 });119120 test('when --inspect-port=1.2.3.4:100 then bump port', (t) => {121 const { spawnSpy } = makeSpies(t);122 const program = makeProgram();123 process.execArgv = ['--inspect-port=1.2.3.4:100'];124 program.parse(['node', 'test', 'cache']);125 const execArgs = extractMockExecArgs(spawnSpy);126 assert.deepEqual(execArgs, ['--inspect-port=1.2.3.4:101']);127 });128129 test('when --inspect-unexpected then unchanged', (t) => {130 const { spawnSpy } = makeSpies(t);131 const program = makeProgram();132 process.execArgv = ['--inspect-unexpected'];133 program.parse(['node', 'test', 'cache']);134 const execArgs = extractMockExecArgs(spawnSpy);135 assert.deepEqual(execArgs, ['--inspect-unexpected']);136 });137138 test('when --frozen-intrinsics then unchanged', (t) => {139 const { spawnSpy } = makeSpies(t);140 const program = makeProgram();141 process.execArgv = ['--frozen-intrinsics '];142 program.parse(['node', 'test', 'cache']);143 const execArgs = extractMockExecArgs(spawnSpy);144 assert.deepEqual(execArgs, ['--frozen-intrinsics ']);145 });146});