Skip to content

Commit 6eeb576

Browse files
authored
chore: enforce test coverage (#159)
* chore: enforce test coverage Signed-off-by: Travis Truman <trumant@gmail.com> * fix: Makefile coverage read before coverage execution * chore(ci): raise test coverage requirement to 26% This level reflects the current coverage, up from 24% when this change was originally proposed. --------- Signed-off-by: Travis Truman <trumant@gmail.com>
1 parent f95aa94 commit 6eeb576

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

.github/workflows/build.yaml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ permissions:
1111
pull-requests: read
1212

1313
jobs:
14-
lint:
14+
build:
1515
name: build
1616
runs-on: ubuntu-latest
1717
steps:
@@ -24,4 +24,19 @@ jobs:
2424
- name: build
2525
run: make build
2626
- name: test
27-
run: make test-cov
27+
run: make test-cov
28+
- name: test coverage check
29+
env:
30+
TESTCOVERAGE_THRESHOLD: 26
31+
run: |
32+
echo "Quality Gate: checking test coverage is above threshold ..."
33+
echo "Threshold : $TESTCOVERAGE_THRESHOLD %"
34+
totalCoverage=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'`
35+
echo "Current test coverage : $totalCoverage %"
36+
if (( $(echo "$totalCoverage $TESTCOVERAGE_THRESHOLD" | awk '{print ($1 >= $2)}') )); then
37+
echo "OK"
38+
else
39+
echo "Current test coverage is below threshold. Please add more unit tests or adjust threshold to a lower value."
40+
echo "Failed"
41+
exit 1
42+
fi

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ BUILD_WIN=@env GOOS=windows GOARCH=amd64 go build -o $(PACKNAME).exe
44
BUILD_LINUX=@env GOOS=linux GOARCH=amd64 go build -o $(PACKNAME)
55
BUILD_MAC=@env GOOS=darwin GOARCH=amd64 go build -o $(PACKNAME)-darwin
66

7+
COVERAGE = $(shell go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+')
8+
79
release: package release bin
810
release-candidate: package release-candidate
911
binary: package build
@@ -35,21 +37,24 @@ build:
3537
package: tidy test
3638
@echo " > Packaging static files..."
3739

38-
test:
40+
vet:
3941
@echo " > Validating code ..."
4042
@go vet ./...
43+
44+
test: vet
4145
@go clean -testcache
4246
@go test ./...
4347

4448
tidy:
4549
@echo " > Tidying go.mod ..."
4650
@go mod tidy
4751

48-
test-cov:
52+
test-cov: vet
4953
@echo "Running tests and generating coverage output ..."
5054
@go test ./... -coverprofile coverage.out -covermode count
51-
@sleep 2 # Sleeping to allow for coverage.out file to get generated
52-
@echo "Current test coverage : $(shell go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+') %"
55+
56+
print-cov:
57+
@echo "Current test coverage : $(COVERAGE)%"
5358

5459
release-candidate: tidy test
5560
@echo " > Building release candidate for Linux..."

0 commit comments

Comments
 (0)