Created
February 11, 2015 15:23
-
-
Save nelsam/86c608d20760a928b267 to your computer and use it in GitHub Desktop.
A little script to show coverage statistics locally
This file contains 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
function help() { | |
echo " | |
Usage: $0 [-C] [-d] [-b BROWSER] [-p PACKAGE] | |
Options: | |
-t TYPE Use coverage type TYPE. Can be any of: | |
profile - Only write the coverage profile to coverage.out. Implies -d. | |
html - Write standard go coverage tool html output to coverage.html. Implies that coverage.html should not be removed. | |
gocov-html - Write gocov-html coverage tool html output to coverage.html. Implies that coverage.html should not be removed. | |
browser - Launch a browser session for the standard go coverage tool html output. | |
gocov-browser - Launch a browser session for the gocov-html coverage tool html output. | |
-d Does not remove coverage.out or coverage.html after launching the browser. | |
-b BROWSER Use BROWSER as the browser command to run. | |
-p PACKAGE Use PACKAGE as the package pattern for testing. | |
Defaults: | |
BROWSER defaults to \"chrome\", \"chromium\", or \"firefox\" (the first one found in your PATH) | |
PACKAGE defaults to \"./...\" | |
TYPE defaults to \"browser\" | |
" >&2 | |
} | |
function cleanup() { | |
rm profile.out > /dev/null 2>&1 | |
rm coverage.out > /dev/null 2>&1 | |
exit | |
} | |
function cover() { | |
echo "mode: count" > coverage.out | |
for package in $(go list "$packages"); | |
do | |
go test -covermode=count -coverprofile=profile.out "$package" | |
if [[ -f profile.out ]] | |
then | |
if [[ "$(cat profile.out)" != "mode: count" ]] | |
then | |
grep -v "mode: count" profile.out >> coverage.out | |
fi | |
rm profile.out | |
fi | |
done | |
} | |
trap cleanup SIGHUP SIGINT SIGTERM | |
packages="./..." | |
dirty=false | |
browser="" | |
type="browser" | |
while getopts ":b:p:dt:h" opt | |
do | |
case $opt in | |
b) | |
browser="${OPTARG}" | |
;; | |
p) | |
packages="${OPTARG}" | |
;; | |
t) | |
type="${OPTARG}" | |
;; | |
d) | |
dirty=true | |
;; | |
\?) | |
echo "Invalid option: -${OPTARG}" >&2 | |
echo | |
help | |
exit 1 | |
;; | |
:) | |
echo "Option -${OPTARG} requires an argument." >&2 | |
echo | |
help | |
exit 1 | |
;; | |
h) | |
help | |
exit 0 | |
;; | |
esac | |
done | |
launchBrowser=false | |
useGoCov=false | |
makeHtml=true | |
case $type in | |
gocov-browser) | |
useGoCov=true | |
launchBrowser=true | |
;; | |
browser) | |
launchBrowser=true | |
;; | |
gocov-html) | |
useGoCov=true | |
;; | |
html) | |
;; | |
profile) | |
makeHtml=false | |
dirty=true # hack to prevent deletion of coverage.out | |
;; | |
*) | |
echo "Type ${type} not understood" >&2 | |
exit 1 | |
;; | |
esac | |
if [[ "$browser" == "" && $launchBrowser == true ]] | |
then | |
browser="$(which chrome 2>/dev/null)" | |
if [[ $? -eq 1 ]] | |
then | |
browser="$(which chromium 2>/dev/null)" | |
if [[ $? -eq 1 ]] | |
then | |
browser="$(which firefox 2>/dev/null)" | |
if [[ $? -eq 1 ]] | |
then | |
echo "$0 called without -b, but couldn't find chrome, chromium, or firefox in PATH" | |
exit 1 | |
fi | |
fi | |
fi | |
fi | |
cover | |
if [[ $makeHtml == true ]] | |
then | |
if [[ $useGoCov == true ]] | |
then | |
gocov=$(which gocov) | |
if [[ "$?" != "0" ]] | |
then | |
echo "The gocov tool does not seem to be installed. Try \`go get -u github.com/axw/gocov/gocov\`" >&2 | |
exit 1 | |
fi | |
gocovHtml=$(which gocov-html) | |
if [[ "$?" != "0" ]] | |
then | |
echo "The gocov-html tool does not seem to be installed. Try \`go get -u gopkg.in/matm/v1/gocov-html\`" >&2 | |
fi | |
"$gocov" convert coverage.out > coverage.json | |
"$gocovHtml" coverage.json > coverage.html | |
if [[ $dirty == false ]] | |
then | |
rm coverage.json | |
fi | |
else | |
go tool cover -html=coverage.out -o coverage.html | |
fi | |
fi | |
if [[ $launchBrowser == true ]] | |
then | |
"$browser" coverage.html | |
fi | |
if [[ $dirty == false ]] | |
then | |
rm coverage.out | |
if [[ $launchBrowser == true ]] | |
then | |
# Give the browser a few seconds to start up before removing the html file, | |
# since the browser command *probably* returned immediately instead of waiting | |
# until it has opened the file. | |
sleep 10 | |
rm coverage.html | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment