1'use strict'; 2const { spawn } = require('child_process'); 3const Path = require('path'); 4const { test } = require('tap'); 5 6const startCLI = require('./start-cli'); 7 8// NOTE(oyyd): We might want to import this regexp from "lib/_inspect.js"? 9const kDebuggerMsgReg = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\//; 10 11function launchTarget(...args) { 12 const childProc = spawn(process.execPath, args); 13 return new Promise((resolve, reject) => { 14 const onExit = () => { 15 reject(new Error('Child process exits unexpectly')); 16 }; 17 childProc.on('exit', onExit); 18 childProc.stderr.setEncoding('utf8'); 19 childProc.stderr.on('data', (data) => { 20 const ret = kDebuggerMsgReg.exec(data); 21 childProc.removeListener('exit', onExit); 22 if (ret) { 23 resolve({ 24 childProc, 25 host: ret[1], 26 port: ret[2], 27 }); 28 } 29 }); 30 }); 31} 32 33// process.debugPort is our proxy for "the version of node used to run this 34// test suite doesn't support SIGUSR1 for enabling --inspect for a process". 35const defaultsToOldProtocol = process.debugPort === 5858; 36 37test('examples/alive.js', { skip: defaultsToOldProtocol }, (t) => { 38 const script = Path.join('examples', 'alive.js'); 39 let cli = null; 40 let target = null; 41 42 function cleanup(error) { 43 if (cli) { 44 cli.quit(); 45 cli = null; 46 } 47 if (target) { 48 target.kill(); 49 target = null; 50 } 51 if (error) throw error; 52 } 53 54 return launchTarget('--inspect=0', script) 55 .then(({ childProc, host, port }) => { 56 target = childProc; 57 cli = startCLI([`${host || '127.0.0.1'}:${port}`]); 58 return cli.waitForPrompt(); 59 }) 60 .then(() => cli.command('sb("alive.js", 3)')) 61 .then(() => cli.waitFor(/break/)) 62 .then(() => cli.waitForPrompt()) 63 .then(() => { 64 t.match( 65 cli.output, 66 '> 3 ++x;', 67 'marks the 3rd line'); 68 }) 69 .then(() => cleanup()) 70 .then(null, cleanup); 71}); 72