Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nilium
Created March 21, 2019 16:07
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 nilium/73d716eef46ca1f87c4d1a737b35546a to your computer and use it in GitHub Desktop.
Save nilium/73d716eef46ca1f87c4d1a737b35546a to your computer and use it in GitHub Desktop.
Script to generate stub_test.go files for any package without tests to produce 0% coverage for those packages
#!/usr/bin/env bash
gen_stub() {
set -e
local pkg="$1"
local pkgdir="$(go list -f '{{.Dir}}' "$pkg")"
local pkgname="$(go list -f '{{.Name}}' "$pkg")"
local stub="$pkgdir/stub_test.go"
if [ -e "$stub" ]; then
echo "# Cannot create $stub: file already exists" >&2
return 1
fi
echo "$stub"
gofmt >"$stub" <<EOF
// TODO: Add tests for package $pkgname
package $pkgname
import "testing"
func TestStub(*testing.T) {
// This test is a generated stub -- remove it once tests are added to the package
}
EOF
}
if [ $# -eq 0 ]; then
set -- ./...
fi
export -f gen_stub
go list -f '{{if not (or .TestGoFiles .XTestGoFiles)}}{{.ImportPath}}{{end}}' "$@" |
xargs --no-run-if-empty -L1 bash -c 'gen_stub "$@"' _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment