Skip to content

Commit 83aa2cb

Browse files
firelizzard18hyangah
authored andcommitted
src/goTestExplorer: implement a test provider for the new test api
What this does: - Implements a test provider using the new API - Populates the test explorer with a list of modules and/or workspaces - Populates each module/workspace/package/file as it is expanded - Creates test entries for the current file, and package/module parents - Runs tests! What this does not: - Debug tests - Handle stretchr suites Issues: - Handling of benchmarks isn't great. But I'm not sure it can get much better without changes to `go test`. - If a test has subtests, I add those subtests. This can get annoying if you have a bunch of subtests. Should this be configurable? Disabling `testing.followRunningTest` can improve this UX. Fixes #1579 Change-Id: I027c7c3b615eda4c528da9739520e6bfd1aa6911 GitHub-Last-Rev: 59af29b GitHub-Pull-Request: #1590 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/330809 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Trust: Hyang-Ah Hana Kim <[email protected]> Trust: Suzy Mueller <[email protected]>
1 parent 945a47e commit 83aa2cb

17 files changed

+1976
-18
lines changed

docs/commands.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ Runs all unit tests in the current file.
5959

6060
Runs all unit tests in the package of the current file.
6161

62+
### `Go Test: Refresh`
63+
64+
Refresh a test in the test explorer. Only available as a context menu option in the test explorer.
65+
6266
### `Go: Benchmark Package`
6367

6468
Runs all benchmarks in the package of the current file.

docs/settings.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,22 @@ Absolute path to a file containing environment variables definitions. File conte
393393
### `go.testEnvVars`
394394

395395
Environment variables that will be passed to the process that runs the Go tests
396+
### `go.testExplorerConcatenateMessages`
397+
398+
If true, test log messages associated with a given location will be shown as a single message.
399+
400+
Default: `true`
401+
### `go.testExplorerPackages`
402+
403+
Control whether packages in the test explorer are presented flat or nested.<br/>
404+
Allowed Options: `flat`, `nested`
405+
406+
Default: `"flat"`
407+
### `go.testExplorerRunBenchmarks`
408+
409+
Include benchmarks when running all tests in a group.
410+
411+
Default: `false`
396412
### `go.testFlags`
397413

398414
Flags to pass to `go test`. If null, then buildFlags will be used. This is not propagated to the language server.

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"Snippets",
1616
"Linters",
1717
"Debuggers",
18-
"Formatters"
18+
"Formatters",
19+
"Testing"
1920
],
2021
"galleryBanner": {
2122
"color": "#F2F2F2",
@@ -70,7 +71,7 @@
7071
"@types/node": "^13.11.1",
7172
"@types/semver": "^7.1.0",
7273
"@types/sinon": "^9.0.0",
73-
"@types/vscode": "^1.52.0",
74+
"@types/vscode": "^1.59.0",
7475
"adm-zip": "^0.4.14",
7576
"fs-extra": "^9.0.0",
7677
"get-port": "^5.1.1",
@@ -232,6 +233,13 @@
232233
"title": "Go: Test Package",
233234
"description": "Runs all unit tests in the package of the current file."
234235
},
236+
{
237+
"command": "go.test.refresh",
238+
"title": "Go Test: Refresh",
239+
"description": "Refresh a test in the test explorer. Only available as a context menu option in the test explorer.",
240+
"category": "Test",
241+
"icon": "$(refresh)"
242+
},
235243
{
236244
"command": "go.benchmark.package",
237245
"title": "Go: Benchmark Package",
@@ -1282,6 +1290,28 @@
12821290
"description": "Flags to pass to `go test`. If null, then buildFlags will be used. This is not propagated to the language server.",
12831291
"scope": "resource"
12841292
},
1293+
"go.testExplorerPackages": {
1294+
"type": "string",
1295+
"enum": [
1296+
"flat",
1297+
"nested"
1298+
],
1299+
"default": "flat",
1300+
"description": "Control whether packages in the test explorer are presented flat or nested.",
1301+
"scope": "resource"
1302+
},
1303+
"go.testExplorerRunBenchmarks": {
1304+
"type": "boolean",
1305+
"default": false,
1306+
"description": "Include benchmarks when running all tests in a group.",
1307+
"scope": "resource"
1308+
},
1309+
"go.testExplorerConcatenateMessages": {
1310+
"type": "boolean",
1311+
"default": true,
1312+
"description": "If true, test log messages associated with a given location will be shown as a single message.",
1313+
"scope": "resource"
1314+
},
12851315
"go.generateTestsFlags": {
12861316
"type": "array",
12871317
"items": {
@@ -2356,6 +2386,12 @@
23562386
}
23572387
},
23582388
"menus": {
2389+
"commandPalette": [
2390+
{
2391+
"command": "go.test.refresh",
2392+
"when": "false"
2393+
}
2394+
],
23592395
"editor/context": [
23602396
{
23612397
"when": "editorTextFocus && config.go.editorContextMenuCommands.toggleTestFile && resourceLangId == go",
@@ -2437,6 +2473,13 @@
24372473
"command": "go.show.commands",
24382474
"group": "Go group 2"
24392475
}
2476+
],
2477+
"testing/item/context": [
2478+
{
2479+
"command": "go.test.refresh",
2480+
"when": "testId =~ /_test\\.go/",
2481+
"group": "inline"
2482+
}
24402483
]
24412484
}
24422485
}

