• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const { mustCall, skipIfInspectorDisabled } = require('../common');
3
4skipIfInspectorDisabled();
5
6const assert = require('assert');
7const { spawn } = require('child_process');
8
9function test(arg, port = '') {
10  const args = [arg, '-p', 'process.debugPort'];
11  const proc = spawn(process.execPath, args);
12  proc.stdout.setEncoding('utf8');
13  proc.stderr.setEncoding('utf8');
14  let stdout = '';
15  let stderr = '';
16  proc.stdout.on('data', (data) => stdout += data);
17  proc.stderr.on('data', (data) => stderr += data);
18  proc.stdout.on('close', (hadErr) => assert(!hadErr));
19  proc.stderr.on('close', (hadErr) => assert(!hadErr));
20  proc.stderr.on('data', () => {
21    if (!stderr.includes('\n')) return;
22    assert(/Debugger listening on (.+)/.test(stderr));
23    port = new URL(RegExp.$1).port;
24    assert(+port > 0);
25  });
26  if (/inspect-brk/.test(arg)) {
27    proc.stderr.on('data', () => {
28      if (stderr.includes('\n') && !proc.killed) proc.kill();
29    });
30  } else {
31    let onclose = () => {
32      onclose = () => assert.strictEqual(port, stdout.trim());
33    };
34    proc.stdout.on('close', mustCall(() => onclose()));
35    proc.stderr.on('close', mustCall(() => onclose()));
36    proc.on('exit', mustCall((exitCode, signal) => assert.strictEqual(
37      exitCode,
38      0,
39      `exitCode: ${exitCode}, signal: ${signal}`)));
40  }
41}
42
43test('--inspect=0');
44test('--inspect=127.0.0.1:0');
45test('--inspect=localhost:0');
46
47test('--inspect-brk=0');
48test('--inspect-brk=127.0.0.1:0');
49test('--inspect-brk=localhost:0');
50
51// In these cases, the inspector doesn't listen, so an ephemeral port is not
52// allocated and the expected value of `process.debugPort` is `0`.
53test('--inspect-port=0', '0');
54test('--inspect-port=127.0.0.1:0', '0');
55test('--inspect-port=localhost:0', '0');
56