test: update startCLI to set --port=0 by default

update the `startCLI` debugging testing utility to set by default
the port to use to `0` (i.e. a random port)

PR-URL: https://github.com/nodejs/node/pull/59042
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Dario Piotrowicz 2025-07-20 18:37:28 +01:00 committed by GitHub
parent bf2384f224
commit 29626f8fb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 43 additions and 41 deletions

View file

@ -20,10 +20,14 @@ function isPreBreak(output) {
return /Break on start/.test(output) && /1 \(function \(exports/.test(output);
}
function startCLI(args, flags = [], spawnOpts = {}) {
function startCLI(args, flags = [], spawnOpts = {}, opts = { randomPort: true }) {
let stderrOutput = '';
const child =
spawn(process.execPath, [...flags, 'inspect', ...args], spawnOpts);
const child = spawn(process.execPath, [
...flags,
'inspect',
...(opts.randomPort !== false ? ['--port=0'] : []),
...args,
], spawnOpts);
const outputBuffer = [];
function bufferOutput(chunk) {

View file

@ -55,7 +55,7 @@ function launchTarget(...args) {
try {
const { childProc, host, port } = await launchTarget('--inspect=0', script);
target = childProc;
cli = startCLI([`${host || '127.0.0.1'}:${port}`]);
cli = startCLI([`${host || '127.0.0.1'}:${port}`], [], {}, { randomPort: false });
await cli.waitForPrompt();
await cli.command('sb("alive.js", 3)');
await cli.waitFor(/break/);

View file

@ -21,9 +21,7 @@ addLibraryPath(process.env);
};
env.NODE_INSPECT_RESUME_ON_START = '1';
const cli = startCLI(['--port=0', script], [], {
env,
});
const cli = startCLI([script], [], { env });
await cli.waitForInitialBreak();
deepStrictEqual(cli.breakInfo, {

View file

@ -13,7 +13,7 @@ const path = require('path');
{
const scriptFullPath = fixtures.path('debugger', 'backtrace.js');
const script = path.relative(process.cwd(), scriptFullPath);
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
async function runTest() {
try {

View file

@ -11,7 +11,7 @@ const path = require('path');
const scriptFullPath = fixtures.path('debugger', 'break.js');
const script = path.relative(process.cwd(), scriptFullPath);
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
(async () => {
await cli.waitForInitialBreak();

View file

@ -9,7 +9,7 @@ const startCLI = require('../common/debugger');
// Test for "Breakpoint at specified location already exists" error.
const script = fixtures.path('debugger', 'three-lines.js');
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
(async () => {
try {

View file

@ -13,7 +13,7 @@ const path = require('path');
{
const scriptFullPath = fixtures.path('debugger', 'break.js');
const script = path.relative(process.cwd(), scriptFullPath);
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
function onFatal(error) {
cli.quit();

View file

@ -13,7 +13,7 @@ const path = require('path');
{
const scriptFullPath = fixtures.path('debugger', 'exceptions.js');
const script = path.relative(process.cwd(), scriptFullPath);
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
(async () => {
try {

View file

@ -7,7 +7,7 @@ import startCLI from '../common/debugger.js';
import assert from 'assert';
const cli = startCLI(['--port=0', path('debugger/backtrace.js')]);
const cli = startCLI([path('debugger/backtrace.js')]);
try {
await cli.waitForInitialBreak();

View file

@ -8,7 +8,7 @@ const startCLI = require('../common/debugger');
const assert = require('assert');
const cli = startCLI(['--port=0', fixtures.path('debugger/alive.js')]);
const cli = startCLI([fixtures.path('debugger/alive.js')]);
async function waitInitialBreak() {
try {

View file

@ -7,7 +7,7 @@ import startCLI from '../common/debugger.js';
import assert from 'assert';
const cli = startCLI(['--port=0', path('debugger', 'three-lines.js')]);
const cli = startCLI([path('debugger', 'three-lines.js')]);
try {
await cli.waitForInitialBreak();

View file

@ -16,7 +16,7 @@ const filename = tmpdir.resolve('node.heapsnapshot');
// Heap profiler take snapshot.
{
const opts = { cwd: tmpdir.path };
const cli = startCLI(['--port=0', fixtures.path('debugger/empty.js')], [], opts);
const cli = startCLI([fixtures.path('debugger/empty.js')], [], opts);
async function waitInitialBreak() {
try {

View file

@ -7,7 +7,7 @@ import startCLI from '../common/debugger.js';
import assert from 'assert';
const cli = startCLI(['--port=0', path('debugger', 'empty.js')]);
const cli = startCLI([path('debugger', 'empty.js')]);
try {
await cli.waitForInitialBreak();

View file

@ -17,7 +17,7 @@ const host = '127.0.0.1';
server.listen(0, mustCall(async () => {
const port = server.address().port;
const cli = startCLI([`${host}:${port}`]);
const cli = startCLI([`${host}:${port}`], [], {}, { randomPort: false });
try {
const code = await cli.quit();
assert.strictEqual(code, 1);
@ -35,7 +35,7 @@ const host = '127.0.0.1';
server.listen(0, host, mustCall(async () => {
const port = server.address().port;
const cli = startCLI([`${host}:${port}`]);
const cli = startCLI([`${host}:${port}`], [], {}, { randomPort: false });
try {
const code = await cli.quit();
assert.strictEqual(code, 1);

View file

@ -8,7 +8,7 @@ const startCLI = require('../common/debugger');
const assert = require('assert');
const cli = startCLI(['--port=0', fixtures.path('debugger/three-lines.js')]);
const cli = startCLI([fixtures.path('debugger/three-lines.js')]);
(async () => {
await cli.waitForInitialBreak();

View file

@ -9,7 +9,7 @@ const assert = require('assert');
// Debugger agent direct access.
{
const cli = startCLI(['--port=0', fixtures.path('debugger/three-lines.js')]);
const cli = startCLI([fixtures.path('debugger/three-lines.js')]);
const scriptPattern = /^\* (\d+): \S+debugger(?:\/|\\)three-lines\.js/m;
async function testDebuggerLowLevel() {

View file

@ -8,7 +8,7 @@ const startCLI = require('../common/debugger');
const assert = require('assert');
const cli = startCLI(['--port=0', fixtures.path('debugger/empty.js')]);
const cli = startCLI([fixtures.path('debugger/empty.js')]);
(async () => {
await cli.waitForInitialBreak();

View file

@ -14,7 +14,7 @@ const script = path.relative(process.cwd(), scriptFullPath);
// Run after quit.
const runTest = async () => {
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
try {
await cli.waitForInitialBreak();
await cli.waitForPrompt();

View file

@ -10,7 +10,7 @@ const assert = require('assert');
const fs = require('fs');
const path = require('path');
const cli = startCLI(['--port=0', fixtures.path('debugger/empty.js')]);
const cli = startCLI([fixtures.path('debugger/empty.js')]);
const rootDir = path.resolve(__dirname, '..', '..');

View file

@ -14,7 +14,7 @@ function delay(ms) {
// Profiles.
{
const cli = startCLI(['--port=0', fixtures.path('debugger/empty.js')], [], {
const cli = startCLI([fixtures.path('debugger/empty.js')], [], {
env: {
...process.env,
// When this test is run with NODE_V8_COVERAGE, it clobbers the inspector

View file

@ -10,7 +10,7 @@ const assert = require('assert');
// Random port with --inspect-port=0.
const script = fixtures.path('debugger', 'three-lines.js');
const cli = startCLI(['--inspect-port=0', script]);
const cli = startCLI(['--inspect-port=0', script], [], {}, { randomPort: false });
(async () => {
await cli.waitForInitialBreak();

View file

@ -12,7 +12,7 @@ const assert = require('assert');
{
const script = fixtures.path('debugger', 'three-lines.js');
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
cli.waitForInitialBreak()
.then(() => cli.waitForPrompt())

View file

@ -14,7 +14,7 @@ const startCLI = require('../common/debugger');
// Using `restart` should result in only one "Connect/For help" message.
{
const script = fixtures.path('debugger', 'three-lines.js');
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
const listeningRegExp = /Debugger listening on/g;

View file

@ -13,7 +13,7 @@ const path = require('path');
{
const scriptFullPath = fixtures.path('debugger', 'three-lines.js');
const script = path.relative(process.cwd(), scriptFullPath);
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
function onFatal(error) {
cli.quit();

View file

@ -17,7 +17,7 @@ const script = path.relative(process.cwd(), scriptFullPath);
const otherScriptFullPath = fixtures.path('debugger', 'cjs', 'other.js');
const otherScript = path.relative(process.cwd(), otherScriptFullPath);
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
(async () => {
await cli.waitForInitialBreak();

View file

@ -11,7 +11,7 @@ const assert = require('assert');
// List scripts.
{
const script = fixtures.path('debugger', 'three-lines.js');
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
(async () => {
try {

View file

@ -7,7 +7,7 @@ import startCLI from '../common/debugger.js';
import assert from 'assert';
const script = path('debugger', 'twenty-lines.js');
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
function onFatal(error) {
cli.quit();

View file

@ -19,7 +19,7 @@ const { createServer } = require('net');
try {
const script = fixtures.path('debugger', 'three-lines.js');
const cli = startCLI([`--port=${port}`, script]);
const cli = startCLI([`--port=${port}`, script], [], {}, { randomPort: false });
const code = await cli.quit();
assert.doesNotMatch(

View file

@ -11,7 +11,7 @@ const assert = require('assert');
// Test for files that start with strict directive.
{
const script = fixtures.path('debugger', 'use-strict.js');
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
function onFatal(error) {
cli.quit();

View file

@ -8,7 +8,7 @@ const startCLI = require('../common/debugger');
const assert = require('assert');
const cli = startCLI(['--port=0', fixtures.path('debugger/break.js')]);
const cli = startCLI([fixtures.path('debugger/break.js')]);
(async () => {
await cli.waitForInitialBreak();

View file

@ -7,7 +7,7 @@ import startCLI from '../common/debugger.js';
import assert from 'assert';
const script = path('debugger', 'break.js');
const cli = startCLI(['--port=0', script]);
const cli = startCLI([script]);
function onFatal(error) {
cli.quit();

View file

@ -11,7 +11,7 @@ const assert = require('assert');
// Custom port.
const script = fixtures.path('debugger', 'three-lines.js');
const cli = startCLI([`--port=${common.PORT}`, script]);
const cli = startCLI([`--port=${common.PORT}`, script], [], {}, { randomPort: false });
(async function() {
try {
await cli.waitForInitialBreak();

View file

@ -9,7 +9,7 @@ const assert = require('assert');
// Launch CLI w/o args.
(async () => {
const cli = startCLI([]);
const cli = startCLI([], [], {}, { randomPort: false });
const code = await cli.quit();
assert.strictEqual(code, 9);
assert.match(cli.output, /^Usage:/, 'Prints usage info');
@ -17,7 +17,7 @@ const assert = require('assert');
// Launch w/ invalid host:port.
(async () => {
const cli = startCLI([`localhost:${common.PORT}`]);
const cli = startCLI([`localhost:${common.PORT}`], [], {}, { randomPort: false });
const code = await cli.quit();
assert.match(
cli.output,

View file

@ -8,7 +8,7 @@ import startCLI from '../common/debugger.js';
import assert from 'assert';
const script = path('debugger', 'three-lines.js');
const cli = startCLI([script]);
const cli = startCLI([script], [], {}, { randomPort: false });
try {
await cli.waitForInitialBreak();
await cli.waitForPrompt();

View file

@ -13,7 +13,7 @@ const script = fixtures.path('debugger', 'alive.js');
const runTest = async () => {
const target = spawn(process.execPath, [script]);
const cli = startCLI(['-p', `${target.pid}`]);
const cli = startCLI(['-p', `${target.pid}`], [], {}, { randomPort: false });
try {
await cli.waitForPrompt();