-
Notifications
You must be signed in to change notification settings - Fork 1
test(mutation): Introduce mutation-based test coverage #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
talagrand
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
talagrand:mutants
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| name: Mutation Testing | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths: | ||
| - ".cargo/*.toml" | ||
| - ".github/workflows/mutants.yml" | ||
| - "Cargo.*" | ||
| - "src/**" | ||
| - "tests/**" | ||
|
|
||
| env: | ||
| CARGO_TERM_COLOR: always | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| mutants: | ||
| name: Mutation Testing | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v6 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Install Rust | ||
| uses: dtolnay/rust-toolchain@stable | ||
|
|
||
| - name: Cache dependencies | ||
| uses: Swatinem/rust-cache@v2 | ||
|
|
||
| - name: Install cargo-mutants | ||
| uses: taiki-e/install-action@v2 | ||
| with: | ||
| tool: cargo-mutants | ||
|
|
||
| - name: Relative diff | ||
| run: | | ||
| git branch -av | ||
| git diff origin/${{ github.base_ref }}.. | tee git.diff | ||
|
|
||
| - name: Run cargo-mutants | ||
| run: cargo mutants --no-shuffle --all-features --in-diff git.diff --in-place | ||
| continue-on-error: true | ||
|
|
||
| - name: Check mutants report | ||
| id: report | ||
| run: | | ||
| if [ -f mutants.out/outcomes.json ]; then | ||
| echo "exists=true" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "exists=false" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Comment on PR | ||
| if: steps.report.outputs.exists == 'true' && (github.event.pull_request.head.repo.full_name == github.repository) | ||
| uses: actions/github-script@v8 | ||
| with: | ||
| script: | | ||
| const fs = require('fs'); | ||
| const report = JSON.parse(fs.readFileSync('mutants.out/outcomes.json', 'utf8')); | ||
|
|
||
| const counts = { | ||
| caught: Number(report.caught ?? 0), | ||
| missed: Number(report.missed ?? 0), | ||
| timeout: Number(report.timeout ?? 0), | ||
| unviable: Number(report.unviable ?? 0), | ||
| }; | ||
|
|
||
| const missed = (report.outcomes ?? []) | ||
| .filter(o => o.summary === 'MissedMutant' && o.scenario?.Mutant) | ||
| .map(o => { | ||
| const mutant = o.scenario.Mutant; | ||
| const functionName = mutant.function?.function_name ?? mutant.name; | ||
| const replacement = mutant.replacement ?? 'unknown replacement'; | ||
| return `${functionName}: ${replacement}`; | ||
| }); | ||
|
|
||
| const totalMutants = Number(report.total_mutants ?? 0); | ||
| const testable = counts.caught + counts.missed; | ||
| const score = testable > 0 ? (counts.caught / testable * 100).toFixed(1) : 'N/A'; | ||
|
|
||
| let body = '## 🧬 Mutation Testing Results\n\n'; | ||
| body += 'Scope: PR diff (`--in-diff`)\n\n'; | ||
| body += '| Metric | Count |\n|--------|-------|\n'; | ||
| body += `| Caught | ${counts.caught} |\n`; | ||
| body += `| Missed | ${counts.missed} |\n`; | ||
| body += `| Timeout | ${counts.timeout} |\n`; | ||
| body += `| Unviable | ${counts.unviable} |\n`; | ||
| body += `| **Mutation Score** | **${score}%** |\n\n`; | ||
|
|
||
| if (totalMutants === 0) { | ||
| body += 'No mutants were generated for the PR diff. This is expected for test-only or non-Rust changes.\n'; | ||
| } else if (missed.length > 0) { | ||
| body += '<details><summary>Missed mutants (test gaps)</summary>\n\n```\n'; | ||
| body += missed.join('\n'); | ||
| body += '\n```\n\n</details>\n'; | ||
| } else { | ||
| body += 'All mutants in the PR diff were caught by tests.\n'; | ||
| } | ||
|
|
||
| const marker = '## 🧬 Mutation Testing Results'; | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| }); | ||
| const existing = comments.find(c => c.body?.includes(marker)); | ||
|
|
||
| if (existing) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: existing.id, | ||
| body, | ||
| }); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| - name: No report notice | ||
| if: steps.report.outputs.exists == 'false' | ||
| run: echo "cargo-mutants did not produce mutants.out/outcomes.json; skipping PR comment and artifact upload." | ||
|
|
||
| - name: Upload mutants report | ||
| if: steps.report.outputs.exists == 'true' | ||
| uses: actions/upload-artifact@v7 | ||
| with: | ||
| name: mutants-report | ||
| path: mutants.out/ | ||
| retention-days: 14 | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| # Generated by Cargo | ||
| target/ | ||
|
|
||
| # Mutation testing output | ||
| mutants.out/ | ||
|
|
||
| .vscode/ |
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The workflow uses the Issues API (listComments/createComment/updateComment), but the job-level
permissionsonly grantscontents: readandpull-requests: write. With these permissions,GITHUB_TOKENwon’t haveissues: write, so the PR comment step is likely to fail with a permissions error. Addissues: write(and optionally droppull-requests: writeif it’s not needed).