Skip to content

Commit

Permalink
chore: dispose when session is terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed May 7, 2024
1 parent 7635240 commit c9f757c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 122 deletions.
93 changes: 78 additions & 15 deletions src/debug/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { log } from '../log'
import { getConfig } from '../config'
import type { WorkerRunnerOptions } from '../worker/types'
import { TestRunner } from '../runner/runner'
import type { TestDebugManager } from './debugManager'
import { findNode } from '../utils'
import { debuggerPath } from '../constants'

export async function createDebugAPI(
export async function debugTests(
controller: vscode.TestController,
tree: TestTree,
debugManager: TestDebugManager,
pkg: VitestPackage,

request: vscode.TestRunRequest,
Expand All @@ -26,17 +26,68 @@ export async function createDebugAPI(
const port = await getPort()
const server = createServer().listen(port)
const wss = new WebSocketServer({ server })
const address = `ws://localhost:${port}`
const wsAddress = `ws://localhost:${port}`

const config = getConfig(pkg.folder)
const promise = Promise.withResolvers<void>()

const execPath = getConfig().nodeExecutable || await findNode(vscode.workspace.workspaceFile?.fsPath || pkg.folder.uri.fsPath)
const env = config.env || {}

const debugConfig = {
type: 'pwa-node',
request: 'launch',
name: 'Debug Tests',
autoAttachChildProcesses: true,
skipFiles: config.debugExclude,
smartStep: true,
runtimeExecutable: execPath,
program: debuggerPath,
__name: 'Vitest',
env: {
...process.env,
...env,
VITEST_VSCODE: 'true',
VITEST_WS_ADDRESS: wsAddress,
// same env var as `startVitest`
// https://github.com/vitest-dev/vitest/blob/5c7e9ca05491aeda225ce4616f06eefcd068c0b4/packages/vitest/src/node/cli/cli-api.ts
TEST: 'true',
VITEST: 'true',
NODE_ENV: env.NODE_ENV ?? process.env.NODE_ENV ?? 'test',
},
}

log.info(`[DEBUG] Starting debugging`)

vscode.debug.startDebugging(
pkg.folder,
debugConfig,
{ suppressDebugView: true },
).then(
(fulfilled) => {
if (fulfilled) {
log.info('[DEBUG] Debugging started')
promise.resolve()
}
else {
promise.reject(new Error('Failed to start debugging. See output for more information.'))
log.error('[DEBUG] Debugging failed')
}
},
(err) => {
promise.reject(new Error('Failed to start debugging', { cause: err }))
log.error('[DEBUG] Start debugging failed')
log.error(err.toString())
},
)

debugManager.startDebugging(pkg, address)
const disposables: vscode.Disposable[] = []

// TODO: dispose this when debugging is stopped
vscode.debug.onDidStartDebugSession(async (session) => {
const onDidStart = vscode.debug.onDidStartDebugSession(async (session) => {
if (session.configuration.__name !== 'Vitest')
return
// TODO
if (token.isCancellationRequested) {
vscode.debug.stopDebugging()
vscode.debug.stopDebugging(session)
return
}
const vitest = await startWebsocketServer(wss, pkg)
Expand All @@ -45,18 +96,30 @@ export async function createDebugAPI(
controller,
tree,
api,
debugManager,
)
disposables.push(api, runner)

token.onCancellationRequested(async () => {
await vitest.rpc.close()
await vscode.debug.stopDebugging(session)
})

await runner.runTests(request, token)
await vitest.rpc.close()
await vscode.debug.stopDebugging(session)

if (!token.isCancellationRequested) {
await vitest.rpc.close()
await vscode.debug.stopDebugging(session)
}
})

vscode.debug.onDidTerminateDebugSession((_session) => {
if (_session.configuration.__name !== 'Vitest')
const onDidTerminate = vscode.debug.onDidTerminateDebugSession((session) => {
if (session.configuration.__name !== 'Vitest')
return
debugManager.stop()
disposables.forEach(d => d.dispose())
server.close()
})

disposables.push(onDidStart, onDidTerminate)
}

function startWebsocketServer(wss: WebSocketServer, pkg: VitestPackage) {
Expand Down
90 changes: 0 additions & 90 deletions src/debug/debugManager.ts

This file was deleted.

18 changes: 3 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import { resolveVitestPackages } from './api/pkg'
import type { TestFile } from './testTreeData'
import { getTestData } from './testTreeData'
import { TagsManager } from './tagsManager'
import { TestDebugManager } from './debug/debugManager'
import { coverageContext } from './coverage'
import { createDebugAPI } from './debug/api'
import { debugTests } from './debug/api'

export async function activate(context: vscode.ExtensionContext) {
const extension = new VitestExtension()
Expand All @@ -33,7 +32,6 @@ class VitestExtension {

private testTree: TestTree
private tagsManager: TagsManager
private debugManager: TestDebugManager
private api: VitestAPI | undefined

private disposables: vscode.Disposable[] = []
Expand All @@ -48,7 +46,6 @@ class VitestExtension {
this.loadingTestItem.sortText = '.0' // show it first
this.testTree = new TestTree(this.testController, this.loadingTestItem)
this.tagsManager = new TagsManager(this.testTree)
this.debugManager = new TestDebugManager()
}

private async defineTestProfiles(showWarning: boolean) {
Expand Down Expand Up @@ -129,7 +126,6 @@ class VitestExtension {
this.testController,
this.testTree,
api,
this.debugManager,
)

const prefix = api.prefix
Expand All @@ -155,22 +151,14 @@ class VitestExtension {
() => {},
false,
undefined,
false, // disable continues debugging
false, // continues debugging is not supported
)
}
debugProfile.tag = api.tag
debugProfile.runHandler = async (request, token) => {
// if (!gte(api.version, minimumDebugVersion)) {
// vscode.window.showWarningMessage(
// `Debugging is not supported for Vitest v${api.version}. Please update Vitest to v${minimumDebugVersion} or higher.`,
// )
// return
// }

await createDebugAPI(
await debugTests(
this.testController,
this.testTree,
this.debugManager,
api.package,

request,
Expand Down
2 changes: 0 additions & 2 deletions src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { VitestFolderAPI } from '../api'
import { log } from '../log'
import { showVitestError } from '../utils'
import { coverageContext, readCoverageReport } from '../coverage'
import type { TestDebugManager } from '../debug/debugManager'

export class TestRunner extends vscode.Disposable {
private continuousRequests = new Set<vscode.TestRunRequest>()
Expand All @@ -26,7 +25,6 @@ export class TestRunner extends vscode.Disposable {
private readonly controller: vscode.TestController,
private readonly tree: TestTree,
private readonly api: VitestFolderAPI,
private readonly debug: TestDebugManager,
) {
super(() => {
api.clearListeners()
Expand Down

0 comments on commit c9f757c

Please sign in to comment.