|
1 | 1 | #!/bin/bash |
2 | | - |
3 | 2 | set -e |
4 | 3 |
|
5 | | -COVERAGE_DIR=/tmp/coverage |
6 | | - |
7 | | -generate_coverage_for_dir () { |
8 | | - echo |
9 | | - echo "Generating coverage report for $1..." |
10 | | - cd "$1" >/dev/null |
11 | | - PKG_COVERAGE_DIR=${COVERAGE_DIR}/"$1" |
12 | | - PKG_PROFILE=${PKG_COVERAGE_DIR}/profile.txt |
13 | | - mkdir -p ${PKG_COVERAGE_DIR} |
14 | | - go test -covermode=set -coverprofile=${PKG_PROFILE} |
15 | | - go tool cover -html=${PKG_PROFILE} -o ${PKG_COVERAGE_DIR}/index.html |
16 | | - cd - >/dev/null |
17 | | - echo "Done generating coverage for $1." |
18 | | - for f in $(ls "$1"); do |
19 | | - REL_PATH="$1/$f" |
20 | | - for exclude in ${EXCLUDED_DIRS}; do |
21 | | - if [[ "$REL_PATH" == "$exclude" ]]; then |
22 | | - continue 2 |
23 | | - fi |
24 | | - done |
25 | | - # If file is directory and not Godeps |
26 | | - # (don't worry about generating 3rd party code coverage) |
27 | | - if [[ -d "$REL_PATH" ]]; then |
28 | | - # invoke recursively |
29 | | - generate_coverage_for_dir ${REL_PATH} |
30 | | - fi |
31 | | - done |
32 | | - echo |
| 4 | +# Generate coverage for code in ., unless explicitly pointed to something else via the second argument |
| 5 | +DIR=${2:-.} |
| 6 | + |
| 7 | +# Output dir is a temp dir (OSX/Linux compatible), unless explicitly specified through env COVERAGE_DIR |
| 8 | +OUTPUT=${COVERAGE_DIR:-$(mktemp -d 2>/dev/null || mktemp -d -t machine-coverage)} |
| 9 | + |
| 10 | +# Ensure destination exists |
| 11 | +mkdir -p "${OUTPUT}" |
| 12 | + |
| 13 | +# Final cover file, mode |
| 14 | +PROFILE=${OUTPUT}/cover.out |
| 15 | +MODE=set |
| 16 | + |
| 17 | +# Generate coverage |
| 18 | +cover() { |
| 19 | + cd "$DIR" |
| 20 | + |
| 21 | + for PKG in $(go list -tags "${BUILDTAGS}" ./... | grep -v "/vendor/" | grep -v "/Godeps/"); do |
| 22 | + go test -tags "${BUILDTAGS}" -covermode=${MODE} -coverprofile="${OUTPUT}/$(echo ${PKG} | tr "/" "-").cover" "${PKG}" |
| 23 | + done |
| 24 | + |
| 25 | + echo "mode: ${MODE}" > "${PROFILE}" |
| 26 | + grep -h -v "^mode:" "${OUTPUT}"/*.cover >> "${PROFILE}" |
| 27 | + go tool cover -html="${PROFILE}" |
| 28 | + |
| 29 | + cd - |
| 30 | +} |
| 31 | + |
| 32 | +# Send the results to coveralls |
| 33 | +report() { |
| 34 | + go get github.com/mattn/goveralls |
| 35 | + goveralls -service travis-ci -coverprofile="${PROFILE}" |
| 36 | +} |
| 37 | + |
| 38 | +# Useful only if building remote/headless |
| 39 | +serve(){ |
| 40 | + @cd "${DIR}" |
| 41 | + python -m SimpleHTTPServer 8000 |
| 42 | + @cd - |
33 | 43 | } |
34 | 44 |
|
35 | | -if [[ "$IN_CONTAINER" == "yes" ]]; then |
36 | | - cd /go/src/github.com/docker |
37 | | - DIR="machine" |
38 | | -else |
39 | | - DIR="." |
40 | | -fi |
41 | | - |
42 | | -# Script will bomb out on some dirs if there are no buildable source files, |
43 | | -# we shouldn't be checking these anyway so skip over them. |
44 | | -EXCLUDED_DIRS="${DIR}/Godeps ${DIR}/test ${DIR}/docs ${DIR}/script ${DIR}/experimental" |
45 | | - |
46 | | -generate_coverage_for_dir ${DIR} |
47 | | -echo "Done checking and generating coverage!" |
48 | | - |
49 | | -if [[ "$SERVE" == "yes" ]]; then |
50 | | - cd ${COVERAGE_DIR}/machine |
51 | | - echo "*****************************************" |
52 | | - echo "* Serving coverage file on port 8000... *" |
53 | | - echo "*****************************************" |
54 | | - python -m SimpleHTTPServer 8000 |
55 | | -fi |
| 45 | +case "$1" in |
| 46 | + # If in the legacy container, serve as well |
| 47 | + serve) |
| 48 | + cover |
| 49 | + serve |
| 50 | + ;; |
| 51 | + # Travis does report |
| 52 | + report) |
| 53 | + cover |
| 54 | + report |
| 55 | + ;; |
| 56 | + # Default is to just cover, no report |
| 57 | + *) |
| 58 | + cover |
| 59 | + ;; |
| 60 | +esac |
0 commit comments