1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #!/bin/bash
- # cover-go - Generate test coverage statistics for all Go packages.
- workdir=.cover
- mkdir -p $workdir
- profile="$workdir/cover.out"
- mode=count
- args=$*
- # Used for saving results when specifying --save-html
- if [[ $args == *--save-html* ]]; then
- for lastarg; do true; done
- outfile=$lastarg
- echo "Outfile: $outfile"
- fi
- generate_cover_data() {
- rm -rf "$workdir"
- mkdir "$workdir"
- for pkg in "$@"; do
- f="$workdir/$(echo $pkg | tr / -).cover"
-
- go test -covermode="$mode" -coverprofile="$f" "$pkg"
-
- ret=$?
- if [ $ret -ne 0 ]; then
- if [[ $args == *--save-html* ]]; then
- echo '<svg width="88" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h41v20H0z"/><path fill="#fc1" d="M41 0h40v20H41z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="20.5" y="14">tests</text><text x="60" y="14">fail</text></g></svg>' > $outfile.svg
- fi
- exit $ret
- fi
- done
- echo "mode: $mode" >"$profile"
- grep -h -v "^mode:" "$workdir"/*.cover >>"$profile"
- }
- show_cover_report() {
- go tool cover -${1}="$profile" -o $profile.${1}
- go tool cover -${1}="$profile"
- }
- save_cover_report() {
- coverage=`go tool cover -func=.cover/cover.out | tee $outfile.txt | grep -En "^total:" | grep -o '[0-9]*.[0-9]*%$'`
- echo $?
- go tool cover -${1}="$profile" -o $outfile.html
- if [ "$coverage" == "100.0%" ]; then
- echo '<svg width="110" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h61v20H0z"/><path fill="#4c1" d="M61 0h50v20H61z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="30.5" y="14">coverage</text><text x="85" y="14">'$coverage'</text></g></svg>' > $outfile.svg
- else
- echo '<svg width="110" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h61v20H0z"/><path fill="#fc1" d="M61 0h50v20H61z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="30.5" y="14">coverage</text><text x="85" y="14">'$coverage'</text></g></svg>' > $outfile.svg
- fi
- }
- generate_cover_data $(go list ./...)
- show_cover_report func
- case "$1" in
- "")
- ;;
- --html)
- show_cover_report html ;;
- --save-html)
- save_cover_report html ;;
- *)
- echo >&2 "error: invalid option: $1"; exit 1 ;;
- esac
|