Skip to content

Commit

Permalink
🐛 Stopping a debug session does not clean up the test status
Browse files Browse the repository at this point in the history
Fixes #77
  • Loading branch information
JustinGrote authored Oct 19, 2021
1 parent 8bbe081 commit cc86f24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
20 changes: 13 additions & 7 deletions src/pesterTestController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { join } from 'path'
import {
debug as vscodeDebug,
DebugSession,
Disposable,
Extension,
ExtensionContext,
Expand Down Expand Up @@ -38,6 +40,7 @@ import {
} from './powershellExtensionClient'
import { findTestItem, forAll, getTestItems } from './testItemUtils'
import debounce = require('debounce-promise')
import { Socket } from 'net'
/** A wrapper for the vscode TestController API specific to PowerShell Pester Test Suite.
* This should only be instantiated once in the extension activate method.
*/
Expand Down Expand Up @@ -481,17 +484,20 @@ export class PesterTestController implements Disposable {

if (usePSIC) {
log.debug('Running Script in PSIC:', scriptPath, scriptArgs)
const psListenerPromise = this.returnServer.waitForConnection()

/** Handles situation where the debug adapter is stopped (usually due to user cancel) before the script completes. */
const endSocketAtDebugTerminate = (session: DebugSession) => {
psListenerPromise.then(socket => socket.end())
}

await this.powerShellExtensionClient!.RunCommand(
scriptPath,
scriptArgs,
debug
)

await this.ps.run(
'',
psOutput,
await this.returnServer.waitForConnection()
debug,
endSocketAtDebugTerminate
)
await this.ps.listen(psOutput, await psListenerPromise)
} else {
const script = `& '${scriptPath}' ${scriptArgs.join(' ')}`
log.debug('Running Script in PS Worker:', script)
Expand Down
5 changes: 5 additions & 0 deletions src/powershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ export class PowerShell {
}
}

/** Similar to {@link run} but doesn't execute anything, rather listens on a particular stream for JSON objects to arrive */
async listen(psOutput: IPSOutput, inputStream?: Readable) {
await this.run('', psOutput, inputStream)
}

/** Run a PowerShell script asynchronously, result objects will arrive via the provided PSOutput streams
* the returned Promise will complete when the script has finished running
* @param inputStream
Expand Down
29 changes: 17 additions & 12 deletions src/powershellExtensionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
debug,
DebugConfiguration,
DebugSession,
Extension,
ExtensionContext,
extensions,
Expand Down Expand Up @@ -109,7 +110,7 @@ export class PowerShellExtensionClient {
command: string,
args?: string[],
isDebug?: boolean,
onComplete?: (terminalData: string) => void
onComplete?: (terminalData: DebugSession) => void
) {
// This indirectly loads the PSES extension and console
await this.GetVersionDetails()
Expand All @@ -124,32 +125,36 @@ export class PowerShellExtensionClient {
// We use the PSIC, not the vscode native debug console
internalConsoleOptions: 'neverOpen',
// TODO: Update this deprecation to match with the paths in the arg?
cwd: workspace.rootPath!,
// FIXME: Temporary Test
noDebug: !isDebug
cwd: workspace.rootPath!
// createTemporaryIntegratedConsole: settings.debugging.createTemporaryIntegratedConsole,
// cwd:
// currentDocument.isUntitled
// ? vscode.workspace.rootPath
// : path.dirname(currentDocument.fileName),
}

const terminalData = ''
// FIXME: Figure out another way to capture terminal data, this is a proposed API that will never go stable
// const terminalDataEvent = window.onDidWriteTerminalData(e => {
// if (e.terminal !== psic) {return}
// terminalData += e.data
// })
if (!(await debug.startDebugging(debugConfig.cwd, debugConfig)))
throw new Error('Debug Session did not start as expected')

// TODO: Figure out how to "await" this and return it as a string
const stopDebugEvent = debug.onDidTerminateDebugSession(e => {
// FIXME: Figure out another way to capture terminal data, this is a proposed API that will never go stable
// terminalDataEvent.dispose()
const debugStarted = await debug.startDebugging(
debugConfig.cwd,
debugConfig
)
// HACK: Ideally startDebugging would return the ID of the session
const thisDebugSession = debug.activeDebugSession
if (!debugStarted || !thisDebugSession) {
throw new Error('Debug Session did not start as expected')
}
const stopDebugEvent = debug.onDidTerminateDebugSession(debugSession => {
if (debugSession.id !== thisDebugSession.id) {
return
}
stopDebugEvent.dispose()
if (onComplete) {
onComplete(terminalData)
onComplete(debugSession)
}
})
}
Expand Down

0 comments on commit cc86f24

Please sign in to comment.