Skip to content

Commit 9c2e3fb

Browse files
committed
wip
1 parent b1e7ab7 commit 9c2e3fb

File tree

4 files changed

+77
-20
lines changed

4 files changed

+77
-20
lines changed

extension/src/goRunTestCodelens.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { CancellationToken, CodeLens, TextDocument } from 'vscode';
1212
import { getGoConfig } from './config';
1313
import { GoBaseCodeLensProvider } from './goBaseCodelens';
1414
import { GoDocumentSymbolProvider } from './goDocumentSymbols';
15-
import { getBenchmarkFunctions, getTestFunctions } from './testUtils';
15+
import { getBenchmarkFunctions, getTestFunctionsOld } from './testUtils';
1616
import { GoExtensionContext } from './context';
1717
import { GO_MODE } from './goMode';
1818

@@ -97,7 +97,7 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
9797
const testPromise = async (): Promise<CodeLens[]> => {
9898
const codelens: CodeLens[] = [];
9999

100-
const testFunctions = await getTestFunctions(this.goCtx, document, token);
100+
const testFunctions = await getTestFunctionsOld(this.goCtx, document, token);
101101
if (!testFunctions) {
102102
return codelens;
103103
}

extension/src/goTest.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import {
1818
getBenchmarkFunctions,
1919
getTestFlags,
2020
getTestFunctionDebugArgs,
21-
getSuiteToTestMap,
22-
getTestFunctions,
21+
getTestFunctionsAndTestSuite,
22+
getTestFunctionsOld,
2323
getTestTags,
2424
goTest,
2525
TestConfig,
@@ -54,9 +54,7 @@ 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(cmd, goCtx, editor.document);
6058
// We use functionName if it was provided as argument
6159
// Otherwise find any test function containing the cursor.
6260
const testFunctionName =
@@ -95,8 +93,7 @@ async function _subTestAtCursor(
9593
}
9694

9795
await editor.document.save();
98-
const testFunctions = (await getTestFunctions(goCtx, editor.document)) ?? [];
99-
const suiteToTest = await getSuiteToTestMap(goCtx, editor.document);
96+
const { testFunctions, suiteToTest } = await getTestFunctionsAndTestSuite(cmd, goCtx, editor.document);
10097
// We use functionName if it was provided as argument
10198
// Otherwise find any test function containing the cursor.
10299
const currentTestFunctions = testFunctions.filter((func) => func.range.contains(editor.selection.start));
@@ -299,14 +296,9 @@ export async function debugTestAtCursor(
299296
sessionID
300297
};
301298
lastDebugConfig = debugConfig;
302-
console.log('✅ debugTestAtCursor', lastDebugConfig, debugConfig);
303299
lastDebugWorkspaceFolder = workspaceFolder;
304300
vscode.commands.executeCommand('workbench.debug.action.focusRepl');
305-
console.log('after execute command');
306-
console.log('workspace', workspaceFolder);
307-
const result = await vscode.debug.startDebugging(workspaceFolder, debugConfig);
308-
console.log('after debugging:', result);
309-
return result;
301+
return await vscode.debug.startDebugging(workspaceFolder, debugConfig);
310302
}
311303

312304
/**
@@ -393,7 +385,7 @@ export function testCurrentFile(isBenchmark: boolean, getConfig = getGoConfig):
393385
return false;
394386
}
395387

396-
const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions;
388+
const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctionsOld;
397389
const isMod = await isModSupported(editor.document.uri);
398390

399391
return editor.document

extension/src/goTest/run.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
getBenchmarkFunctions,
2626
getTestFlags,
2727
getSuiteToTestMap,
28-
getTestFunctions,
28+
getTestFunctionsOld,
2929
goTest,
3030
GoTestOutput
3131
} from '../testUtils';
@@ -168,7 +168,7 @@ export class GoTestRunner {
168168
await doc.save();
169169

170170
const goConfig = getGoConfig(test.uri);
171-
const getFunctions = kind === 'benchmark' ? getBenchmarkFunctions : getTestFunctions;
171+
const getFunctions = kind === 'benchmark' ? getBenchmarkFunctions : getTestFunctionsOld;
172172
const testFunctions = await getFunctions(this.goCtx, doc, token);
173173
const suiteToTest = await getSuiteToTestMap(this.goCtx, doc, token);
174174

extension/src/testUtils.ts

+67-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from './utils/pathUtils';
2929
import { killProcessTree } from './utils/processUtils';
3030
import { GoExtensionContext } from './context';
31+
import type { TestAtCursorCmd } from './goTest';
3132

3233
const testOutputChannel = vscode.window.createOutputChannel('Go Tests');
3334
const 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

Comments
 (0)