-
Notifications
You must be signed in to change notification settings - Fork 3
161 lines (138 loc) · 5.74 KB
/
unit-testing.yml
File metadata and controls
161 lines (138 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
name: 'Java SDK - Unit Testing'
# This workflow runs ONLY unit tests (excludes integration tests ending with IT.java)
# Integration tests require network access and valid .env credentials
on:
pull_request:
branches:
- development
- staging
- main
jobs:
coverage:
name: Unit Test Coverage Check
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
checks: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 8
uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'temurin'
cache: 'maven'
- name: Run unit tests (excluding integration tests)
run: |
echo "Running unit tests only (excluding *IT.java files)..."
mvn clean test -Dtest='!*IT' -Dgpg.skip=true
continue-on-error: false
- name: Generate JaCoCo coverage report
run: |
echo "Generating JaCoCo report..."
mvn jacoco:report -Dgpg.skip=true
echo "✅ Coverage report generated"
if: always()
- name: Verify JaCoCo reports generated
run: |
echo "Checking for JaCoCo reports..."
echo "Current directory: $(pwd)"
echo "Target directory contents:"
ls -la target/ || echo "No target directory found"
if [ -d "target/site/jacoco" ]; then
echo "JaCoCo directory contents:"
ls -lh target/site/jacoco/
fi
if [ ! -f "target/site/jacoco/jacoco.xml" ]; then
echo "❌ Error: jacoco.xml not found"
echo "This usually means tests didn't run or JaCoCo plugin isn't configured correctly"
exit 1
fi
if [ ! -f "target/site/jacoco/jacoco.csv" ]; then
echo "⚠️ Warning: jacoco.csv not found (badge generation will be skipped)"
fi
echo "✅ JaCoCo XML report found"
if: always()
- name: Generate JaCoCo Badge
id: jacoco
uses: cicirello/jacoco-badge-generator@v2
if: hashFiles('target/site/jacoco/jacoco.csv') != ''
continue-on-error: true
with:
jacoco-csv-file: target/site/jacoco/jacoco.csv
badges-directory: .github/badges
generate-branches-badge: true
generate-summary: true
- name: Check coverage thresholds
id: coverage-check
if: hashFiles('target/site/jacoco/jacoco.xml') != ''
run: |
echo "Checking coverage thresholds (unit tests only)..."
# Extract coverage percentages from JaCoCo XML report (using sed for cross-platform compatibility)
INSTRUCTION_COVERAGE=$(sed -n 's/.*type="INSTRUCTION".*covered="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1)
INSTRUCTION_MISSED=$(sed -n 's/.*type="INSTRUCTION".*missed="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1)
BRANCH_COVERAGE=$(sed -n 's/.*type="BRANCH".*covered="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1)
BRANCH_MISSED=$(sed -n 's/.*type="BRANCH".*missed="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1)
# Calculate percentages
INSTRUCTION_TOTAL=$((INSTRUCTION_COVERAGE + INSTRUCTION_MISSED))
BRANCH_TOTAL=$((BRANCH_COVERAGE + BRANCH_MISSED))
if [ $INSTRUCTION_TOTAL -gt 0 ]; then
INSTRUCTION_PCT=$((INSTRUCTION_COVERAGE * 100 / INSTRUCTION_TOTAL))
else
INSTRUCTION_PCT=0
fi
if [ $BRANCH_TOTAL -gt 0 ]; then
BRANCH_PCT=$((BRANCH_COVERAGE * 100 / BRANCH_TOTAL))
else
BRANCH_PCT=0
fi
echo "instruction_pct=$INSTRUCTION_PCT" >> $GITHUB_OUTPUT
echo "branch_pct=$BRANCH_PCT" >> $GITHUB_OUTPUT
# Check thresholds
THRESHOLD_MET=true
MESSAGES=""
if [ $INSTRUCTION_PCT -lt 90 ]; then
THRESHOLD_MET=false
MESSAGES="${MESSAGES}❌ Overall instruction coverage is ${INSTRUCTION_PCT}% (threshold: 90%)\n"
else
MESSAGES="${MESSAGES}✅ Overall instruction coverage is ${INSTRUCTION_PCT}% (threshold: 90%)\n"
fi
if [ $BRANCH_PCT -lt 80 ]; then
THRESHOLD_MET=false
MESSAGES="${MESSAGES}❌ Branch coverage is ${BRANCH_PCT}% (threshold: 80%)\n"
else
MESSAGES="${MESSAGES}✅ Branch coverage is ${BRANCH_PCT}% (threshold: 80%)\n"
fi
echo "threshold_met=$THRESHOLD_MET" >> $GITHUB_OUTPUT
echo -e "$MESSAGES"
echo "messages<<EOF" >> $GITHUB_OUTPUT
echo -e "$MESSAGES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
if [ "$THRESHOLD_MET" = "false" ]; then
exit 1
fi
- name: Add coverage comment to PR
uses: madrapps/jacoco-report@v1.6.1
if: always()
with:
paths: |
${{ github.workspace }}/target/site/jacoco/jacoco.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 90
min-coverage-changed-files: 80
title: '📊 Unit Test Coverage Report'
update-comment: true
- name: Upload JaCoCo coverage report
uses: actions/upload-artifact@v4
if: always()
with:
name: jacoco-report
path: target/site/jacoco/
- name: Fail if coverage thresholds not met
if: steps.coverage-check.outputs.threshold_met == 'false'
run: |
echo "Coverage thresholds not met:"
echo "${{ steps.coverage-check.outputs.messages }}"
exit 1