Skip to content

Commit

Permalink
Add coverage report for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaczukin committed Aug 1, 2018
1 parent f9a9b5f commit f731c10
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ artifacts
tmp/gitlab-test
/.gopath/

/.cover/
codeclimate.json
coverprofile.html
coverprofile.txt
coverprofile.func.txt

# Ignore the generated binary
/gitlab-runner
11 changes: 11 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,18 @@ code_quality:
paths: [gl-code-quality-report.json]
expire_in: 1 week

.unit_tests: &unit_tests
coverage: /total:\s+\(statements\)\s+\d+.\d+\%/
artifacts:
paths:
- coverprofile.html
- coverprofile.txt
- coverprofile.func.txt
expire_in: 1 week

unit tests (no race):
<<: *docker
<<: *unit_tests
stage: test
retry: 2
script:
Expand All @@ -169,6 +179,7 @@ unit tests (no race):

unit tests:
<<: *docker
<<: *unit_tests
stage: test
allow_failure: true
script:
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export VERSION := $(shell ./ci/version)
REVISION := $(shell git rev-parse --short=8 HEAD || echo unknown)
BRANCH := $(shell git show-ref | grep "$(REVISION)" | grep -v HEAD | awk '{print $$2}' | sed 's|refs/remotes/origin/||' | sed 's|refs/heads/||' | sort | head -n 1)
BUILT := $(shell date +%Y-%m-%dT%H:%M:%S%:z)
TESTFLAGS ?= -cover
export TESTFLAGS ?= -cover

LATEST_STABLE_TAG := $(shell git -c versionsort.prereleaseSuffix="-rc" -c versionsort.prereleaseSuffix="-RC" tag -l "v*.*.*" --sort=-v:refname | awk '!/rc/' | head -n 1)
export IS_LATEST :=
Expand Down Expand Up @@ -45,7 +45,7 @@ export PATH := $(GOPATH_BIN):$(PATH)

# Packages in vendor/ are included in ./...
# https://github.com/golang/go/issues/11659
OUR_PACKAGES ?= $(subst _$(BUILD_DIR),$(PKG),$(shell go list ./... | grep -v '/vendor/'))
export OUR_PACKAGES ?= $(subst _$(BUILD_DIR),$(PKG),$(shell go list ./... | grep -v '/vendor/'))

GO_LDFLAGS ?= -X $(COMMON_PACKAGE_NAMESPACE).NAME=$(PACKAGE_NAME) -X $(COMMON_PACKAGE_NAMESPACE).VERSION=$(VERSION) \
-X $(COMMON_PACKAGE_NAMESPACE).REVISION=$(REVISION) -X $(COMMON_PACKAGE_NAMESPACE).BUILT=$(BUILT) \
Expand Down Expand Up @@ -123,7 +123,7 @@ check_race_conditions:

test: $(PKG_BUILD_DIR) docker
# Running tests...
go test $(OUR_PACKAGES) $(TESTFLAGS)
@./scripts/go_test_with_coverage_report

install:
go install --ldflags="$(GO_LDFLAGS)" $(PKG)
Expand Down
30 changes: 30 additions & 0 deletions scripts/go_test_with_coverage_report
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

set -eo pipefail

coverMode="count"

if [[ ${TESTFLAGS} = *"-cover"* ]]; then
rm -rf ".cover/"
mkdir -p ".cover"

if [[ ${TESTFLAGS} = *"-race"* ]]; then
coverMode="atomic"
fi

echo "Starting go tests with coverprofile in ${coverMode} mode"

for pkg in ${OUR_PACKAGES}; do
profileFile=".cover/$(echo ${pkg} | tr "/" "-").cover"

go test -covermode="${coverMode}" -coverprofile="${profileFile}" ${TESTFLAGS} -v ${pkg}
done

echo "mode: ${coverMode}" > coverprofile.txt
grep -h -v "^mode:" .cover/*.cover >> coverprofile.txt

echo "Generating coverprofile.html file"
go tool cover -o coverprofile.html -html=coverprofile.txt
go tool cover -o coverprofile.func.txt -func=coverprofile.txt
grep total coverprofile.func.txt || true
fi

0 comments on commit f731c10

Please sign in to comment.