Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/goTest: Add command Test Current Package With Custom Run #1745

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
testCurrentFile,
testCurrentPackage,
testPrevious,
testCurrentPackageWithRun,
testWorkspace
} from './goTest';
import { getConfiguredTools } from './goTools';
Expand Down Expand Up @@ -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();
Expand Down
38 changes: 38 additions & 0 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 4 additions & 1 deletion src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -586,6 +586,9 @@ function targetArgs(testconfig: TestConfig): Array<string> {
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 {
Expand Down