Skip to content

Commit 1d6148c

Browse files
committed
wip
1 parent 25ff58b commit 1d6148c

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

extension/src/goTest.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import {
1818
getBenchmarkFunctions,
1919
getTestFlags,
2020
getTestFunctionDebugArgs,
21-
getSuiteToTestMap,
22-
getTestFunctions,
21+
getTestFunctionsAndTestSuite,
2322
getTestTags,
2423
goTest,
2524
TestConfig,
26-
SuiteToTestMap
25+
SuiteToTestMap,
26+
getTestFunctions
2727
} from './testUtils';
2828

2929
// lastTestConfig holds a reference to the last executed TestConfig which allows
@@ -54,9 +54,11 @@ async function _testAtCursor(
5454
throw new NotFoundError('No tests found. Current file is not a test file.');
5555
}
5656

57-
const getFunctions = cmd === 'benchmark' ? getBenchmarkFunctions : getTestFunctions;
58-
const testFunctions = (await getFunctions(goCtx, editor.document)) ?? [];
59-
const suiteToTest = await getSuiteToTestMap(goCtx, editor.document);
57+
const { testFunctions, suiteToTest } = await getTestFunctionsAndTestSuite(
58+
cmd === 'benchmark',
59+
goCtx,
60+
editor.document
61+
);
6062
// We use functionName if it was provided as argument
6163
// Otherwise find any test function containing the cursor.
6264
const testFunctionName =
@@ -95,8 +97,7 @@ async function _subTestAtCursor(
9597
}
9698

9799
await editor.document.save();
98-
const testFunctions = (await getTestFunctions(goCtx, editor.document)) ?? [];
99-
const suiteToTest = await getSuiteToTestMap(goCtx, editor.document);
100+
const { testFunctions, suiteToTest } = await getTestFunctionsAndTestSuite(false, goCtx, editor.document);
100101
// We use functionName if it was provided as argument
101102
// Otherwise find any test function containing the cursor.
102103
const currentTestFunctions = testFunctions.filter((func) => func.range.contains(editor.selection.start));

extension/src/goTest/run.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ import vscode = require('vscode');
2121
import { outputChannel } from '../goStatus';
2222
import { isModSupported } from '../goModules';
2323
import { getGoConfig } from '../config';
24-
import {
25-
getBenchmarkFunctions,
26-
getTestFlags,
27-
getSuiteToTestMap,
28-
getTestFunctions,
29-
goTest,
30-
GoTestOutput
31-
} from '../testUtils';
24+
import { getTestFlags, getTestFunctionsAndTestSuite, goTest, GoTestOutput } from '../testUtils';
3225
import { GoTestResolver } from './resolve';
3326
import { dispose, forEachAsync, GoTest, Workspace } from './utils';
3427
import { GoTestProfiler, ProfilingOptions } from './profile';
@@ -168,9 +161,11 @@ export class GoTestRunner {
168161
await doc.save();
169162

170163
const goConfig = getGoConfig(test.uri);
171-
const getFunctions = kind === 'benchmark' ? getBenchmarkFunctions : getTestFunctions;
172-
const testFunctions = await getFunctions(this.goCtx, doc, token);
173-
const suiteToTest = await getSuiteToTestMap(this.goCtx, doc, token);
164+
const { testFunctions, suiteToTest } = await getTestFunctionsAndTestSuite(
165+
kind === 'benchmark',
166+
this.goCtx,
167+
doc
168+
);
174169

175170
// TODO Can we get output from the debug session, in order to check for
176171
// run/pass/fail events?

extension/src/testUtils.ts

+44-5
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,46 @@ export async function getTestFunctions(
155155
doc: vscode.TextDocument,
156156
token?: vscode.CancellationToken
157157
): Promise<vscode.DocumentSymbol[] | undefined> {
158+
const result = await getTestFunctionsAndTestifyHint(goCtx, doc, token);
159+
return result.testFunctions;
160+
}
161+
162+
export async function getTestFunctionsAndTestifyHint(
163+
goCtx: GoExtensionContext,
164+
doc: vscode.TextDocument,
165+
token?: vscode.CancellationToken
166+
): Promise<{ testFunctions?: vscode.DocumentSymbol[]; foundTestifyTestFunction?: boolean }> {
158167
const documentSymbolProvider = GoDocumentSymbolProvider(goCtx, true);
159168
const symbols = await documentSymbolProvider.provideDocumentSymbols(doc);
160169
if (!symbols || symbols.length === 0) {
161-
return;
170+
return {};
162171
}
163172
const symbol = symbols[0];
164173
if (!symbol) {
165-
return;
174+
return {};
166175
}
167176
const children = symbol.children;
168177

169178
// With gopls symbol provider, the symbols have the imports of all
170179
// the package, so suite tests from all files will be found.
171180
const testify = importsTestify(symbols);
172-
return children.filter(
181+
182+
const allTestFunctions = children.filter(
173183
(sym) =>
174-
(sym.kind === vscode.SymbolKind.Function || sym.kind === vscode.SymbolKind.Method) &&
184+
sym.kind === vscode.SymbolKind.Function &&
175185
// Skip TestMain(*testing.M) - see https://github.com/golang/vscode-go/issues/482
176186
!testMainRegex.test(doc.lineAt(sym.range.start.line).text) &&
177-
(testFuncRegex.test(sym.name) || fuzzFuncRegx.test(sym.name) || (testify && testMethodRegex.test(sym.name)))
187+
(testFuncRegex.test(sym.name) || fuzzFuncRegx.test(sym.name))
178188
);
189+
190+
const allTestMethods = testify
191+
? children.filter((sym) => sym.kind === vscode.SymbolKind.Method && testMethodRegex.test(sym.name))
192+
: [];
193+
194+
return {
195+
testFunctions: allTestFunctions.concat(allTestMethods),
196+
foundTestifyTestFunction: allTestMethods.length > 0
197+
};
179198
}
180199

181200
/**
@@ -725,3 +744,23 @@ export function importsTestify(syms: vscode.DocumentSymbol[]): boolean {
725744
(sym.name === '"github.com/stretchr/testify/suite"' || sym.name === 'github.com/stretchr/testify/suite')
726745
);
727746
}
747+
748+
export async function getTestFunctionsAndTestSuite(
749+
isBenchmark: boolean,
750+
goCtx: GoExtensionContext,
751+
document: vscode.TextDocument
752+
): Promise<{ testFunctions: vscode.DocumentSymbol[]; suiteToTest: SuiteToTestMap }> {
753+
if (isBenchmark) {
754+
return {
755+
testFunctions: (await getBenchmarkFunctions(goCtx, document)) ?? [],
756+
suiteToTest: {}
757+
};
758+
}
759+
760+
const { testFunctions, foundTestifyTestFunction } = await getTestFunctionsAndTestifyHint(goCtx, document);
761+
762+
return {
763+
testFunctions: testFunctions ?? [],
764+
suiteToTest: foundTestifyTestFunction ? await getSuiteToTestMap(goCtx, document) : {}
765+
};
766+
}

0 commit comments

Comments
 (0)