@@ -28,6 +28,7 @@ import {
28
28
} from './utils/pathUtils' ;
29
29
import { killProcessTree } from './utils/processUtils' ;
30
30
import { GoExtensionContext } from './context' ;
31
+ import type { TestAtCursorCmd } from './goTest' ;
31
32
32
33
const testOutputChannel = vscode . window . createOutputChannel ( 'Go Tests' ) ;
33
34
const STATUS_BAR_ITEM_NAME = 'Go Test Cancel' ;
@@ -150,7 +151,7 @@ export function getTestTags(goConfig: vscode.WorkspaceConfiguration): string {
150
151
* @param the URI of a Go source file.
151
152
* @return test function symbols for the source file.
152
153
*/
153
- export async function getTestFunctions (
154
+ export async function getTestFunctionsOld (
154
155
goCtx : GoExtensionContext ,
155
156
doc : vscode . TextDocument ,
156
157
token ?: vscode . CancellationToken
@@ -169,6 +170,7 @@ export async function getTestFunctions(
169
170
// With gopls symbol provider, the symbols have the imports of all
170
171
// the package, so suite tests from all files will be found.
171
172
const testify = importsTestify ( symbols ) ;
173
+ console . log ( '🅰️ Is testify imported?' , testify , doc . fileName ) ;
172
174
return children . filter (
173
175
( sym ) =>
174
176
( sym . kind === vscode . SymbolKind . Function || sym . kind === vscode . SymbolKind . Method ) &&
@@ -178,6 +180,45 @@ export async function getTestFunctions(
178
180
) ;
179
181
}
180
182
183
+ export async function getTestFunctions (
184
+ goCtx : GoExtensionContext ,
185
+ doc : vscode . TextDocument ,
186
+ token ?: vscode . CancellationToken
187
+ ) : Promise < { testFunctions ?: vscode . DocumentSymbol [ ] ; foundTestifyTestFunction ?: boolean } > {
188
+ const documentSymbolProvider = GoDocumentSymbolProvider ( goCtx , true ) ;
189
+ const symbols = await documentSymbolProvider . provideDocumentSymbols ( doc ) ;
190
+ if ( ! symbols || symbols . length === 0 ) {
191
+ return { } ;
192
+ }
193
+ const symbol = symbols [ 0 ] ;
194
+ if ( ! symbol ) {
195
+ return { } ;
196
+ }
197
+ const children = symbol . children ;
198
+
199
+ // With gopls symbol provider, the symbols have the imports of all
200
+ // the package, so suite tests from all files will be found.
201
+ const testify = importsTestify ( symbols ) ;
202
+ console . log ( '🅰️ Is testify imported?' , testify , doc . fileName ) ;
203
+
204
+ const allTestFunctions = children . filter (
205
+ ( sym ) =>
206
+ sym . kind === vscode . SymbolKind . Function &&
207
+ // Skip TestMain(*testing.M) - see https://github.com/golang/vscode-go/issues/482
208
+ ! testMainRegex . test ( doc . lineAt ( sym . range . start . line ) . text ) &&
209
+ ( testFuncRegex . test ( sym . name ) || fuzzFuncRegx . test ( sym . name ) )
210
+ ) ;
211
+
212
+ const allTestMethods = testify
213
+ ? children . filter ( ( sym ) => sym . kind === vscode . SymbolKind . Method && testMethodRegex . test ( sym . name ) )
214
+ : [ ] ;
215
+
216
+ return {
217
+ testFunctions : allTestFunctions . concat ( allTestMethods ) ,
218
+ foundTestifyTestFunction : allTestMethods . length > 0
219
+ } ;
220
+ }
221
+
181
222
/**
182
223
* Extracts test method name of a suite test function.
183
224
* For example a symbol with name "(*testSuite).TestMethod" will return "TestMethod".
@@ -281,6 +322,7 @@ export async function getSuiteToTestMap(
281
322
doc : vscode . TextDocument ,
282
323
token ?: vscode . CancellationToken
283
324
) {
325
+ console . log ( 'getSuiteToTestMap' ) ;
284
326
// Get all the package documents.
285
327
const packageDir = path . parse ( doc . fileName ) . dir ;
286
328
const packageContent = await fs . readdir ( packageDir , { withFileTypes : true } ) ;
@@ -295,7 +337,7 @@ export async function getSuiteToTestMap(
295
337
296
338
const suiteToTest : SuiteToTestMap = { } ;
297
339
for ( const packageDoc of packageDocs ) {
298
- const funcs = await getTestFunctions ( goCtx , packageDoc , token ) ;
340
+ const funcs = await getTestFunctionsOld ( goCtx , packageDoc , token ) ;
299
341
if ( ! funcs ) {
300
342
continue ;
301
343
}
@@ -317,6 +359,8 @@ export async function getSuiteToTestMap(
317
359
}
318
360
}
319
361
362
+ console . log ( 'getSuiteToTestMap done' ) ;
363
+
320
364
return suiteToTest ;
321
365
}
322
366
@@ -725,3 +769,24 @@ export function importsTestify(syms: vscode.DocumentSymbol[]): boolean {
725
769
( sym . name === '"github.com/stretchr/testify/suite"' || sym . name === 'github.com/stretchr/testify/suite' )
726
770
) ;
727
771
}
772
+
773
+ export async function getTestFunctionsAndTestSuite (
774
+ cmd : TestAtCursorCmd ,
775
+ goCtx : GoExtensionContext ,
776
+ document : vscode . TextDocument
777
+ ) : Promise < { testFunctions : vscode . DocumentSymbol [ ] ; suiteToTest : SuiteToTestMap } > {
778
+ if ( cmd === 'benchmark' ) {
779
+ return {
780
+ testFunctions : ( await getBenchmarkFunctions ( goCtx , document ) ) ?? [ ] ,
781
+ suiteToTest : { }
782
+ } ;
783
+ }
784
+
785
+ const { testFunctions, foundTestifyTestFunction } = await getTestFunctions ( goCtx , document ) ;
786
+ console . log ( 'found testify test func' , foundTestifyTestFunction ) ;
787
+
788
+ return {
789
+ testFunctions : testFunctions ?? [ ] ,
790
+ suiteToTest : foundTestifyTestFunction ? await getSuiteToTestMap ( goCtx , document ) : { }
791
+ } ;
792
+ }
0 commit comments