summaryrefslogtreecommitdiff
path: root/test/sequential/test-inspector-port-zero.js
blob: 59027b5e30769d8ee44f7f8331804dcf915270fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const { mustCall, skipIfInspectorDisabled } = require('../common');

skipIfInspectorDisabled();

const assert = require('assert');
const { URL } = require('url');
const { spawn } = require('child_process');

function test(arg, port = '') {
  const args = [arg, '-p', 'process.debugPort'];
  const proc = spawn(process.execPath, args);
  proc.stdout.setEncoding('utf8');
  proc.stderr.setEncoding('utf8');
  let stdout = '';
  let stderr = '';
  proc.stdout.on('data', (data) => stdout += data);
  proc.stderr.on('data', (data) => stderr += data);
  proc.stdout.on('close', (hadErr) => assert(!hadErr));
  proc.stderr.on('close', (hadErr) => assert(!hadErr));
  proc.stderr.on('data', () => {
    if (!stderr.includes('\n')) return;
    assert(/Debugger listening on (.+)/.test(stderr));
    port = new URL(RegExp.$1).port;
    assert(+port > 0);
  });
  if (/inspect-brk/.test(arg)) {
    proc.stderr.on('data', () => {
      if (stderr.includes('\n') && !proc.killed) proc.kill();
    });
  } else {
    let onclose = () => {
      onclose = () => assert.strictEqual(port, stdout.trim());
    };
    proc.stdout.on('close', mustCall(() => onclose()));
    proc.stderr.on('close', mustCall(() => onclose()));
    proc.on('exit', mustCall((exitCode, signal) => assert.strictEqual(
      exitCode,
      0,
      `exitCode: ${exitCode}, signal: ${signal}`)));
  }
}

test('--inspect=0');
test('--inspect=127.0.0.1:0');
test('--inspect=localhost:0');

test('--inspect-brk=0');
test('--inspect-brk=127.0.0.1:0');
test('--inspect-brk=localhost:0');

// In these cases, the inspector doesn't listen, so an ephemeral port is not
// allocated and the expected value of `process.debugPort` is `0`.
test('--inspect-port=0', '0');
test('--inspect-port=127.0.0.1:0', '0');
test('--inspect-port=localhost:0', '0');