vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

Show coverage for all packages in workspace

Open alankritjoshi opened this issue 3 years ago • 7 comments

Is your feature request related to a problem? Please describe. Right now I can't make vs code show code coverage across all packages in the workspace. This is true for either scenarios: when I run go test ./... or via "Go: Test all packages in workspace".

Alternatively,

Describe the solution you'd like Provide a setting go.coverOnTestsInWorkspace similar to go.coverOnSingleTestFile, go.coverOnSingleTest that already exist

  • user -> runs -> "Go: Test all packages in workspace"
  • vs code -> applies -> coverage to all relevant packages in the workspace when go.coverOnTestsInWorkspace is true

Describe alternatives you've considered None

Additional context We usually run tests on entire workspace using go test ./... via make.

I couldn't find a way to make vs code listen to a specific coverprofile path. This would have helped in automatically applying coverage to the workspace whenever go test is run in terminal. Right now, I have to do 1. go test -coverprofile=cover.out ./... 2. "Go: Apply Cover Profile" 3. Specify absolute path of the cover profile.

So this feature request would help alleviate it? go test may not work but the existing command "Go: Test all packages in workspace" will be the entry point for showing coverage.

alankritjoshi avatar Jun 22 '22 20:06 alankritjoshi

Cc @pjweinb for test coverage

dle8 avatar Jun 23 '22 15:06 dle8

One workaround is to manually run

go test -coverpkg=all -coverprofile=/tmp/cover ./...

(or use "Go: Test All Packages in Workspace" command after setting "go.testFlags": ["-coverpkg=all", "-coverprofile=/tmp/cover"] --but this will affect test code lenses, etc.)

And load the generated coverprofile (/tmp/cover in this example ) by using "Go: Apply Cover Profile" command.

cc @golang/tools-team I think potentially, we can tweak the "Go: Apply Cover Profile" command (go.apply.coverprofile) to optionally accept a cover profile filename (currently it requires user input), so users can come up with their own test task configuration and invoke go.apply.coverprofile command as a post task command or compound task.

hyangah avatar Jun 23 '22 20:06 hyangah

Upvoting this feature request! Goland has this capability but not VSCode.

For context, my use case is that, I'm testing route handlers from a different package than we're defined to circumvent import cycles. However, this means I can't see the true code coverage of that handler (via test output or VSCode's visual cover highlights) unless I add

"go.testFlags": [
        "-v",
        "-coverpkg=all"
    ]

to settings.json. This is an okay workaround, except I'd like to be able to see the coverage for just that package as well. But so far the only way to do that is to remove the -coverpkg=all flag.

Sophie1142 avatar Dec 29 '22 03:12 Sophie1142

I'm having the same problem that you @Sophie1142. Had you find a better solution?

hantonelli avatar Jul 27 '23 14:07 hantonelli

Thanks for the notes, I had the same issue however my package is quite small, and I just wanted to include the coverage from a parent module when running integration tests. Luckily -coverpkg supports a list of packages so I just set it to one as follows in my .vscode/settings.json.

{
    "go.testFlags": [
        "-v",
        "-coverpkg=github.com/wolfeidau/s3iofs"
    ]
}

wolfeidau avatar Sep 18 '23 18:09 wolfeidau