X Tutup
Skip to content

Commit 4c96efa

Browse files
author
Olivier Gambier
committed
Better coverage script
- added coveralls integration - more efficient script logic - simplified usage Signed-off-by: Olivier Gambier <viapanda@gmail.com>
1 parent 08208bd commit 4c96efa

File tree

3 files changed

+59
-61
lines changed

3 files changed

+59
-61
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ script:
1212
- script/validate-dco
1313
- script/validate-gofmt
1414
- go test -v -short ./...
15-
- script/generate-coverage
15+
- script/generate-coverage report

script/coverage

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
#!/bin/bash
2-
32
set -e
4-
docker build -t docker-machine .
53

6-
if [[ "$1" == "serve" ]]; then
7-
SERVE=yes
8-
else
9-
SERVE=no
10-
fi
4+
docker build -t docker-machine .
115

126
docker run -it \
13-
-e IN_CONTAINER=yes \
14-
-e SERVE=${SERVE} \
7+
-e BUILDTAGS=${BUILDTAGS} \
158
-p 8000:8000 \
169
--rm docker-machine \
17-
./script/generate-coverage
10+
./script/generate-coverage serve /go/src/github.com/docker/machine

script/generate-coverage

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
11
#!/bin/bash
2-
32
set -e
43

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 -
3343
}
3444

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

Comments
 (0)
X Tutup