Skip to content

Instantly share code, notes, and snippets.

@kisom
Created August 31, 2013 18:57
Show Gist options
  • Save kisom/6399969 to your computer and use it in GitHub Desktop.
Save kisom/6399969 to your computer and use it in GitHub Desktop.
Go test code coverage.
#!/bin/sh
if [ -z "$1" ]; then
PKG=$(pwd)
PKG=${PKG##${GOPATH}/src/}
else
PKG=$1
fi
OUTBASE=${PKG##*/}
SLOC=$(gofmt -comments=false $(find ${GOPATH}/src/${PKG} -iname \*.go | xargs)\
| sed -e '/^$/d' | wc -l)
echo "$SLOC lines of code."
go test -test.covermode count -test.coverprofile ${OUTBASE}.out $PKG
if [ -e ${OUTBASE}.out ]; then
go tool cover -html ${OUTBASE}.out -o ${OUTBASE}.html
if [ -e ${OUTBASE}.html ]; then
firefox -new-tab file://$(pwd)/${OUTBASE}.html
else
echo "No HTML file was generated."
fi
else
echo "No coverage profile generated."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment