Skip to content

Commit 709f6a0

Browse files
authored
Added automated unit test coverage reporting for PRs
1 parent b94392c commit 709f6a0

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

.github/workflows/github-actions.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111
- master
1212
- stable/**
1313
permissions:
14+
pull-requests: write
1415
contents: read
1516
# Optional: allow read access to pull request. Use with `only-new-issues` option.
1617
# pull-requests: read
@@ -120,3 +121,82 @@ jobs:
120121
with:
121122
name: "coverage-report-${{ runner.os }}-${{ github.run_number }}.html"
122123
path: ${{ runner.os }}-coverage.html
124+
125+
coveragecomment:
126+
name: UT Code Coverage Report
127+
runs-on: ubuntu-latest
128+
steps:
129+
- name: Check out code
130+
uses: actions/checkout@v4
131+
132+
- name: Setup Golang for unit test
133+
uses: actions/setup-go@v5
134+
with:
135+
go-version-file: "${{ github.workspace }}/go.mod"
136+
cache: false
137+
138+
- name: Install dependencies
139+
run: |
140+
go install github.com/t-yuki/gocover-cobertura@latest
141+
# ToDo: run tests in /cmd directory
142+
- name: Run the tests (not on Windows)
143+
run: |
144+
set -x
145+
go version
146+
147+
# Define packages to exclude
148+
EXCLUDE_PACKAGES=(
149+
"client/clientset" # Auto-generated client code
150+
"client/informers" # Auto-generated informer code
151+
"client/listers" # Auto-generated lister code
152+
"storage/external_test" # Test code, excluded from production
153+
"ontap/api/azgo" # Auto-generated code for ONTAP API (AZGO)
154+
"ontap/api/rest" # Auto-generated code for ONTAP REST API
155+
"fake" # Mock code for testing purposes
156+
"github.com/netapp/trident/mocks/" # Mock code for unit tests
157+
"github.com/netapp/trident/operator/controllers/provisioner" # Auto-generated operator code
158+
"github.com/netapp/trident/operator/controllers/provisioner/apis/netapp/v1" # Auto-generated API definitions
159+
)
160+
161+
# Build regex pattern from exclusion list
162+
EXCLUDE_REGEX=$(IFS="|"; echo "${EXCLUDE_PACKAGES[*]}")
163+
164+
# Run tests excluding the specified packages
165+
echo mkdir ${{ runner.temp }}/${{ runner.os }}-coverage-binary.out
166+
mkdir ${{ runner.temp }}/${{ runner.os }}-coverage-binary.out
167+
go test $(go list ./... | grep -v -E "$EXCLUDE_REGEX") -covermode=count -test.gocoverdir=${{ runner.temp }}/${{ runner.os }}-coverage-binary.out
168+
go tool covdata textfmt -i=${{ runner.temp }}/${{ runner.os }}-coverage-binary.out -o ${{ runner.os }}-coverage.out
169+
170+
- name: Convert Go coverage to Cobertura XML
171+
run: |
172+
gocover-cobertura < ${{ runner.os }}-coverage.out > cobertura-coverage.xml
173+
174+
- name: Summarize code coverage
175+
uses: irongut/[email protected]
176+
with:
177+
filename: cobertura-coverage.xml
178+
badge: true
179+
fail_below_min: true
180+
format: markdown
181+
hide_complexity: true
182+
indicators: false
183+
output: both
184+
thresholds: '62 80'
185+
186+
- name: Add coverage PR comment
187+
uses: marocchino/sticky-pull-request-comment@v2
188+
if: github.event_name == 'pull_request'
189+
with:
190+
recreate: true
191+
path: code-coverage-results.md
192+
193+
- name: Upload coverage as artifacts
194+
uses: actions/upload-artifact@v4
195+
if: always()
196+
with:
197+
name: code-coverage
198+
path: |
199+
${{ runner.os }}-coverage.out
200+
cobertura-coverage.xml
201+
code-coverage-results.md
202+
retention-days: 5

0 commit comments

Comments
 (0)