Skip to content

Commit

Permalink
src/goTest.ts: add debug previous command
Browse files Browse the repository at this point in the history
The extension currently provides a "Go: Test Previous" command which
re-executes the last test that was run. To re-run the last debug session
the user can usually just use F5 to run the currently selected launch
config. However when debugging a test without a launch config (either
"Go: Debug Test at Cursor" or through the codelens), they must first
navigate back to the test to debug it.

This change adds a "Go: Debug Previous" command which allows the user
to rerun the last debug session that was executed through debugging
the test at the cursor. This does not include the debug sessions that
occur using the launch.json, since these can be re-run by using the
currently selected launch configuration.

Fixes #798

Change-Id: If44cd9b8d213a9030664a0f3ae0972e567a57f22
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/310185
Trust: Suzy Mueller <suzmue@golang.org>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
  • Loading branch information
suzmue committed Apr 19, 2021
1 parent 015d002 commit afa59f6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { GoReferencesCodeLensProvider } from './goReferencesCodelens';
import { GoRunTestCodeLensProvider } from './goRunTestCodelens';
import { disposeGoStatusBar, expandGoStatusBar, outputChannel, updateGoStatusBar } from './goStatus';
import {
debugPrevious,
subTestAtCursor,
testAtCursor,
testCurrentFile,
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -203,6 +208,8 @@ async function debugTestAtCursor(
args,
buildFlags: buildFlags.join(' ')
};
lastDebugConfig = debugConfig;
lastDebugWorkspaceFolder = workspaceFolder;
return await vscode.debug.startDebugging(workspaceFolder, debugConfig);
}

Expand Down Expand Up @@ -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);
}

0 comments on commit afa59f6

Please # to comment.