Add coverage report for unit tests

parent f9a9b5f4
......@@ -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
......@@ -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:
......@@ -169,6 +179,7 @@ unit tests (no race):
unit tests:
<<: *docker
<<: *unit_tests
stage: test
allow_failure: true
script:
......
......@@ -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 :=
......@@ -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) \
......@@ -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)
......
#!/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
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment