Skip to content

Commit

Permalink
src/goTestExplorer: implement a test provider for the new test api
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
firelizzard18 authored and hyangah committed Aug 17, 2021
1 parent 945a47e commit 83aa2cb
Show file tree
Hide file tree
Showing 17 changed files with 1,976 additions and 18 deletions.
4 changes: 4 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Runs all unit tests in the current file.

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

### `Go Test: Refresh`

Refresh a test in the test explorer. Only available as a context menu option in the test explorer.

### `Go: Benchmark Package`

Runs all benchmarks in the package of the current file.
Expand Down
16 changes: 16 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,22 @@ Absolute path to a file containing environment variables definitions. File conte
### `go.testEnvVars`

Environment variables that will be passed to the process that runs the Go tests
### `go.testExplorerConcatenateMessages`

If true, test log messages associated with a given location will be shown as a single message.

Default: `true`
### `go.testExplorerPackages`

Control whether packages in the test explorer are presented flat or nested.<br/>
Allowed Options: `flat`, `nested`

Default: `"flat"`
### `go.testExplorerRunBenchmarks`

Include benchmarks when running all tests in a group.

Default: `false`
### `go.testFlags`

Flags to pass to `go test`. If null, then buildFlags will be used. This is not propagated to the language server.
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 45 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"Snippets",
"Linters",
"Debuggers",
"Formatters"
"Formatters",
"Testing"
],
"galleryBanner": {
"color": "#F2F2F2",
Expand Down Expand Up @@ -70,7 +71,7 @@
"@types/node": "^13.11.1",
"@types/semver": "^7.1.0",
"@types/sinon": "^9.0.0",
"@types/vscode": "^1.52.0",
"@types/vscode": "^1.59.0",
"adm-zip": "^0.4.14",
"fs-extra": "^9.0.0",
"get-port": "^5.1.1",
Expand Down Expand Up @@ -232,6 +233,13 @@
"title": "Go: Test Package",
"description": "Runs all unit tests in the package of the current file."
},
{
"command": "go.test.refresh",
"title": "Go Test: Refresh",
"description": "Refresh a test in the test explorer. Only available as a context menu option in the test explorer.",
"category": "Test",
"icon": "$(refresh)"
},
{
"command": "go.benchmark.package",
"title": "Go: Benchmark Package",
Expand Down Expand Up @@ -1282,6 +1290,28 @@
"description": "Flags to pass to `go test`. If null, then buildFlags will be used. This is not propagated to the language server.",
"scope": "resource"
},
"go.testExplorerPackages": {
"type": "string",
"enum": [
"flat",
"nested"
],
"default": "flat",
"description": "Control whether packages in the test explorer are presented flat or nested.",
"scope": "resource"
},
"go.testExplorerRunBenchmarks": {
"type": "boolean",
"default": false,
"description": "Include benchmarks when running all tests in a group.",
"scope": "resource"
},
"go.testExplorerConcatenateMessages": {
"type": "boolean",
"default": true,
"description": "If true, test log messages associated with a given location will be shown as a single message.",
"scope": "resource"
},
"go.generateTestsFlags": {
"type": "array",
"items": {
Expand Down Expand Up @@ -2356,6 +2386,12 @@
}
},
"menus": {
"commandPalette": [
{
"command": "go.test.refresh",
"when": "false"
}
],
"editor/context": [
{
"when": "editorTextFocus && config.go.editorContextMenuCommands.toggleTestFile && resourceLangId == go",
Expand Down Expand Up @@ -2437,6 +2473,13 @@
"command": "go.show.commands",
"group": "Go group 2"
}
],
"testing/item/context": [
{
"command": "go.test.refresh",
"when": "testId =~ /_test\\.go/",
"group": "inline"
}
]
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/goLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,9 @@ export async function buildLanguageClient(cfg: BuildLanguageClientOption): Promi
// cause to reorder candiates, which is not ideal.
// Force to use non-empty `label`.
// https://github.com/golang/vscode-go/issues/441
hardcodedFilterText = items[0].label;
let { label } = items[0];
if (typeof label !== 'string') label = label.label;
hardcodedFilterText = label;
}
for (const item of items) {
item.filterText = hardcodedFilterText;
Expand Down
11 changes: 11 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import { getFormatTool } from './goFormat';
import { resetSurveyConfig, showSurveyConfig, timeMinute } from './goSurvey';
import { ExtensionAPI } from './export';
import extensionAPI from './extensionAPI';
import { isVscodeTestingAPIAvailable, TestExplorer } from './goTestExplorer';

export let buildDiagnosticCollection: vscode.DiagnosticCollection;
export let lintDiagnosticCollection: vscode.DiagnosticCollection;
Expand Down Expand Up @@ -335,6 +336,16 @@ If you would like additional configuration for diagnostics from gopls, please se
})
);

if (isVscodeTestingAPIAvailable) {
const testExplorer = TestExplorer.setup(ctx);

ctx.subscriptions.push(
vscode.commands.registerCommand('go.test.refresh', (args) => {
if (args) testExplorer.resolve(args);
})
);
}

ctx.subscriptions.push(
vscode.commands.registerCommand('go.subtest.cursor', (args) => {
const goConfig = getGoConfig();
Expand Down
3 changes: 2 additions & 1 deletion src/goModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ async function runGoModEnv(folderPath: string): Promise<string> {
return resolve('');
}
const [goMod] = stdout.split('\n');
resolve(goMod);
if (goMod === '/dev/null' || goMod === 'NUL') resolve('');
else resolve(goMod);
});
});
}
Expand Down
9 changes: 7 additions & 2 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider,
return;
}

let { label } = item;
if (typeof label !== 'string') label = label.label;

return runGodoc(
path.dirname(item.fileName),
item.package || path.dirname(item.fileName),
item.receiver,
item.label,
label,
token
)
.then((doc) => {
Expand Down Expand Up @@ -358,7 +361,9 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider,
areCompletionsForPackageSymbols = true;
}
if (suggest.class === 'package') {
const possiblePackageImportPaths = this.getPackageImportPath(item.label);
let { label } = item;
if (typeof label !== 'string') label = label.label;
const possiblePackageImportPaths = this.getPackageImportPath(label);
if (possiblePackageImportPaths.length === 1) {
item.detail = possiblePackageImportPaths[0];
}
Expand Down
Loading

0 comments on commit 83aa2cb

Please sign in to comment.