|
| 1 | +name: Mutation Testing |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - ".cargo/*.toml" |
| 7 | + - ".github/workflows/mutants.yml" |
| 8 | + - "Cargo.*" |
| 9 | + - "src/**" |
| 10 | + - "tests/**" |
| 11 | + |
| 12 | +env: |
| 13 | + CARGO_TERM_COLOR: always |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + mutants: |
| 21 | + name: Mutation Testing |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@v6 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Install Rust |
| 30 | + uses: dtolnay/rust-toolchain@stable |
| 31 | + |
| 32 | + - name: Cache dependencies |
| 33 | + uses: Swatinem/rust-cache@v2 |
| 34 | + |
| 35 | + - name: Install cargo-mutants |
| 36 | + uses: taiki-e/install-action@v2 |
| 37 | + with: |
| 38 | + tool: cargo-mutants |
| 39 | + |
| 40 | + - name: Relative diff |
| 41 | + run: | |
| 42 | + git branch -av |
| 43 | + git diff origin/${{ github.base_ref }}.. | tee git.diff |
| 44 | +
|
| 45 | + - name: Run cargo-mutants |
| 46 | + run: cargo mutants --no-shuffle --all-features --in-diff git.diff --in-place |
| 47 | + continue-on-error: true |
| 48 | + |
| 49 | + - name: Check mutants report |
| 50 | + id: report |
| 51 | + run: | |
| 52 | + if [ -f mutants.out/outcomes.json ]; then |
| 53 | + echo "exists=true" >> "$GITHUB_OUTPUT" |
| 54 | + else |
| 55 | + echo "exists=false" >> "$GITHUB_OUTPUT" |
| 56 | + fi |
| 57 | +
|
| 58 | + - name: Comment on PR |
| 59 | + if: steps.report.outputs.exists == 'true' && (github.event.pull_request.head.repo.full_name == github.repository) |
| 60 | + uses: actions/github-script@v8 |
| 61 | + with: |
| 62 | + script: | |
| 63 | + const fs = require('fs'); |
| 64 | + const report = JSON.parse(fs.readFileSync('mutants.out/outcomes.json', 'utf8')); |
| 65 | +
|
| 66 | + const counts = { |
| 67 | + caught: Number(report.caught ?? 0), |
| 68 | + missed: Number(report.missed ?? 0), |
| 69 | + timeout: Number(report.timeout ?? 0), |
| 70 | + unviable: Number(report.unviable ?? 0), |
| 71 | + }; |
| 72 | +
|
| 73 | + const missed = (report.outcomes ?? []) |
| 74 | + .filter(o => o.summary === 'MissedMutant' && o.scenario?.Mutant) |
| 75 | + .map(o => { |
| 76 | + const mutant = o.scenario.Mutant; |
| 77 | + const functionName = mutant.function?.function_name ?? mutant.name; |
| 78 | + const replacement = mutant.replacement ?? 'unknown replacement'; |
| 79 | + return `${functionName}: ${replacement}`; |
| 80 | + }); |
| 81 | +
|
| 82 | + const totalMutants = Number(report.total_mutants ?? 0); |
| 83 | + const testable = counts.caught + counts.missed; |
| 84 | + const score = testable > 0 ? (counts.caught / testable * 100).toFixed(1) : 'N/A'; |
| 85 | +
|
| 86 | + let body = '## 🧬 Mutation Testing Results\n\n'; |
| 87 | + body += 'Scope: PR diff (`--in-diff`)\n\n'; |
| 88 | + body += '| Metric | Count |\n|--------|-------|\n'; |
| 89 | + body += `| Caught | ${counts.caught} |\n`; |
| 90 | + body += `| Missed | ${counts.missed} |\n`; |
| 91 | + body += `| Timeout | ${counts.timeout} |\n`; |
| 92 | + body += `| Unviable | ${counts.unviable} |\n`; |
| 93 | + body += `| **Mutation Score** | **${score}%** |\n\n`; |
| 94 | +
|
| 95 | + if (totalMutants === 0) { |
| 96 | + body += 'No mutants were generated for the PR diff. This is expected for test-only or non-Rust changes.\n'; |
| 97 | + } else if (missed.length > 0) { |
| 98 | + body += '<details><summary>Missed mutants (test gaps)</summary>\n\n```\n'; |
| 99 | + body += missed.join('\n'); |
| 100 | + body += '\n```\n\n</details>\n'; |
| 101 | + } else { |
| 102 | + body += 'All mutants in the PR diff were caught by tests.\n'; |
| 103 | + } |
| 104 | +
|
| 105 | + const marker = '## 🧬 Mutation Testing Results'; |
| 106 | + const { data: comments } = await github.rest.issues.listComments({ |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + issue_number: context.issue.number, |
| 110 | + }); |
| 111 | + const existing = comments.find(c => c.body?.includes(marker)); |
| 112 | +
|
| 113 | + if (existing) { |
| 114 | + await github.rest.issues.updateComment({ |
| 115 | + owner: context.repo.owner, |
| 116 | + repo: context.repo.repo, |
| 117 | + comment_id: existing.id, |
| 118 | + body, |
| 119 | + }); |
| 120 | + } else { |
| 121 | + await github.rest.issues.createComment({ |
| 122 | + owner: context.repo.owner, |
| 123 | + repo: context.repo.repo, |
| 124 | + issue_number: context.issue.number, |
| 125 | + body, |
| 126 | + }); |
| 127 | + } |
| 128 | +
|
| 129 | + - name: No report notice |
| 130 | + if: steps.report.outputs.exists == 'false' |
| 131 | + run: echo "cargo-mutants did not produce mutants.out/outcomes.json; skipping PR comment and artifact upload." |
| 132 | + |
| 133 | + - name: Upload mutants report |
| 134 | + if: steps.report.outputs.exists == 'true' |
| 135 | + uses: actions/upload-artifact@v7 |
| 136 | + with: |
| 137 | + name: mutants-report |
| 138 | + path: mutants.out/ |
| 139 | + retention-days: 14 |
0 commit comments