Skip to content

Commit 77a4fdb

Browse files
firelizzard18h9jiang
authored andcommitted
extension/src/goTest: fix debug subtest at cursor
When the user manually executes the command, `args` is undefined. However the command implementation doesn't actually allow that but this was hidden because `args` was typed as `any`. I updated the typings to make the expected usage clear and fixed the bug. Fixes #3718. Change-Id: Ie0a28fa93985178c32532920b1a74576603fdf8b Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/657395 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Hongxiang Jiang <hxjiang@golang.org> Reviewed-by: Peter Weinberger <pjw@google.com> kokoro-CI: kokoro <noreply+kokoro@google.com>
1 parent eeb3c24 commit 77a4fdb

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ diff between options locally discovered and options available for download.
1515
* Addressed an issue that caused a `Cannot read properties of null (reading 'Token')`
1616
error during command execution when the command result did not include a token.
1717
([Issue 3698](https://github.com/golang/vscode-go/issues/3698))
18+
* Addressed an issue that broke the `Debug Subtest At Cursor` command. ([Issue 3718](https://github.com/golang/vscode-go/issues/3718))
1819

1920
## v0.46.1
2021

extension/src/goTest.ts

+40-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
SuiteToTestMap,
2626
getTestFunctions
2727
} from './testUtils';
28+
import type { GoRunTestCodeLensProvider } from './goRunTestCodelens';
2829

2930
// lastTestConfig holds a reference to the last executed TestConfig which allows
3031
// the last test to be easily re-executed.
@@ -80,11 +81,27 @@ async function _testAtCursor(
8081
}
8182
}
8283

84+
/**
85+
* Arguments for the run/debug subtest at cursor command.
86+
*/
87+
type SubTestAtCursorArgs = {
88+
/**
89+
* The name of the test that contains the subtest. If unspecified, this will
90+
* be deduced from the cursor location.
91+
*/
92+
functionName?: string;
93+
94+
/**
95+
* The name of the subtest. If unspecified, this will prompt the user.
96+
*/
97+
subTestName?: string;
98+
} & TestAtCursor;
99+
83100
async function _subTestAtCursor(
84101
goCtx: GoExtensionContext,
85102
goConfig: vscode.WorkspaceConfiguration,
86103
cmd: SubTestAtCursorCmd,
87-
args: any
104+
args?: SubTestAtCursorArgs
88105
) {
89106
const editor = vscode.window.activeTextEditor;
90107
if (!editor) {
@@ -100,7 +117,7 @@ async function _subTestAtCursor(
100117
const { testFunctions, suiteToTest } = await getTestFunctionsAndTestSuite(false, goCtx, editor.document);
101118
// We use functionName if it was provided as argument
102119
// Otherwise find any test function containing the cursor.
103-
const currentTestFunctions = args.functionName
120+
const currentTestFunctions = args?.functionName
104121
? testFunctions.filter((func) => func.name === args.functionName)
105122
: testFunctions.filter((func) => func.range.contains(editor.selection.start));
106123
const testFunctionName =
@@ -202,6 +219,16 @@ export function testAtCursorOrPrevious(cmd: TestAtCursorCmd): CommandFactory {
202219
};
203220
}
204221

222+
/**
223+
* Arguments for the run test at cursor command.
224+
*/
225+
type TestAtCursor = {
226+
/**
227+
* Flags to be passed to `go test`.
228+
*/
229+
flags?: string[];
230+
};
231+
205232
/**
206233
* Runs the test at cursor.
207234
*/
@@ -212,7 +239,7 @@ async function runTestAtCursor(
212239
suiteToTest: SuiteToTestMap,
213240
goConfig: vscode.WorkspaceConfiguration,
214241
cmd: TestAtCursorCmd,
215-
args: any
242+
args?: TestAtCursor
216243
) {
217244
const testConfigFns = [testFunctionName];
218245
if (cmd !== 'benchmark' && extractInstanceTestName(testFunctionName)) {
@@ -240,9 +267,17 @@ async function runTestAtCursor(
240267
* @param cmd Whether the command is test or debug.
241268
*/
242269
export function subTestAtCursor(cmd: SubTestAtCursorCmd): CommandFactory {
243-
return (_, goCtx) => async (args: string[]) => {
270+
return (_, goCtx) => async (
271+
/**
272+
* When this command is run manually by the user (e.g. via vscode's
273+
* command pallet), args is undefined. When this command is run via a
274+
* codelens provided by {@link GoRunTestCodeLensProvider}, args
275+
* specifies the function and subtest names.
276+
*/
277+
args?: [SubTestAtCursorArgs]
278+
) => {
244279
try {
245-
return await _subTestAtCursor(goCtx, getGoConfig(), cmd, args);
280+
return await _subTestAtCursor(goCtx, getGoConfig(), cmd, args?.[0]);
246281
} catch (err) {
247282
if (err instanceof NotFoundError) {
248283
vscode.window.showInformationMessage(err.message);

0 commit comments

Comments
 (0)