-
Notifications
You must be signed in to change notification settings - Fork 1
131 lines (113 loc) · 4.73 KB
/
test-coverage.yml
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
# .github/workflows/test-coverage.yml
name: Update Test Coverage
on:
push:
branches:
- master
pull_request:
jobs:
test:
runs-on: macOS-15
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Use latest available Xcode version
run: sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
- name: Resolve Swift Package Dependencies
run: xcodebuild -resolvePackageDependencies
- name: Build Tests
run: |
xcodebuild build \
-project Weather.xcodeproj \
-scheme Weather \
-destination 'platform=iOS Simulator,name=iPhone 16' \
| tee xcodebuild.log
- name: Run tests with coverage
run: |
xcodebuild test \
-project Weather.xcodeproj \
-scheme Weather \
-destination 'platform=iOS Simulator,name=iPhone 16' \
-enableCodeCoverage YES \
-resultBundlePath TestResults.xcresult | tee xcodebuild.log
continue-on-error: true
- name: Debug test failure (if any)
if: failure()
run: |
tail -n 100 xcodebuild.log
- name: Verify coverage data
run: |
if ! [ -d "TestResults.xcresult" ]; then
echo "❌ Test results not found. Check test logs."
exit 1
fi
if ! xcrun xccov view --report TestResults.xcresult > /dev/null 2>&1; then
echo "❌ No coverage data found. Ensure tests are generating coverage."
exit 1
fi
- name: Extract coverage data
run: |
xcrun xccov view --report --json TestResults.xcresult > coverage.json
- name: Update README with Test Coverage
run: |
# Function to generate dynamic badge
generate_badge() {
local coverage=$1
local color
local text
if (( $(echo "$coverage < 25" | bc -l) )); then
color="red"
text="Needs%20Attention"
elif (( $(echo "$coverage >= 25 && $coverage < 80" | bc -l) )); then
color="yellow"
text="Good%20Job"
else
color="green"
text="Excellent"
fi
echo "https://img.shields.io/badge/Coverage-${coverage}%25-${color}?style=flat&logo=swift&logoColor=white&label=${text}"
}
# Generate coverage section content
echo "## Test Coverage" > coverage_section.md
echo "" >> coverage_section.md
echo "### 📊 General Coverage" >> coverage_section.md
TOTAL_COVERAGE=$(jq -r '.coveredLines / .executableLines * 100 | round / 1' coverage.json)
COVERAGE_BADGE_URL=$(generate_badge $TOTAL_COVERAGE)
echo "" >> coverage_section.md
echo "" >> coverage_section.md
echo "---" >> coverage_section.md
echo "### 📄 File-wise Coverage Breakdown" >> coverage_section.md
echo "| File | Covered Lines | Executable Lines | Coverage |" >> coverage_section.md
echo "|------|--------------|-----------------|----------|" >> coverage_section.md
# Generate file-wise coverage rows with dynamic badges
jq -r '.targets[].files[] | "\(.name) \(.coveredLines) \(.executableLines) \(.lineCoverage * 100 | round)"' coverage.json | while read -r name coveredLines executableLines coverage; do
BADGE_URL=$(generate_badge $coverage)
echo "| $name | $coveredLines | $executableLines |  |" >> coverage_section.md
done
echo "---" >> coverage_section.md
echo "" >> coverage_section.md
# Remove ONLY the "Test Coverage" section and its subsections from README
awk '
/^## Test Coverage$/ { skip = 1 }
/^## / && !/^## Test Coverage$/ { skip = 0 }
!skip { print }
' README.md > README.tmp
# Add the new "Test Coverage" section content from coverage_section.md
cat coverage_section.md >> README.tmp
# Move the updated file back to README.md
mv README.tmp README.md
# Clean up temporary file
rm coverage_section.md
- name: Commit and push coverage report
env:
GH_PAT: ${{ secrets.GH_PAT }}
run: |
git config --global user.name "mrugama"
git config --global user.email "mrugama@gmail.com"
git remote set-url origin https://mrugama:${GH_PAT}@github.com/mrugama/Weather.git
git add coverage.json
git add README.md
git commit -m "Update test coverage report" || exit 0
git push origin HEAD:master