Skip to content

Instantly share code, notes, and snippets.

@rdumont
Last active June 14, 2021 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdumont/19188045995e155ed35c3427f1546c8e to your computer and use it in GitHub Desktop.
Save rdumont/19188045995e155ed35c3427f1546c8e to your computer and use it in GitHub Desktop.
Go test coverage in VS Code

Requirements

  • Coverage Gutters: vscode extension to display language-agnostic coverage from lcov reports
  • Reflex: to watch files and perform actions on change
  • gcov2lcov: to convert from Go's coverage report format to lcov
  • gotest: optional, you could just use go test

Usage

The path to what you want to test is required.

The rest is optional, but you can pass whatever you'd pass to go test.

# Watch and test all packages
retest ./...

# Watch and test a specific path
retest ./tokenizer

# Watch and test a subset of tests
retest -run=TestSomething/Foo ./tokenizer
function retest
set coverage_folder "./.coverage"
set coverage_output "$coverage_folder/cover.out"
set lcov_path "$coverage_folder/lcov.info"
set default_test_flags "-v -count=1"
set cmd_prepare_folder "mkdir -p $coverage_folder"
set cmd_run_tests "(gotest -cover -covermode=count -coverprofile=$coverage_output -coverpkg=./... $default_test_flags $argv[1..-1] || true)"
set cmd_print_total "go tool cover -func=$coverage_output | tail -1 | awk '{printf \"%s %s %s\\n\", \$1, \$2, \$3}'"
set cmd_convert_report "gcov2lcov -infile $coverage_output -outfile $lcov_path"
reflex -s --decoration=none -r "\\.(go|yml)\$" -- sh -c "clear \
&& $cmd_prepare_folder \
&& $cmd_run_tests \
&& $cmd_print_total \
&& echo Converting coverage... \
&& $cmd_convert_report \
&& echo Done."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment