diff --git a/package.json b/package.json index 59b07f91d1..e3c0e2c9a2 100644 --- a/package.json +++ b/package.json @@ -208,6 +208,11 @@ "title": "Go: Test Function At Cursor or Test Previous", "description": "Runs a unit test at the cursor if one is found, otherwise re-runs the last executed test." }, + { + "command": "go.test.packageWithRun", + "title": "Go: Test Current Package With Custom -run", + "description": "Runs unit tests in the package of the current file for custom -run flag." + }, { "command": "go.subtest.cursor", "title": "Go: Subtest At Cursor", diff --git a/src/goMain.ts b/src/goMain.ts index 8300c5b3b4..9994cd70f0 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -68,6 +68,7 @@ import { testCurrentFile, testCurrentPackage, testPrevious, + testCurrentPackageWithRun, testWorkspace } from './goTest'; import { getConfiguredTools } from './goTools'; @@ -387,6 +388,14 @@ If you would like additional configuration for diagnostics from gopls, please se }) ); + ctx.subscriptions.push( + vscode.commands.registerCommand('go.test.packageWithRun', (args) => { + const goConfig = getGoConfig(); + const isBenchmark = false; + testCurrentPackageWithRun(goConfig, isBenchmark, args); + }) + ); + ctx.subscriptions.push( vscode.commands.registerCommand('go.test.file', (args) => { const goConfig = getGoConfig(); diff --git a/src/goTest.ts b/src/goTest.ts index 325c08c25b..1d578122ca 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -373,3 +373,41 @@ export function debugPrevious() { } return vscode.debug.startDebugging(lastDebugWorkspaceFolder, lastDebugConfig); } + +/** + * Runs tests in the current package for custom -run flag. + * + * @param goConfig Configuration for the Go extension. + */ +export async function testCurrentPackageWithRun( + goConfig: vscode.WorkspaceConfiguration, + isBenchmark: boolean, + args: any +) { + const editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage('No editor is active.'); + return; + } + + const customRunFlagArg = await vscode.window.showInputBox({ + value: '', + prompt: 'Enter custom -run flag' + }); + if (!customRunFlagArg) { + return; + } + + const isMod = await isModSupported(editor.document.uri); + const testConfig: TestConfig = { + goConfig, + dir: path.dirname(editor.document.fileName), + flags: getTestFlags(goConfig, args), + isBenchmark, + functions: customRunFlagArg, + isMod + }; + // Remember this config as the last executed test. + lastTestConfig = testConfig; + return goTest(testConfig); +} diff --git a/src/testUtils.ts b/src/testUtils.ts index ef91f6d8a1..764c0c07d8 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -60,7 +60,7 @@ export interface TestConfig { /** * Specific function names to test. */ - functions?: string[]; + functions?: string[] | string; /** * Test was not requested explicitly. The output should not appear in the UI. */ @@ -586,6 +586,9 @@ function targetArgs(testconfig: TestConfig): Array { let params: string[] = []; if (testconfig.functions) { + if (typeof testconfig.functions === 'string') { + return ['-run', testconfig.functions]; + } if (testconfig.isBenchmark) { params = ['-bench', util.format('^(%s)$', testconfig.functions.join('|'))]; } else {