From 845089ea062f3351163272027a394997de8e852e Mon Sep 17 00:00:00 2001 From: Vasily Tsybenko Date: Tue, 8 Oct 2024 21:29:01 +0300 Subject: [PATCH] Validate code coverage in CI --- .github/workflows/test.yml | 27 +++++++++++++++++---------- min-coverage.txt | 2 +- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7d7f09c..aeaca86 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,13 +35,20 @@ jobs: run: go mod download - name: Run tests with coverage - run: go test -race -cover -coverprofile="coverage.out" -covermode=atomic -v ./... - - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - files: ./coverage.out - - - name: Calculate coverage - run: go tool cover -func=coverage.out | grep total | awk '{print substr($3, 1, length($3)-1)}' - id: coverage + run: go test -race -cover -coverprofile="coverage.out" -covermode=atomic -v --coverpkg=./... ./... + + - name: Exclude generated code and examples from coverage + if: matrix.os == 'ubuntu-latest' + run: grep -Ev '/pb/|/examples/' ./coverage.out > ./coverage_filtered.out && mv -f ./coverage_filtered.out ./coverage.out + + - name: Check coverage + if: matrix.os == 'ubuntu-latest' + run: | + real_coverage=$(go tool cover -func=coverage.out | grep total | awk '{print substr($3, 1, length($3)-1)}') + min_coverage=$(cat min-coverage.txt) + if (( $(echo "$real_coverage < $min_coverage" | bc -l) )); then + echo "Coverage check failed: $real_coverage% is lower than the required $min_coverage%" + exit 1 + else + echo "Coverage check passed: $real_coverage% meets the minimum requirement of $min_coverage%" + fi diff --git a/min-coverage.txt b/min-coverage.txt index d15a2cc..85322d0 100644 --- a/min-coverage.txt +++ b/min-coverage.txt @@ -1 +1 @@ -80 +79