|
| 1 | +import assert from 'node:assert'; |
| 2 | +import net from 'node:net'; |
| 3 | +import path from 'node:path'; |
| 4 | +import vscode from 'vscode'; |
| 5 | +import { |
| 6 | + FIXTURES_ROOT, |
| 7 | + getRstestExports, |
| 8 | + getTestItemByLabels, |
| 9 | + waitFor, |
| 10 | +} from './helpers'; |
| 11 | + |
| 12 | +suite('Rstest debug launch', () => { |
| 13 | + for (const finish of ['continue', 'cancel', 'stop'] as const) { |
| 14 | + test(`hits a breakpoint with port 9229 occupied and cleans up after ${finish}`, async () => { |
| 15 | + const occupied = net.createServer(); |
| 16 | + await new Promise<void>((resolve, reject) => { |
| 17 | + occupied.once('error', reject); |
| 18 | + occupied.listen(9229, '127.0.0.1', resolve); |
| 19 | + }); |
| 20 | + const cancellation = new vscode.CancellationTokenSource(); |
| 21 | + const sessions = new Map<string, vscode.DebugSession>(); |
| 22 | + const processIds = new Set<number>(); |
| 23 | + const disposables: vscode.Disposable[] = []; |
| 24 | + const file = path.join(FIXTURES_ROOT, 'workspace-1/test/index.test.ts'); |
| 25 | + const breakpoint = new vscode.SourceBreakpoint( |
| 26 | + new vscode.Location(vscode.Uri.file(file), new vscode.Position(5, 0)), |
| 27 | + ); |
| 28 | + let stopped: |
| 29 | + { session: vscode.DebugSession; threadId: number } | undefined; |
| 30 | + const passed: vscode.TestItem[] = []; |
| 31 | + let ended = false; |
| 32 | + let running: Promise<void> | undefined; |
| 33 | + try { |
| 34 | + disposables.push( |
| 35 | + vscode.debug.onDidStartDebugSession((session) => { |
| 36 | + sessions.set(session.id, session); |
| 37 | + }), |
| 38 | + vscode.debug.onDidTerminateDebugSession((session) => |
| 39 | + sessions.delete(session.id), |
| 40 | + ), |
| 41 | + vscode.debug.registerDebugAdapterTrackerFactory('*', { |
| 42 | + // js-debug resolves the public 'node' type to 'pwa-node'. |
| 43 | + createDebugAdapterTracker: (session) => ({ |
| 44 | + onDidSendMessage: (message) => { |
| 45 | + if ( |
| 46 | + message.type === 'event' && |
| 47 | + message.event === 'stopped' && |
| 48 | + message.body.reason === 'breakpoint' |
| 49 | + ) { |
| 50 | + stopped = { session, threadId: message.body.threadId }; |
| 51 | + } |
| 52 | + }, |
| 53 | + }), |
| 54 | + }), |
| 55 | + ); |
| 56 | + vscode.debug.addBreakpoints([breakpoint]); |
| 57 | + const api = await getRstestExports(); |
| 58 | + const item = await waitFor(() => |
| 59 | + getTestItemByLabels(api.testController.items, [ |
| 60 | + 'test', |
| 61 | + 'index.test.ts', |
| 62 | + ]), |
| 63 | + ); |
| 64 | + const request = new vscode.TestRunRequest( |
| 65 | + [item], |
| 66 | + undefined, |
| 67 | + api.debugProfile, |
| 68 | + ); |
| 69 | + // A real TestRun is required for startDebugging's testRun association. |
| 70 | + running = api.startTestRun( |
| 71 | + request, |
| 72 | + cancellation.token, |
| 73 | + false, |
| 74 | + (runRequest) => { |
| 75 | + const run = api.testController.createTestRun(runRequest); |
| 76 | + const originalPassed = run.passed.bind(run); |
| 77 | + const originalEnd = run.end.bind(run); |
| 78 | + run.passed = (test, duration) => { |
| 79 | + passed.push(test); |
| 80 | + originalPassed(test, duration); |
| 81 | + }; |
| 82 | + run.end = () => { |
| 83 | + ended = true; |
| 84 | + originalEnd(); |
| 85 | + }; |
| 86 | + return run; |
| 87 | + }, |
| 88 | + ); |
| 89 | + await waitFor( |
| 90 | + () => assert.ok(stopped, 'debuggee should stop at the breakpoint'), |
| 91 | + { timeoutMs: 60_000 }, |
| 92 | + ); |
| 93 | + assert.ok(stopped); |
| 94 | + const stack = await stopped.session.customRequest('stackTrace', { |
| 95 | + threadId: stopped.threadId, |
| 96 | + }); |
| 97 | + assert.equal( |
| 98 | + path.normalize(stack.stackFrames[0].source.path), |
| 99 | + path.normalize(file), |
| 100 | + ); |
| 101 | + assert.equal(stack.stackFrames[0].line, 6); |
| 102 | + const root = [...sessions.values()].find( |
| 103 | + (session) => session.name === 'Rstest Debug', |
| 104 | + ); |
| 105 | + assert.ok(root); |
| 106 | + for (const session of sessions.values()) { |
| 107 | + if (session.id === root.id) continue; |
| 108 | + const evaluation = await session.customRequest('evaluate', { |
| 109 | + expression: 'process.pid', |
| 110 | + context: 'repl', |
| 111 | + }); |
| 112 | + const pid = Number(evaluation.result); |
| 113 | + assert.ok(Number.isInteger(pid) && pid > 0, evaluation.result); |
| 114 | + processIds.add(pid); |
| 115 | + } |
| 116 | + assert.ok( |
| 117 | + processIds.size >= 2, |
| 118 | + 'worker and pool child must be debugged', |
| 119 | + ); |
| 120 | + if (finish === 'continue') { |
| 121 | + await stopped.session.customRequest('continue', { |
| 122 | + threadId: stopped.threadId, |
| 123 | + }); |
| 124 | + } else if (finish === 'cancel') { |
| 125 | + cancellation.cancel(); |
| 126 | + } else { |
| 127 | + await vscode.debug.stopDebugging(root); |
| 128 | + } |
| 129 | + await waitFor( |
| 130 | + () => { |
| 131 | + assert.equal(ended, true, 'TestRun must end'); |
| 132 | + if (finish === 'continue') { |
| 133 | + assert.deepEqual(passed.map((test) => test.label).sort(), [ |
| 134 | + 'Index', |
| 135 | + 'index.test.ts', |
| 136 | + 'should add two numbers correctly', |
| 137 | + 'should test source code correctly', |
| 138 | + ]); |
| 139 | + } |
| 140 | + assert.equal(sessions.size, 0, 'all debug sessions must end'); |
| 141 | + for (const pid of processIds) { |
| 142 | + assert.throws( |
| 143 | + () => process.kill(pid, 0), |
| 144 | + { code: 'ESRCH' }, |
| 145 | + `process ${pid} must exit`, |
| 146 | + ); |
| 147 | + } |
| 148 | + }, |
| 149 | + { timeoutMs: 60_000 }, |
| 150 | + ); |
| 151 | + await running; |
| 152 | + } finally { |
| 153 | + cancellation.cancel(); |
| 154 | + vscode.debug.removeBreakpoints([breakpoint]); |
| 155 | + await Promise.all( |
| 156 | + [...sessions.values()].map((session) => |
| 157 | + vscode.debug.stopDebugging(session), |
| 158 | + ), |
| 159 | + ); |
| 160 | + for (const disposable of disposables) disposable.dispose(); |
| 161 | + cancellation.dispose(); |
| 162 | + await new Promise<void>((resolve) => occupied.close(() => resolve())); |
| 163 | + } |
| 164 | + }); |
| 165 | + } |
| 166 | +}); |
0 commit comments