...
1#!/bin/bash
2
3# Modified version of chef-runner/script/coverage
4# Copyright 2004 Mathias Lafeldt
5# Apache License 2.0
6# Source: https://github.com/mlafeldt/chef-runner/blob/v0.7.0/script/coverage
7
8# Generate test coverage statistics for Go packages.
9#
10# Works around the fact that `go test -coverprofile` currently does not work
11# with multiple packages, see https://code.google.com/p/go/issues/detail?id=6909
12#
13# Usage: script/coverage [--html|--coveralls]
14#
15# --html Additionally create HTML report and open it in browser
16# --coveralls Push coverage statistics to coveralls.io
17#
18# Changes: directories ending in .go used to fail
19
20set -e
21
22workdir=.cover
23profile="$workdir/cover.out"
24mode=count
25
26generate_cover_data() {
27 rm -rf "$workdir"
28 mkdir "$workdir"
29
30 for pkg in "$@"; do
31 f="$workdir/$(echo $pkg | tr / -).cover"
32 go test -covermode="$mode" -coverprofile="$f" "$pkg/"
33 done
34
35 echo "mode: $mode" >"$profile"
36 grep -h -v "^mode:" "$workdir"/*.cover >>"$profile"
37}
38
39show_cover_report() {
40 go tool cover -${1}="$profile"
41}
42
43push_to_coveralls() {
44 echo "Pushing coverage statistics to coveralls.io"
45 goveralls -coverprofile="$profile"
46}
47
48generate_cover_data $(go list ./...)
49show_cover_report func
50case "$1" in
51"")
52 ;;
53--html)
54 show_cover_report html ;;
55--coveralls)
56 push_to_coveralls ;;
57*)
58 echo >&2 "error: invalid option: $1"; exit 1 ;;
59esac
View as plain text