src/goLanguageServer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,9 @@ export async function buildLanguageClient(cfg: BuildLanguageClientOption): Promi
631631
// cause to reorder candiates, which is not ideal.
632632
// Force to use non-empty `label`.
633633
// https://github.com/golang/vscode-go/issues/441
634-
hardcodedFilterText = items[0].label;
634+
let { label } = items[0];
635+
if (typeof label !== 'string') label = label.label;
636+
hardcodedFilterText = label;
635637
}
636638
for (const item of items) {
637639
item.filterText = hardcodedFilterText;

src/goMain.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import { getFormatTool } from './goFormat';
114114
import { resetSurveyConfig, showSurveyConfig, timeMinute } from './goSurvey';
115115
import { ExtensionAPI } from './export';
116116
import extensionAPI from './extensionAPI';
117+
import { isVscodeTestingAPIAvailable, TestExplorer } from './goTestExplorer';
117118

118119
export let buildDiagnosticCollection: vscode.DiagnosticCollection;
119120
export let lintDiagnosticCollection: vscode.DiagnosticCollection;
@@ -335,6 +336,16 @@ If you would like additional configuration for diagnostics from gopls, please se
335336
})
336337
);
337338

339+
if (isVscodeTestingAPIAvailable) {
340+
const testExplorer = TestExplorer.setup(ctx);
341+
342+
ctx.subscriptions.push(
343+
vscode.commands.registerCommand('go.test.refresh', (args) => {
344+
if (args) testExplorer.resolve(args);
345+
})
346+
);
347+
}
348+
338349
ctx.subscriptions.push(
339350
vscode.commands.registerCommand('go.subtest.cursor', (args) => {
340351
const goConfig = getGoConfig();

src/goModules.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ async function runGoModEnv(folderPath: string): Promise<string> {
3636
return resolve('');
3737
}
3838
const [goMod] = stdout.split('\n');
39-
resolve(goMod);
39+
if (goMod === '/dev/null' || goMod === 'NUL') resolve('');
40+
else resolve(goMod);
4041
});
4142
});
4243
}

src/goSuggest.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,14 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider,
122122
return;
123123
}
124124

125+
let { label } = item;
126+
if (typeof label !== 'string') label = label.label;
127+
125128
return runGodoc(
126129
path.dirname(item.fileName),
127130
item.package || path.dirname(item.fileName),
128131
item.receiver,
129-
item.label,
132+
label,
130133
token
131134
)
132135
.then((doc) => {
@@ -358,7 +361,9 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider,
358361
areCompletionsForPackageSymbols = true;
359362
}
360363
if (suggest.class === 'package') {
361-
const possiblePackageImportPaths = this.getPackageImportPath(item.label);
364+
let { label } = item;
365+
if (typeof label !== 'string') label = label.label;
366+
const possiblePackageImportPaths = this.getPackageImportPath(label);
362367
if (possiblePackageImportPaths.length === 1) {
363368
item.detail = possiblePackageImportPaths[0];
364369
}

0 commit comments

Comments
 (0)