Created
March 21, 2019 16:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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