Add unit tests and coverage verification workflow #1
This file contains hidden or 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
| name: Java CI Coverage | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| pull_request: | |
| permissions: | |
| contents: read | |
| pages: write | |
| id-token: write | |
| jobs: | |
| coverage: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: 📥 Checkout code | |
| uses: actions/checkout@v4 | |
| - name: ☕ Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| cache: gradle | |
| - name: ⚡ Grant execute permission for Gradlew | |
| run: chmod +x gradlew | |
| - name: 🧪 Build and run tests with coverage | |
| run: ./gradlew clean build jacocoTestReport --no-daemon --console=plain | |
| - name: 📊 Check JaCoCo coverage | |
| run: | | |
| REPORT_FILE="build/reports/jacoco/test/jacocoTestReport.xml" | |
| if [ ! -f "$REPORT_FILE" ]; then | |
| echo "::error::Coverage report not found: $REPORT_FILE" | |
| exit 1 | |
| fi | |
| # Parse root-level coverage from JaCoCo XML | |
| COVERAGE=$(python3 << EOF | |
| import xml.etree.ElementTree as ET | |
| import sys | |
| tree = ET.parse("${REPORT_FILE}") | |
| root = tree.getroot() | |
| for counter in root.findall('counter'): | |
| if counter.get('type') == 'INSTRUCTION': | |
| missed = int(counter.get('missed', 0)) | |
| covered = int(counter.get('covered', 0)) | |
| total = missed + covered | |
| if total > 0: | |
| print(f"{(covered * 100.0 / total):.2f}") | |
| sys.exit(0) | |
| sys.exit(1) | |
| EOF | |
| ) || { echo "::error::Failed to parse coverage from XML"; exit 1; } | |
| THRESHOLD=50.0 | |
| if awk "BEGIN {exit !($COVERAGE < $THRESHOLD)}"; then | |
| echo "::error::Coverage ${COVERAGE}% is below ${THRESHOLD}% threshold" | |
| exit 1 | |
| fi | |
| echo "::notice::Code Coverage: ${COVERAGE}% (threshold: ${THRESHOLD}%)" | |
| - name: 📤 Upload JaCoCo Coverage Report | |
| uses: actions/upload-artifact@v4 | |
| if: always() | |
| with: | |
| name: jacoco-coverage-report | |
| path: build/reports/jacoco/test/html/** | |
| retention-days: 30 | |
| - name: 📄 Setup Pages | |
| if: github.ref == 'refs/heads/main' | |
| uses: actions/configure-pages@v4 | |
| - name: 📤 Upload coverage to Pages | |
| if: github.ref == 'refs/heads/main' | |
| uses: actions/upload-pages-artifact@v3 | |
| with: | |
| path: build/reports/jacoco/test/html | |
| - name: 🚀 Deploy to GitHub Pages | |
| if: github.ref == 'refs/heads/main' | |
| id: deployment | |
| uses: actions/deploy-pages@v4 |