diff --git a/docs/commands.md b/docs/commands.md index 9e4cbe5408..34f8a0c425 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -71,6 +71,10 @@ Runs all unit tests from all packages in the current workspace. Re-runs the last executed test. +### `Go: Debug Previous` + +Re-runs the last debugged test run through a codelens or "Go: Debug Test at Cursor" command. + ### `Go: Toggle Test Coverage In Current Package` Displays test coverage in the current package. diff --git a/package.json b/package.json index ffd0b4f4b6..0fae7132fd 100644 --- a/package.json +++ b/package.json @@ -232,6 +232,11 @@ "title": "Go: Test Previous", "description": "Re-runs the last executed test." }, + { + "command": "go.debug.previous", + "title": "Go: Debug Previous", + "description": "Re-runs the last debugged test run through a codelens or \"Go: Debug Test at Cursor\" command." + }, { "command": "go.test.coverage", "title": "Go: Toggle Test Coverage In Current Package", diff --git a/src/goMain.ts b/src/goMain.ts index 3cf4872768..104ba62cfb 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -64,6 +64,7 @@ import { GoReferencesCodeLensProvider } from './goReferencesCodelens'; import { GoRunTestCodeLensProvider } from './goRunTestCodelens'; import { disposeGoStatusBar, expandGoStatusBar, outputChannel, updateGoStatusBar } from './goStatus'; import { + debugPrevious, subTestAtCursor, testAtCursor, testCurrentFile, @@ -375,6 +376,12 @@ If you would like additional configuration for diagnostics from gopls, please se }) ); + ctx.subscriptions.push( + vscode.commands.registerCommand('go.debug.previous', () => { + debugPrevious(); + }) + ); + ctx.subscriptions.push( vscode.commands.registerCommand('go.test.coverage', () => { toggleCoverageCurrentPackage(); diff --git a/src/goTest.ts b/src/goTest.ts index d1cb29f25c..e25c9d7b19 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -24,6 +24,11 @@ import { // the last test to be easily re-executed. let lastTestConfig: TestConfig; +// lastDebugConfig holds a reference to the last executed DebugConfiguration which allows +// the last test to be easily re-executed and debugged. +let lastDebugConfig: vscode.DebugConfiguration; +let lastDebugWorkspaceFolder: vscode.WorkspaceFolder; + export type TestAtCursorCmd = 'debug' | 'test' | 'benchmark'; /** @@ -203,6 +208,8 @@ async function debugTestAtCursor( args, buildFlags: buildFlags.join(' ') }; + lastDebugConfig = debugConfig; + lastDebugWorkspaceFolder = workspaceFolder; return await vscode.debug.startDebugging(workspaceFolder, debugConfig); } @@ -327,3 +334,14 @@ export function testPrevious() { console.error(err); }); } + +/** + * Runs the previously executed test. + */ +export function debugPrevious() { + if (!lastDebugConfig) { + vscode.window.showInformationMessage('No test has been recently debugged.'); + return; + } + return vscode.debug.startDebugging(lastDebugWorkspaceFolder, lastDebugConfig); +}