@@ -25,6 +25,7 @@ import {
25
25
SuiteToTestMap ,
26
26
getTestFunctions
27
27
} from './testUtils' ;
28
+ import type { GoRunTestCodeLensProvider } from './goRunTestCodelens' ;
28
29
29
30
// lastTestConfig holds a reference to the last executed TestConfig which allows
30
31
// the last test to be easily re-executed.
@@ -80,11 +81,27 @@ async function _testAtCursor(
80
81
}
81
82
}
82
83
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
+
83
100
async function _subTestAtCursor (
84
101
goCtx : GoExtensionContext ,
85
102
goConfig : vscode . WorkspaceConfiguration ,
86
103
cmd : SubTestAtCursorCmd ,
87
- args : any
104
+ args ?: SubTestAtCursorArgs
88
105
) {
89
106
const editor = vscode . window . activeTextEditor ;
90
107
if ( ! editor ) {
@@ -100,7 +117,7 @@ async function _subTestAtCursor(
100
117
const { testFunctions, suiteToTest } = await getTestFunctionsAndTestSuite ( false , goCtx , editor . document ) ;
101
118
// We use functionName if it was provided as argument
102
119
// Otherwise find any test function containing the cursor.
103
- const currentTestFunctions = args . functionName
120
+ const currentTestFunctions = args ? .functionName
104
121
? testFunctions . filter ( ( func ) => func . name === args . functionName )
105
122
: testFunctions . filter ( ( func ) => func . range . contains ( editor . selection . start ) ) ;
106
123
const testFunctionName =
@@ -202,6 +219,16 @@ export function testAtCursorOrPrevious(cmd: TestAtCursorCmd): CommandFactory {
202
219
} ;
203
220
}
204
221
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
+
205
232
/**
206
233
* Runs the test at cursor.
207
234
*/
@@ -212,7 +239,7 @@ async function runTestAtCursor(
212
239
suiteToTest : SuiteToTestMap ,
213
240
goConfig : vscode . WorkspaceConfiguration ,
214
241
cmd : TestAtCursorCmd ,
215
- args : any
242
+ args ?: TestAtCursor
216
243
) {
217
244
const testConfigFns = [ testFunctionName ] ;
218
245
if ( cmd !== 'benchmark' && extractInstanceTestName ( testFunctionName ) ) {
@@ -240,9 +267,17 @@ async function runTestAtCursor(
240
267
* @param cmd Whether the command is test or debug.
241
268
*/
242
269
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
+ ) => {
244
279
try {
245
- return await _subTestAtCursor ( goCtx , getGoConfig ( ) , cmd , args ) ;
280
+ return await _subTestAtCursor ( goCtx , getGoConfig ( ) , cmd , args ?. [ 0 ] ) ;
246
281
} catch ( err ) {
247
282
if ( err instanceof NotFoundError ) {
248
283
vscode . window . showInformationMessage ( err . message ) ;
0 commit comments