@@ -28,6 +28,7 @@ import {
2828} from './utils/pathUtils' ;
2929import { killProcessTree } from './utils/processUtils' ;
3030import { GoExtensionContext } from './context' ;
31+ import type { TestAtCursorCmd } from './goTest' ;
3132
3233const testOutputChannel = vscode . window . createOutputChannel ( 'Go Tests' ) ;
3334const STATUS_BAR_ITEM_NAME = 'Go Test Cancel' ;
@@ -150,7 +151,7 @@ export function getTestTags(goConfig: vscode.WorkspaceConfiguration): string {
150151 * @param the URI of a Go source file.
151152 * @return test function symbols for the source file.
152153 */
153- export async function getTestFunctions (
154+ export async function getTestFunctionsOld (
154155 goCtx : GoExtensionContext ,
155156 doc : vscode . TextDocument ,
156157 token ?: vscode . CancellationToken
@@ -169,6 +170,7 @@ export async function getTestFunctions(
169170 // With gopls symbol provider, the symbols have the imports of all
170171 // the package, so suite tests from all files will be found.
171172 const testify = importsTestify ( symbols ) ;
173+ console . log ( '🅰️ Is testify imported?' , testify , doc . fileName ) ;
172174 return children . filter (
173175 ( sym ) =>
174176 ( sym . kind === vscode . SymbolKind . Function || sym . kind === vscode . SymbolKind . Method ) &&
@@ -178,6 +180,45 @@ export async function getTestFunctions(
178180 ) ;
179181}
180182
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+
181222/**
182223 * Extracts test method name of a suite test function.
183224 * For example a symbol with name "(*testSuite).TestMethod" will return "TestMethod".
@@ -281,6 +322,7 @@ export async function getSuiteToTestMap(
281322 doc : vscode . TextDocument ,
282323 token ?: vscode . CancellationToken
283324) {
325+ console . log ( 'getSuiteToTestMap' ) ;
284326 // Get all the package documents.
285327 const packageDir = path . parse ( doc . fileName ) . dir ;
286328 const packageContent = await fs . readdir ( packageDir , { withFileTypes : true } ) ;
@@ -295,7 +337,7 @@ export async function getSuiteToTestMap(
295337
296338 const suiteToTest : SuiteToTestMap = { } ;
297339 for ( const packageDoc of packageDocs ) {
298- const funcs = await getTestFunctions ( goCtx , packageDoc , token ) ;
340+ const funcs = await getTestFunctionsOld ( goCtx , packageDoc , token ) ;
299341 if ( ! funcs ) {
300342 continue ;
301343 }
@@ -317,6 +359,8 @@ export async function getSuiteToTestMap(
317359 }
318360 }
319361
362+ console . log ( 'getSuiteToTestMap done' ) ;
363+
320364 return suiteToTest ;
321365}
322366
@@ -725,3 +769,24 @@ export function importsTestify(syms: vscode.DocumentSymbol[]): boolean {
725769 ( sym . name === '"github.com/stretchr/testify/suite"' || sym . name === 'github.com/stretchr/testify/suite' )
726770 ) ;
727771}
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