|
| 1 | +import { resolve } from 'node:path' |
| 2 | + |
| 3 | +import * as vscode from 'vscode' |
| 4 | + |
| 5 | +import { TestRunner } from './run.js' |
| 6 | +import { WdioExtensionWorker } from './worker.js' |
| 7 | +import { log } from '../utils/logger.js' |
| 8 | + |
| 9 | +let debuggerId = 0 |
| 10 | +const DEBUGGER_NAME = 'wdio-debugger' |
| 11 | +const WORKER_PATH = resolve(__dirname, 'worker/index.cjs') |
| 12 | + |
| 13 | +export class DebugSessionTerminatedError extends Error { |
| 14 | + constructor(message = 'Debug session was terminated during test execution') { |
| 15 | + super(message) |
| 16 | + this.name = 'DebugSessionTerminatedError' |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +export class DebugRunner extends TestRunner { |
| 21 | + private _runController: AbortController | null = null |
| 22 | + |
| 23 | + constructor( |
| 24 | + workspaceFolder: vscode.WorkspaceFolder | undefined, |
| 25 | + token: vscode.CancellationToken, |
| 26 | + workerCwd: string, |
| 27 | + worker = new WdioExtensionDebugWorker(`#DEBUGGER${debuggerId++}`, workerCwd, workspaceFolder, token) |
| 28 | + ) { |
| 29 | + super(worker) |
| 30 | + |
| 31 | + worker.setDebugTerminationCallback(() => { |
| 32 | + if (this._runController) { |
| 33 | + this._runController.abort(new DebugSessionTerminatedError()) |
| 34 | + } |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + public override async run(test: vscode.TestItem) { |
| 39 | + this._runController = new AbortController() |
| 40 | + |
| 41 | + try { |
| 42 | + await this.worker.start() |
| 43 | + await this.worker.waitForStart() |
| 44 | + |
| 45 | + const runPromise = Promise.race([ |
| 46 | + super.run(test), |
| 47 | + new Promise<never>((_, reject) => { |
| 48 | + this._runController!.signal.addEventListener('abort', () => { |
| 49 | + const reason = this._runController!.signal.reason || new DebugSessionTerminatedError() |
| 50 | + reject(reason) |
| 51 | + }) |
| 52 | + }), |
| 53 | + ]) |
| 54 | + |
| 55 | + return await runPromise |
| 56 | + } catch (error) { |
| 57 | + if (error instanceof DebugSessionTerminatedError) { |
| 58 | + log.error('[DEBUG] Test execution interrupted: debug session terminated') |
| 59 | + } |
| 60 | + throw error |
| 61 | + } finally { |
| 62 | + this._runController = null |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public async dispose(): Promise<void> { |
| 67 | + await this.worker.stop() |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export class WdioExtensionDebugWorker extends WdioExtensionWorker { |
| 72 | + private _deferredPromise = promiseWithResolvers<void>() |
| 73 | + private _session: vscode.DebugSession | undefined = undefined |
| 74 | + private _debugTerminationCallback: (() => void) | null = null |
| 75 | + |
| 76 | + constructor( |
| 77 | + cid: string = '#0', |
| 78 | + cwd: string, |
| 79 | + private _workspaceFolder: vscode.WorkspaceFolder | undefined, |
| 80 | + private _token: vscode.CancellationToken |
| 81 | + ) { |
| 82 | + super(cid, cwd) |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set a callback to be called when the debug session terminates |
| 87 | + */ |
| 88 | + public setDebugTerminationCallback(callback: () => void): void { |
| 89 | + this._debugTerminationCallback = callback |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Start the worker process |
| 94 | + */ |
| 95 | + public override async start(): Promise<void> { |
| 96 | + try { |
| 97 | + const wsUrl = await this.getServer() |
| 98 | + |
| 99 | + vscode.debug |
| 100 | + .startDebugging(this._workspaceFolder, { |
| 101 | + __name: DEBUGGER_NAME, |
| 102 | + name: 'Debug Tests', |
| 103 | + type: 'node', |
| 104 | + request: 'launch', |
| 105 | + cwd: this.cwd, |
| 106 | + program: WORKER_PATH, |
| 107 | + autoAttachChildProcesses: true, |
| 108 | + env: { |
| 109 | + ...process.env, |
| 110 | + WDIO_EXTENSION_WORKER_CID: this.cid, |
| 111 | + WDIO_EXTENSION_WORKER_WS_URL: wsUrl, |
| 112 | + FORCE_COLOR: '1', |
| 113 | + ELECTRON_RUN_AS_NODE: undefined, |
| 114 | + }, |
| 115 | + }) |
| 116 | + .then( |
| 117 | + (resolved) => { |
| 118 | + if (resolved) { |
| 119 | + log.info('[DEBUG] Debugging started.') |
| 120 | + } else { |
| 121 | + this._deferredPromise.reject( |
| 122 | + new Error('Failed to start debugging. See output for more information.') |
| 123 | + ) |
| 124 | + log.error('[DEBUG] Debugging failed') |
| 125 | + } |
| 126 | + }, |
| 127 | + (error) => { |
| 128 | + this._deferredPromise.reject(new Error('Failed to start debugging', { cause: error })) |
| 129 | + log.error('[DEBUG] Start debugging failed') |
| 130 | + log.error(error.toString()) |
| 131 | + } |
| 132 | + ) |
| 133 | + |
| 134 | + const onDidStart = vscode.debug.onDidStartDebugSession(async (session) => { |
| 135 | + if (session.configuration.__name !== DEBUGGER_NAME) { |
| 136 | + return |
| 137 | + } |
| 138 | + if (this._token.isCancellationRequested) { |
| 139 | + log.info('[DEBUG] Debugging cancel requested.') |
| 140 | + vscode.debug.stopDebugging(session) |
| 141 | + return |
| 142 | + } |
| 143 | + this._session = session |
| 144 | + this._token.onCancellationRequested(async () => { |
| 145 | + log.info('[DEBUG] Debugging cancel requested.') |
| 146 | + await this.stop() |
| 147 | + await vscode.debug.stopDebugging(session) |
| 148 | + }) |
| 149 | + |
| 150 | + log.debug('[DEBUG] Debugging started completely.') |
| 151 | + this._deferredPromise.resolve() |
| 152 | + }) |
| 153 | + |
| 154 | + const onDidTerminate = vscode.debug.onDidTerminateDebugSession((session) => { |
| 155 | + if (session.configuration.__name !== DEBUGGER_NAME) { |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + if (this._debugTerminationCallback) { |
| 160 | + log.debug('[DEBUG] Debug session terminated, calling termination callback.') |
| 161 | + this._debugTerminationCallback() |
| 162 | + } |
| 163 | + |
| 164 | + this.disposables.reverse().forEach((d) => d.dispose()) |
| 165 | + }) |
| 166 | + |
| 167 | + this.disposables.push(onDidStart, onDidTerminate) |
| 168 | + } catch (error) { |
| 169 | + log.debug(`Failed to start worker: ${error instanceof Error ? error.message : String(error)}`) |
| 170 | + this.stop() |
| 171 | + throw error |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + public override async waitForStart(): Promise<void> { |
| 176 | + log.debug('[DEBUG] Wait for connecting worker process.') |
| 177 | + await super.waitForStart() |
| 178 | + await this._deferredPromise.promise |
| 179 | + log.debug('[DEBUG] The worker process connected.') |
| 180 | + } |
| 181 | + |
| 182 | + public override async stop() { |
| 183 | + await super.stop() |
| 184 | + if (this._session) { |
| 185 | + await vscode.debug.stopDebugging() |
| 186 | + this._session = undefined |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +export function promiseWithResolvers<T>() { |
| 192 | + let resolve!: (value: T | PromiseLike<T>) => void |
| 193 | + let reject!: (reason?: any) => void |
| 194 | + const promise = new Promise<T>((res, rej) => { |
| 195 | + resolve = res |
| 196 | + reject = rej |
| 197 | + }) |
| 198 | + return { promise, resolve, reject } |
| 199 | +} |
0 commit comments