Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr4zySheep committed Jan 21, 2024
1 parent b1e7ab7 commit 9c2e3fb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
4 changes: 2 additions & 2 deletions extension/src/goRunTestCodelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CancellationToken, CodeLens, TextDocument } from 'vscode';
import { getGoConfig } from './config';
import { GoBaseCodeLensProvider } from './goBaseCodelens';
import { GoDocumentSymbolProvider } from './goDocumentSymbols';
import { getBenchmarkFunctions, getTestFunctions } from './testUtils';
import { getBenchmarkFunctions, getTestFunctionsOld } from './testUtils';
import { GoExtensionContext } from './context';
import { GO_MODE } from './goMode';

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

const testFunctions = await getTestFunctions(this.goCtx, document, token);
const testFunctions = await getTestFunctionsOld(this.goCtx, document, token);
if (!testFunctions) {
return codelens;
}
Expand Down
20 changes: 6 additions & 14 deletions extension/src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
getBenchmarkFunctions,
getTestFlags,
getTestFunctionDebugArgs,
getSuiteToTestMap,
getTestFunctions,
getTestFunctionsAndTestSuite,
getTestFunctionsOld,
getTestTags,
goTest,
TestConfig,
Expand Down Expand Up @@ -54,9 +54,7 @@ async function _testAtCursor(
throw new NotFoundError('No tests found. Current file is not a test file.');
}

const getFunctions = cmd === 'benchmark' ? getBenchmarkFunctions : getTestFunctions;
const testFunctions = (await getFunctions(goCtx, editor.document)) ?? [];
const suiteToTest = await getSuiteToTestMap(goCtx, editor.document);
const { testFunctions, suiteToTest } = await getTestFunctionsAndTestSuite(cmd, goCtx, editor.document);
// We use functionName if it was provided as argument
// Otherwise find any test function containing the cursor.
const testFunctionName =
Expand Down Expand Up @@ -95,8 +93,7 @@ async function _subTestAtCursor(
}

await editor.document.save();
const testFunctions = (await getTestFunctions(goCtx, editor.document)) ?? [];
const suiteToTest = await getSuiteToTestMap(goCtx, editor.document);
const { testFunctions, suiteToTest } = await getTestFunctionsAndTestSuite(cmd, goCtx, editor.document);
// We use functionName if it was provided as argument
// Otherwise find any test function containing the cursor.
const currentTestFunctions = testFunctions.filter((func) => func.range.contains(editor.selection.start));
Expand Down Expand Up @@ -299,14 +296,9 @@ export async function debugTestAtCursor(
sessionID
};
lastDebugConfig = debugConfig;
console.log('✅ debugTestAtCursor', lastDebugConfig, debugConfig);
lastDebugWorkspaceFolder = workspaceFolder;
vscode.commands.executeCommand('workbench.debug.action.focusRepl');
console.log('after execute command');
console.log('workspace', workspaceFolder);
const result = await vscode.debug.startDebugging(workspaceFolder, debugConfig);
console.log('after debugging:', result);
return result;
return await vscode.debug.startDebugging(workspaceFolder, debugConfig);
}

