Skip to content

Update test-coverage.yml #62

Update test-coverage.yml

Update test-coverage.yml #62

Workflow file for this run

# .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 ExistingSchemeName \
-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: Move coverage.json to repo root
run: |
mv coverage/coverage.json 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 "[![Test Coverage]($COVERAGE_BADGE_URL)](https://github.com/mrugama/Weather/actions)" >> coverage_section.md
echo "" >> coverage_section.md
echo "---" >> coverage_section.md
# Group files by target and generate coverage breakdown
echo "### 📄 File-wise Coverage Breakdown by Target" >> coverage_section.md
jq -r '.targets[] | "### \(.name)\n| File | Covered Lines | Executable Lines | Coverage |\n|------|--------------|-----------------|----------|\n" + (.files[] | "| \(.name) | \(.coveredLines) | \(.executableLines) | [![Coverage]($(generate_badge \((.lineCoverage * 100) | round))](https://github.com/mrugama/Weather/actions) |") + "\n"' coverage.json >> coverage_section.md
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