Update README.md #49
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# .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: | | |
# Extract total coverage and format the percentage to 2 decimals | |
TOTAL_COVERAGE=$(jq -r '.coveredLines / .executableLines * 100 | round / 1' coverage.json) | |
# Create badge for total coverage | |
COVERAGE_BADGE_URL="https://img.shields.io/badge/Test%20Coverage-${TOTAL_COVERAGE}%25-green?style=flat&logo=swift&logoColor=white" | |
# Generate coverage section content | |
echo "## Test Coverage" > coverage_section.md | |
echo "[](https://github.com/mrugama/Weather/actions)" >> coverage_section.md | |
echo "" >> coverage_section.md | |
echo "### 📊 General Coverage" >> coverage_section.md | |
echo "> **Total Test Coverage:** \`$TOTAL_COVERAGE%\`" >> 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 | |
# Loop through each file in coverage.json and add to table | |
jq -r '.targets[].files[] | "| \(.name) | \(.coveredLines) | \(.executableLines) | \((.lineCoverage * 100) | round)%"' coverage.json >> coverage_section.md | |
echo "---" >> coverage_section.md | |
echo "" >> coverage_section.md | |
# Remove the existing "Test Coverage" section from README | |
sed '/## Test Coverage/ , /---/d' 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 |