Skip to content

Commit ea1c220

Browse files
committed
extension/src: add generate test command using gopls.add_test
Command which uses third party tool "gotests" will be marked as "legacy" in both editor context menu and command palette. Legacy command is "go.test.generate.function.legacy". Instead, the original command will be pointing to the gopls's implementation. For #1594 Change-Id: If2d7ff67ff479ee82da3fd7322a6dad313306928 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/692575 Reviewed-by: Madeline Kalil <mkalil@google.com> kokoro-CI: kokoro <noreply+kokoro@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 02af254 commit ea1c220

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

docs/commands.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,19 @@ Displays test coverage in the current package.
117117

118118
### `Go: Generate Unit Tests For Package`
119119

120-
Generates unit tests for the current package
120+
Generates unit tests for the current package using gotests
121121

122122
### `Go: Generate Unit Tests For File`
123123

124-
Generates unit tests for the current file
124+
Generates unit tests for the current file using gotests
125125

126126
### `Go: Generate Unit Tests For Function`
127127

128-
Generates unit tests for the selected function in the current file
128+
Generates unit tests for the selected function in the current file using gopls
129+
130+
### `Go: Generate Unit Tests For Function (legacy)`
131+
132+
Generates unit tests for the selected function in the current file using gotests
129133

130134
### `Go: Generate Interface Stubs`
131135

extension/package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,17 +336,22 @@
336336
{
337337
"command": "go.test.generate.package",
338338
"title": "Go: Generate Unit Tests For Package",
339-
"description": "Generates unit tests for the current package"
339+
"description": "Generates unit tests for the current package using gotests"
340340
},
341341
{
342342
"command": "go.test.generate.file",
343343
"title": "Go: Generate Unit Tests For File",
344-
"description": "Generates unit tests for the current file"
344+
"description": "Generates unit tests for the current file using gotests"
345345
},
346346
{
347347
"command": "go.test.generate.function",
348348
"title": "Go: Generate Unit Tests For Function",
349-
"description": "Generates unit tests for the selected function in the current file"
349+
"description": "Generates unit tests for the selected function in the current file using gopls"
350+
},
351+
{
352+
"command": "go.test.generate.function.legacy",
353+
"title": "Go: Generate Unit Tests For Function (legacy)",
354+
"description": "Generates unit tests for the selected function in the current file using gotests"
350355
},
351356
{
352357
"command": "go.impl.cursor",
@@ -3536,6 +3541,11 @@
35363541
"command": "go.test.generate.function",
35373542
"group": "Go group 1"
35383543
},
3544+
{
3545+
"when": "editorTextFocus && config.go.editorContextMenuCommands.generateTestForFunction && resourceLangId == go",
3546+
"command": "go.test.generate.function.legacy",
3547+
"group": "Go group 1"
3548+
},
35393549
{
35403550
"when": "editorTextFocus && config.go.editorContextMenuCommands.generateTestForFile && resourceLangId == go",
35413551
"command": "go.test.generate.file",

extension/src/goGenerateTests.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { TelemetryKey, telemetryReporter } from './goTelemetry';
2323

2424
const generatedWord = 'Generated ';
2525

26+
const COMMAND = 'gopls.add_test';
27+
2628
/**
2729
* If current active editor has a Go file, returns the editor.
2830
*/
@@ -105,6 +107,26 @@ export const generateTestCurrentFile: CommandFactory = (ctx, goCtx) => () => {
105107
);
106108
};
107109

110+
/**
111+
* Generates a test for the selected function using 'gopls.add_test'.
112+
*/
113+
export const goplsGenerateTest: CommandFactory = (_, goCtx) => async () => {
114+
if (!goCtx.serverInfo?.Commands?.includes(COMMAND)) {
115+
vscode.window.showWarningMessage(`Please upgrade gopls to use the '${COMMAND}' command`);
116+
return;
117+
}
118+
119+
const editor = checkActiveEditor();
120+
if (!editor) {
121+
return;
122+
}
123+
124+
await vscode.commands.executeCommand(COMMAND, {
125+
URI: editor.document.uri.toString(),
126+
range: editor.selection
127+
});
128+
};
129+
108130
export const generateTestCurrentFunction: CommandFactory = (ctx, goCtx) => async () => {
109131
const editor = checkActiveEditor();
110132
if (!editor) {

extension/src/goMain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<ExtensionA
194194

195195
registerCommand('go.test.generate.package', goGenerateTests.generateTestCurrentPackage);
196196
registerCommand('go.test.generate.file', goGenerateTests.generateTestCurrentFile);
197-
registerCommand('go.test.generate.function', goGenerateTests.generateTestCurrentFunction);
197+
registerCommand('go.test.generate.function.legacy', goGenerateTests.generateTestCurrentFunction);
198+
registerCommand('go.test.generate.function', goGenerateTests.goplsGenerateTest);
198199
registerCommand('go.toggle.test.file', goGenerateTests.toggleTestFile);
199200
registerCommand('go.debug.startSession', commands.startDebugSession);
200201
registerCommand('go.show.commands', commands.showCommands);

extension/test/integration/extension.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ const testAll = (isModuleMode: boolean) => {
251251
});
252252

253253
test('Test Generate unit tests skeleton for file', async () => {
254-
const gotestsPath = getBinPath('gotests');
255254
const uri = vscode.Uri.file(path.join(generateTestsSourcePath, 'generatetests.go'));
256255
const document = await vscode.workspace.openTextDocument(uri);
257256
await vscode.window.showTextDocument(document);
@@ -263,7 +262,6 @@ const testAll = (isModuleMode: boolean) => {
263262
});
264263

265264
test('Test Generate unit tests skeleton for a function', async () => {
266-
const gotestsPath = getBinPath('gotests');
267265
const uri = vscode.Uri.file(path.join(generateFunctionTestSourcePath, 'generatetests.go'));
268266
const document = await vscode.workspace.openTextDocument(uri);
269267
const editor = await vscode.window.showTextDocument(document);
@@ -276,7 +274,6 @@ const testAll = (isModuleMode: boolean) => {
276274
});
277275

278276
test('Test Generate unit tests skeleton for package', async () => {
279-
const gotestsPath = getBinPath('gotests');
280277
const uri = vscode.Uri.file(path.join(generatePackageTestSourcePath, 'generatetests.go'));
281278
const document = await vscode.workspace.openTextDocument(uri);
282279
await vscode.window.showTextDocument(document);

0 commit comments

Comments
 (0)