Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit a67e470

Browse files
author
Jan Krems
committed
feat: Restore breakpoints on restart
1 parent 3a752aa commit a67e470

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

lib/internal/inspect-repl.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ function createRepl(inspector) {
432432
}
433433

434434
function handleBreakpointResolved({ breakpointId, location }) {
435+
const script = knownScripts[location.scriptId];
436+
const scriptUrl = script && script.url;
437+
if (scriptUrl) {
438+
Object.assign(location, { scriptUrl });
439+
}
435440
const isExisting = knownBreakpoints.some(bp => {
436441
if (bp.breakpointId === breakpointId) {
437442
Object.assign(bp, { location });
@@ -445,6 +450,11 @@ function createRepl(inspector) {
445450
}
446451

447452
function listBreakpoints() {
453+
if (!knownBreakpoints.length) {
454+
print('No breakpoints yet');
455+
return;
456+
}
457+
448458
function formatLocation(location) {
449459
if (!location) return '<unknown location>';
450460
const script = knownScripts[location.scriptId];
@@ -571,6 +581,19 @@ function createRepl(inspector) {
571581
});
572582
}
573583

584+
function restoreBreakpoints() {
585+
const lastBreakpoints = knownBreakpoints.slice();
586+
knownBreakpoints.length = 0;
587+
const newBreakpoints = lastBreakpoints
588+
.filter(({ location }) => !!location.scriptUrl)
589+
.map(({ location }) =>
590+
setBreakpoint(location.scriptUrl, location.lineNumber + 1));
591+
if (!newBreakpoints.length) return;
592+
Promise.all(newBreakpoints).then((results) => {
593+
print(`${results.length} breakpoints restored.`);
594+
});
595+
}
596+
574597
Debugger.on('paused', ({ callFrames, reason /* , hitBreakpoints */ }) => {
575598
// Save execution context's data
576599
currentBacktrace = Backtrace.from(callFrames);
@@ -765,7 +788,7 @@ function createRepl(inspector) {
765788
});
766789

767790
inspector.client.on('ready', () => {
768-
// TODO: restore breakpoints
791+
restoreBreakpoints();
769792
});
770793

771794
return repl;

test/cli/preserve-breaks.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
const { test } = require('tap');
3+
4+
const startCLI = require('./start-cli');
5+
6+
test('run after quit / restart', (t) => {
7+
const cli = startCLI(['examples/three-lines.js']);
8+
9+
function onFatal(error) {
10+
cli.quit();
11+
throw error;
12+
}
13+
14+
return cli.waitFor(/break/)
15+
.then(() => cli.waitForPrompt())
16+
.then(() => cli.command('breakpoints'))
17+
.then(() => {
18+
t.match(cli.output, 'No breakpoints yet');
19+
})
20+
.then(() => cli.command('sb(2)'))
21+
.then(() => cli.command('sb(3)'))
22+
.then(() => cli.command('breakpoints'))
23+
.then(() => {
24+
t.match(cli.output, '#0 examples/three-lines.js:2');
25+
t.match(cli.output, '#1 examples/three-lines.js:3');
26+
})
27+
.then(() => cli.stepCommand('c')) // hit line 2
28+
.then(() => cli.stepCommand('c')) // hit line 3
29+
.then(() => {
30+
t.match(cli.output, 'break in examples/three-lines.js:3');
31+
})
32+
.then(() => cli.command('restart'))
33+
.then(() => cli.waitFor([/break in examples/, /breakpoints restored/]))
34+
.then(() => cli.waitForPrompt())
35+
.then(() => {
36+
t.match(cli.output, 'break in examples/three-lines.js:1');
37+
})
38+
.then(() => cli.stepCommand('c'))
39+
.then(() => {
40+
t.match(cli.output, 'break in examples/three-lines.js:2');
41+
})
42+
.then(() => cli.stepCommand('c'))
43+
.then(() => {
44+
t.match(cli.output, 'break in examples/three-lines.js:3');
45+
})
46+
.then(() => cli.command('breakpoints'))
47+
.then(() => {
48+
t.match(cli.output, '#0 examples/three-lines.js:2');
49+
t.match(cli.output, '#1 examples/three-lines.js:3');
50+
})
51+
.then(() => cli.quit())
52+
.then(null, onFatal);
53+
});

test/cli/start-cli.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ function startCLI(args) {
4040
},
4141

4242
waitFor(pattern, timeout = 2000) {
43+
function checkPattern(str) {
44+
if (Array.isArray(pattern)) {
45+
return pattern.every((p) => p.test(str));
46+
}
47+
return pattern.test(str);
48+
}
49+
4350
return new Promise((resolve, reject) => {
4451
function checkOutput() {
45-
if (pattern.test(getOutput())) {
52+
if (checkPattern(getOutput())) {
4653
tearDown(); // eslint-disable-line no-use-before-define
4754
resolve();
4855
}

0 commit comments

Comments
 (0)