From b6e501039597f622b9051c0ba74f30f49d6a8977 Mon Sep 17 00:00:00 2001 From: Clayton Kehoe <118750525+kehoecj@users.noreply.github.com> Date: Wed, 25 Oct 2023 13:25:37 -0500 Subject: [PATCH] Add Unit Test Coverage Threshold and Badge (#72) * adding badge workflow * updating execution rules * fixing yaml error * fixing yaml * running script * switching to inline script * updating coverage threshold * chore: Updated coverage badge. * updating coverage threshold * chore: Updated coverage badge. * updating badge job * chore: Updated coverage badge. * updating badge job * chore: Updated coverage badge. * updates before PR review * fix name * fixing status logic * changing index file * fixing setting the var * chore: Updated coverage badge. * adding index to git add cmd * chore: Updated coverage badge. * adding back verbose unit test output * chore: Updated coverage badge. * push the badge changes to the branch --------- Co-authored-by: GitHub Action --- .github/workflows/go.yml | 65 ++++++++++++++++++++++++++++++++++++++-- README.md | 2 ++ index.md | 2 ++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index fff0d018..d6d8baed 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -12,7 +12,6 @@ on: pull_request: jobs: - download: runs-on: ubuntu-latest steps: @@ -60,13 +59,73 @@ jobs: test: needs: download runs-on: ubuntu-latest + name: Update coverage badge steps: - uses: actions/checkout@v3 + with: + persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token. + fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository. - name: Set up Go uses: actions/setup-go@v4 with: go-version: 1.21 - - name: Test - run: go test -v ./... + - name: Unit test + run: go test -v -cover -coverprofile coverage.out ./... + + - name: Check coverage + id: check-coverage + run: | + # Validate that the coverage is above or at the required threshold + echo "Checking if test coverage is above threshold ..." + echo "Coverage threshold: $COVERAGE_THRESHOLD %" + totalCoverage=`go tool cover -func coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'` + echo "Current test coverage : $totalCoverage %" + if (( $(echo "$COVERAGE_THRESHOLD <= $totalCoverage" | bc -l) )); then + echo "Coverage OK" + else + echo "Current test coverage is below threshold" + exit 1 + fi + + echo "total_coverage=$totalCoverage" >> "$GITHUB_OUTPUT" + env: + COVERAGE_THRESHOLD: 95 + + - name: Create badge img tag and apply to README files + id: generate-badge + run: | + # Create Badge URL + # Badge will always be green because of coverage threshold check + # so we just have to populate the total coverage + totalCoverage=${{ steps.check-coverage.outputs.total_coverage }} + BADGE_URL=https://img.shields.io/badge/Coverage-$totalCoverage%25-brightgreen + BADGE_IMG_TAG="\"Code" + + # Update README.md and index.md + for markdown_file in README.md index.md; do + sed -i "/id=\"cov\"/c\\${BADGE_IMG_TAG}" $markdown_file + done + + # Check to see if files were updated + if [[ `git status --porcelain` ]]; then + echo "badge_updates=true" >> "$GITHUB_OUTPUT" + else + echo "badge_updates=false" >> "$GITHUB_OUTPUT" + fi + + - name: Commit changes + if: steps.generate-badge.outputs.badge_updates == 'true' + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add README.md index.md + git commit -m "chore: Updated coverage badge." + + - name: Push changes + if: steps.generate-badge.outputs.badge_updates == 'true' + uses: ad-m/github-push-action@master + with: + github_token: ${{ github.token }} + branch: ${{ github.ref }} diff --git a/README.md b/README.md index 7685740c..9391a84e 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@

+Code Coverage + Apache 2 License diff --git a/index.md b/index.md index b81f5849..58d3d862 100644 --- a/index.md +++ b/index.md @@ -17,6 +17,8 @@

+Code Coverage + Apache 2 License