/**
Expand Down Expand Up @@ -393,7 +385,7 @@ export function testCurrentFile(isBenchmark: boolean, getConfig = getGoConfig):
return false;
}

const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions;
const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctionsOld;
const isMod = await isModSupported(editor.document.uri);

return editor.document
Expand Down
4 changes: 2 additions & 2 deletions extension/src/goTest/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getBenchmarkFunctions,
getTestFlags,
getSuiteToTestMap,
getTestFunctions,
getTestFunctionsOld,
goTest,
GoTestOutput
} from '../testUtils';
Expand Down Expand Up @@ -168,7 +168,7 @@ export class GoTestRunner {
await doc.save();

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

Expand Down
69 changes: 67 additions & 2 deletions extension/src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from './utils/pathUtils';
import { killProcessTree } from './utils/processUtils';
import { GoExtensionContext } from './context';
import type { TestAtCursorCmd } from './goTest';

const testOutputChannel = vscode.window.createOutputChannel('Go Tests');
const STATUS_BAR_ITEM_NAME = 'Go Test Cancel';
Expand Down Expand Up @@ -150,7 +151,7 @@ export function getTestTags(goConfig: vscode.WorkspaceConfiguration): string {
* @param the URI of a Go source file.
* @return test function symbols for the source file.
*/
export async function getTestFunctions(
export async function getTestFunctionsOld(
goCtx: GoExtensionContext,
doc: vscode.TextDocument,
token?: vscode.CancellationToken
Expand All @@ -169,6 +170,7 @@ export async function getTestFunctions(
// With gopls symbol provider, the symbols have the imports of all
// the package, so suite tests from all files will be found.
const testify = importsTestify(symbols);
console.log('🅰️ Is testify imported?', testify, doc.fileName);
return children.filter(
(sym) =>
(sym.kind === vscode.SymbolKind.Function || sym.kind === vscode.SymbolKind.Method) &&
Expand All @@ -178,6 +180,45 @@ export async function getTestFunctions(
);
}

export async function getTestFunctions(
goCtx: GoExtensionContext,
doc: vscode.TextDocument,
token?: vscode.CancellationToken
): Promise<{ testFunctions?: vscode.DocumentSymbol[]; foundTestifyTestFunction?: boolean }> {
const documentSymbolProvider = GoDocumentSymbolProvider(goCtx, true);
const symbols = await documentSymbolProvider.provideDocumentSymbols(doc);
if (!symbols || symbols.length === 0) {
return {};
}
const symbol = symbols[0];
if (!symbol) {
return {};
}
const children = symbol.children;

// With gopls symbol provider, the symbols have the imports of all
// the package, so suite tests from all files will be found.
const testify = importsTestify(symbols);
console.log('🅰️ Is testify imported?', testify, doc.fileName);

const allTestFunctions = children.filter(
(sym) =>
sym.kind === vscode.SymbolKind.Function &&
// Skip TestMain(*testing.M) - see https://github.com/golang/vscode-go/issues/482
!testMainRegex.test(doc.lineAt(sym.range.start.line).text) &&
(testFuncRegex.test(sym.name) || fuzzFuncRegx.test(sym.name))
);

const allTestMethods = testify
? children.filter((sym) => sym.kind === vscode.SymbolKind.Method && testMethodRegex.test(sym.name))
: [];

return {
testFunctions: allTestFunctions.concat(allTestMethods),
foundTestifyTestFunction: allTestMethods.length > 0
};
}

/**
* Extracts test method name of a suite test function.
* For example a symbol with name "(*testSuite).TestMethod" will return "TestMethod".
Expand Down Expand Up @@ -281,6 +322,7 @@ export async function getSuiteToTestMap(
doc: vscode.TextDocument,
token?: vscode.CancellationToken
) {
console.log('getSuiteToTestMap');
// Get all the package documents.
const packageDir = path.parse(doc.fileName).dir;
const packageContent = await fs.readdir(packageDir, { withFileTypes: true });
Expand All @@ -295,7 +337,7 @@ export async function getSuiteToTestMap(

const suiteToTest: SuiteToTestMap = {};
for (const packageDoc of packageDocs) {
const funcs = await getTestFunctions(goCtx, packageDoc, token);
const funcs = await getTestFunctionsOld(goCtx, packageDoc, token);
if (!funcs) {
continue;
}
Expand All @@ -317,6 +359,8 @@ export async function getSuiteToTestMap(
}
}

console.log('getSuiteToTestMap done');

return suiteToTest;
}

Expand Down Expand Up @@ -725,3 +769,24 @@ export function importsTestify(syms: vscode.DocumentSymbol[]): boolean {
(sym.name === '"github.com/stretchr/testify/suite"' || sym.name === 'github.com/stretchr/testify/suite')
);
}

export async function getTestFunctionsAndTestSuite(
cmd: TestAtCursorCmd,
goCtx: GoExtensionContext,
document: vscode.TextDocument
): Promise<{ testFunctions: vscode.DocumentSymbol[]; suiteToTest: SuiteToTestMap }> {
if (cmd === 'benchmark') {
return {
testFunctions: (await getBenchmarkFunctions(goCtx, document)) ?? [],
suiteToTest: {}
};
}

const { testFunctions, foundTestifyTestFunction } = await getTestFunctions(goCtx, document);
console.log('found testify test func', foundTestifyTestFunction);

return {
testFunctions: testFunctions ?? [],
suiteToTest: foundTestifyTestFunction ? await getSuiteToTestMap(goCtx, document) : {}
};
}

0 comments on commit 9c2e3fb

Please sign in to comment.