|
| 1 | +name: Code Coverage |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | + push: |
| 8 | + branches: [main] |
| 9 | + |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + pull-requests: write |
| 15 | + pages: write |
| 16 | + id-token: write |
| 17 | + |
| 18 | +defaults: |
| 19 | + run: |
| 20 | + shell: bash |
| 21 | + |
| 22 | +jobs: |
| 23 | + coverage: |
| 24 | + name: Generate code coverage with cargo-llvm-cov |
| 25 | + runs-on: ubuntu-latest |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout Repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + # Fetch enough history for diff against base branch |
| 32 | + fetch-depth: 0 |
| 33 | + |
| 34 | + - name: Cache cargo builds |
| 35 | + uses: Swatinem/rust-cache@v2 |
| 36 | + |
| 37 | + - name: Install Rust toolchain with llvm-tools-preview |
| 38 | + uses: dtolnay/rust-toolchain@stable |
| 39 | + with: |
| 40 | + components: llvm-tools-preview |
| 41 | + |
| 42 | + - name: Install cargo-llvm-cov |
| 43 | + uses: taiki-e/install-action@cargo-llvm-cov |
| 44 | + |
| 45 | + - name: Install Linux deps for winit/wgpu |
| 46 | + run: | |
| 47 | + sudo apt-get update |
| 48 | + sudo apt-get install -y \ |
| 49 | + pkg-config libx11-dev libxcb1-dev libxcb-render0-dev \ |
| 50 | + libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev \ |
| 51 | + libwayland-dev libudev-dev \ |
| 52 | + libvulkan-dev libvulkan1 mesa-vulkan-drivers vulkan-tools |
| 53 | +
|
| 54 | + - name: Install Linux deps for audio |
| 55 | + run: | |
| 56 | + sudo apt-get update |
| 57 | + sudo apt-get install -y libasound2-dev |
| 58 | +
|
| 59 | + - name: Configure Vulkan (Ubuntu) |
| 60 | + run: | |
| 61 | + echo "WGPU_BACKEND=vulkan" >> "$GITHUB_ENV" |
| 62 | + # Prefer Mesa's software Vulkan (lavapipe) for headless availability |
| 63 | + echo "VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/lvp_icd.x86_64.json" >> "$GITHUB_ENV" |
| 64 | + vulkaninfo --summary || true |
| 65 | +
|
| 66 | + - name: Generate full coverage JSON |
| 67 | + run: | |
| 68 | + cargo llvm-cov --workspace \ |
| 69 | + --features lambda-rs/with-vulkan,lambda-rs/audio-output-device \ |
| 70 | + --json \ |
| 71 | + --output-path coverage.json |
| 72 | +
|
| 73 | + - name: Generate HTML coverage report |
| 74 | + run: | |
| 75 | + cargo llvm-cov --workspace \ |
| 76 | + --features lambda-rs/with-vulkan,lambda-rs/audio-output-device \ |
| 77 | + --html \ |
| 78 | + --output-dir coverage-html \ |
| 79 | + --no-run |
| 80 | +
|
| 81 | + - name: Get changed files in PR |
| 82 | + if: github.event_name == 'pull_request' |
| 83 | + id: changed |
| 84 | + run: | |
| 85 | + # Use GitHub's provided base/head SHAs for accurate diff |
| 86 | + base_sha="${{ github.event.pull_request.base.sha }}" |
| 87 | + head_sha="${{ github.event.pull_request.head.sha }}" |
| 88 | + changed_files=$(git diff --name-only "$base_sha" "$head_sha" -- '*.rs' | tr '\n' ' ') |
| 89 | + echo "files=$changed_files" >> "$GITHUB_OUTPUT" |
| 90 | +
|
| 91 | + - name: Generate coverage report data |
| 92 | + id: cov |
| 93 | + run: | |
| 94 | + # Extract total coverage and round to 2 decimal places |
| 95 | + pct_raw=$(jq -r '(.data[0].totals.lines.percent // 0)' coverage.json) |
| 96 | + pct=$(printf "%.2f" "$pct_raw") |
| 97 | + covered=$(jq -r '(.data[0].totals.lines.covered // 0)' coverage.json) |
| 98 | + total=$(jq -r '(.data[0].totals.lines.count // 0)' coverage.json) |
| 99 | + echo "pct=$pct" >> "$GITHUB_OUTPUT" |
| 100 | + echo "covered=$covered" >> "$GITHUB_OUTPUT" |
| 101 | + echo "total=$total" >> "$GITHUB_OUTPUT" |
| 102 | +
|
| 103 | + # Extract per-file coverage as JSON for changed files |
| 104 | + jq -r '.data[0].files[] | "\(.filename)|\(.summary.lines.percent // 0)|\(.summary.lines.covered // 0)|\(.summary.lines.count // 0)"' coverage.json > file_coverage.txt |
| 105 | +
|
| 106 | + - name: Build PR coverage comment |
| 107 | + if: github.event_name == 'pull_request' |
| 108 | + id: comment |
| 109 | + env: |
| 110 | + CHANGED_FILES: ${{ steps.changed.outputs.files }} |
| 111 | + RUN_ID: ${{ github.run_id }} |
| 112 | + REPO: ${{ github.repository }} |
| 113 | + COMMIT_SHA: ${{ github.event.pull_request.head.sha }} |
| 114 | + run: | |
| 115 | + # Base URL for GitHub Pages coverage (from main branch) |
| 116 | + PAGES_BASE="https://lambda-sh.github.io/lambda/coverage" |
| 117 | + # Get current timestamp in UTC |
| 118 | + TIMESTAMP=$(date -u +"%Y-%m-%d %H:%M:%S UTC") |
| 119 | + # Short commit SHA for display |
| 120 | + SHORT_SHA="${COMMIT_SHA:0:7}" |
| 121 | +
|
| 122 | + # Build the comment body |
| 123 | + { |
| 124 | + echo "### ✅ Coverage Report" |
| 125 | + echo "" |
| 126 | + echo "📊 [View Full HTML Report](https://github.com/${REPO}/actions/runs/${RUN_ID}) (download artifact)" |
| 127 | + echo "" |
| 128 | + echo "#### Overall Coverage" |
| 129 | + echo "" |
| 130 | + echo "| Metric | Value |" |
| 131 | + echo "|--------|-------|" |
| 132 | + echo "| **Total Line Coverage** | ${{ steps.cov.outputs.pct }}% |" |
| 133 | + echo "| **Lines Covered** | ${{ steps.cov.outputs.covered }} / ${{ steps.cov.outputs.total }} |" |
| 134 | + echo "" |
| 135 | +
|
| 136 | + # Calculate coverage for changed files |
| 137 | + if [ -n "$CHANGED_FILES" ]; then |
| 138 | + echo "#### Changed Files in This PR" |
| 139 | + echo "" |
| 140 | + echo "| File | Coverage | Lines |" |
| 141 | + echo "|------|----------|-------|" |
| 142 | +
|
| 143 | + pr_covered=0 |
| 144 | + pr_total=0 |
| 145 | +
|
| 146 | + for file in $CHANGED_FILES; do |
| 147 | + # Find this file in coverage data (match by filename ending) |
| 148 | + match=$(grep -E "/${file}\|" file_coverage.txt || grep -E "^${file}\|" file_coverage.txt || true) |
| 149 | + if [ -n "$match" ]; then |
| 150 | + file_pct=$(echo "$match" | cut -d'|' -f2) |
| 151 | + file_covered=$(echo "$match" | cut -d'|' -f3) |
| 152 | + file_total=$(echo "$match" | cut -d'|' -f4) |
| 153 | + # Format percentage to 2 decimal places |
| 154 | + file_pct_fmt=$(printf "%.2f" "$file_pct") |
| 155 | + # Create HTML filename (replace / with path structure, add .html) |
| 156 | + html_file=$(echo "$file" | sed 's|/|/|g').html |
| 157 | + echo "| [\`${file}\`](${PAGES_BASE}/${html_file}) | ${file_pct_fmt}% | ${file_covered}/${file_total} |" |
| 158 | + pr_covered=$((pr_covered + file_covered)) |
| 159 | + pr_total=$((pr_total + file_total)) |
| 160 | + else |
| 161 | + echo "| \`${file}\` | N/A | (no coverage data) |" |
| 162 | + fi |
| 163 | + done |
| 164 | +
|
| 165 | + echo "" |
| 166 | + if [ "$pr_total" -gt 0 ]; then |
| 167 | + pr_pct=$(echo "scale=2; $pr_covered * 100 / $pr_total" | bc) |
| 168 | + echo "**PR Files Coverage:** ${pr_pct}% (${pr_covered}/${pr_total} lines)" |
| 169 | + fi |
| 170 | + else |
| 171 | + echo "*No Rust files changed in this PR.*" |
| 172 | + fi |
| 173 | +
|
| 174 | + echo "" |
| 175 | + echo "---" |
| 176 | + echo "*Generated by [cargo-llvm-cov](https://github.com/taiki-e/cargo-llvm-cov) · [Latest main coverage](${PAGES_BASE})*" |
| 177 | + echo "" |
| 178 | + echo "<sub>Last updated: ${TIMESTAMP} · Commit: [\`${SHORT_SHA}\`](https://github.com/${REPO}/commit/${COMMIT_SHA})</sub>" |
| 179 | + } > comment_body.md |
| 180 | +
|
| 181 | + # Store as output (handle multiline) |
| 182 | + { |
| 183 | + echo "body<<EOF" |
| 184 | + cat comment_body.md |
| 185 | + echo "EOF" |
| 186 | + } >> "$GITHUB_OUTPUT" |
| 187 | +
|
| 188 | + - name: Find existing coverage comment |
| 189 | + if: github.event_name == 'pull_request' |
| 190 | + id: find_comment |
| 191 | + uses: actions/github-script@v7 |
| 192 | + with: |
| 193 | + script: | |
| 194 | + const { owner, repo } = context.repo; |
| 195 | + const issue_number = context.issue.number; |
| 196 | +
|
| 197 | + const comments = await github.rest.issues.listComments({ |
| 198 | + owner, |
| 199 | + repo, |
| 200 | + issue_number, |
| 201 | + }); |
| 202 | +
|
| 203 | + const botComment = comments.data.find(comment => |
| 204 | + comment.user.type === 'Bot' && |
| 205 | + comment.body.includes('### ✅ Coverage Report') |
| 206 | + ); |
| 207 | +
|
| 208 | + return botComment ? botComment.id : null; |
| 209 | + result-encoding: string |
| 210 | + |
| 211 | + - name: Create or update PR comment |
| 212 | + if: github.event_name == 'pull_request' |
| 213 | + uses: actions/github-script@v7 |
| 214 | + with: |
| 215 | + script: | |
| 216 | + const fs = require('fs'); |
| 217 | + const body = fs.readFileSync('comment_body.md', 'utf8'); |
| 218 | + const { owner, repo } = context.repo; |
| 219 | + const issue_number = context.issue.number; |
| 220 | + const existingCommentId = ${{ steps.find_comment.outputs.result }}; |
| 221 | +
|
| 222 | + if (existingCommentId) { |
| 223 | + await github.rest.issues.updateComment({ |
| 224 | + owner, |
| 225 | + repo, |
| 226 | + comment_id: existingCommentId, |
| 227 | + body, |
| 228 | + }); |
| 229 | + console.log(`Updated existing comment ${existingCommentId}`); |
| 230 | + } else { |
| 231 | + await github.rest.issues.createComment({ |
| 232 | + owner, |
| 233 | + repo, |
| 234 | + issue_number, |
| 235 | + body, |
| 236 | + }); |
| 237 | + console.log('Created new coverage comment'); |
| 238 | + } |
| 239 | +
|
| 240 | + - name: Upload coverage HTML as artifact |
| 241 | + uses: actions/upload-artifact@v4 |
| 242 | + with: |
| 243 | + name: coverage-html-report |
| 244 | + path: coverage-html/ |
| 245 | + retention-days: 30 |
| 246 | + |
| 247 | + - name: Upload coverage JSON as artifact |
| 248 | + uses: actions/upload-artifact@v4 |
| 249 | + with: |
| 250 | + name: coverage-json |
| 251 | + path: coverage.json |
| 252 | + retention-days: 30 |
| 253 | + |
| 254 | + # Deploy HTML report to GitHub Pages on pushes to main |
| 255 | + deploy-coverage: |
| 256 | + name: Deploy coverage to GitHub Pages |
| 257 | + needs: coverage |
| 258 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 259 | + runs-on: ubuntu-latest |
| 260 | + |
| 261 | + environment: |
| 262 | + name: github-pages |
| 263 | + url: ${{ steps.deployment.outputs.page_url }} |
| 264 | + |
| 265 | + steps: |
| 266 | + - name: Download coverage HTML artifact |
| 267 | + uses: actions/download-artifact@v4 |
| 268 | + with: |
| 269 | + name: coverage-html-report |
| 270 | + path: coverage-html |
| 271 | + |
| 272 | + - name: Setup Pages |
| 273 | + uses: actions/configure-pages@v4 |
| 274 | + |
| 275 | + - name: Upload to GitHub Pages |
| 276 | + uses: actions/upload-pages-artifact@v3 |
| 277 | + with: |
| 278 | + path: coverage-html |
| 279 | + |
| 280 | + - name: Deploy to GitHub Pages |
| 281 | + id: deployment |
| 282 | + uses: actions/deploy-pages@v4 |
0 commit comments