Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.