From 325efe45f47aa4f77537bb0ede74337dc47a002c Mon Sep 17 00:00:00 2001 From: tanryberdi Date: Thu, 19 Jan 2023 11:04:31 +0500 Subject: [PATCH] Feat: Script for Coverage check #22 - Added script for CI for test coverage Signed-off-by: Tanryberdi See #22 --- .github/workflows/test.yml | 19 ++++++++++++++++++- Makefile | 4 ++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 557b4d3c..7dc7cd67 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,4 +15,21 @@ jobs: - name: Checkout Code uses: actions/checkout@v3.3.0 - name: Unit Tests - run: go test -v -race ./... + run: | + sudo go test ./... -coverprofile coverage.out -covermode count + sudo go tool cover -func coverage.out + - name: Quality Gate - Test coverage shall be above threshold + env: + TESTCOVERAGE_THRESHOLD: 75 + run: | + echo "Quality Gate: checking test coverage is above threshold ..." + echo "Threshold : $TESTCOVERAGE_THRESHOLD %" + totalCoverage=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'` + echo "Current test coverage : $totalCoverage %" + if (( $(echo "$totalCoverage $TESTCOVERAGE_THRESHOLD" | awk '{print ($1 > $2)}') )); then + echo "OK" + else + echo "Current test coverage is below threshold. Please add more unit tests or adjust threshold to a lower value." + echo "Failed" + exit 1 + fi \ No newline at end of file diff --git a/Makefile b/Makefile index 0cae8d34..a0ce39a7 100644 --- a/Makefile +++ b/Makefile @@ -5,5 +5,5 @@ lint: test: @echo "Testing ..." @go clean -testcache - @go test -v -race -coverprofile cover.out ./... - @go tool cover -func=cover.out + @go test -v -race -coverprofile coverage.out ./... + @go tool cover -func=coverage.out