Test Pytest Coverage Comment #22
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: Test Pytest Coverage Comment | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| issue-number: | |
| description: 'PR number to comment on (leave empty to auto-create)' | |
| required: false | |
| default: '' | |
| permissions: | |
| contents: write | |
| checks: write | |
| pull-requests: write | |
| jobs: | |
| setup: | |
| name: Setup PR for Testing | |
| runs-on: ubuntu-latest | |
| outputs: | |
| pr-number: ${{ steps.set-pr.outputs.pr-number }} | |
| steps: | |
| - uses: actions/checkout@v5 | |
| - name: Check if PR number provided and exists | |
| id: check-pr | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| if [ -n "${{ inputs.issue-number }}" ]; then | |
| echo "Checking if PR #${{ inputs.issue-number }} exists..." | |
| if gh pr view ${{ inputs.issue-number }} > /dev/null 2>&1; then | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| echo "✅ PR #${{ inputs.issue-number }} exists" | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| echo "❌ PR #${{ inputs.issue-number }} not found" | |
| fi | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| echo "No PR number provided" | |
| fi | |
| - name: Create test PR if needed | |
| if: steps.check-pr.outputs.exists == 'false' | |
| id: create-pr | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| BRANCH_NAME="test-coverage-$(date +%s)" | |
| echo "Creating test branch: $BRANCH_NAME" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git checkout -b $BRANCH_NAME | |
| echo "# Test PR for Coverage Workflow" > test-workflow-pr.md | |
| echo "Auto-created by workflow run at $(date)" >> test-workflow-pr.md | |
| git add test-workflow-pr.md | |
| git commit -m "Test PR for coverage workflow" | |
| git push -u origin $BRANCH_NAME | |
| PR_URL=$(gh pr create --title "Test PR for Coverage Workflow" --body "Auto-created for testing pytest-coverage-comment action. This PR can be closed after testing." --base main --head $BRANCH_NAME) | |
| PR_NUMBER=$(echo $PR_URL | grep -oP '(?<=pull/)\d+') | |
| echo "pr-number=$PR_NUMBER" >> $GITHUB_OUTPUT | |
| echo "✅ Created PR #$PR_NUMBER" | |
| # Add to job summary | |
| echo "## 🎯 Test PR Created" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**PR #$PR_NUMBER:** $PR_URL" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "All test jobs will post coverage comments to this PR." >> $GITHUB_STEP_SUMMARY | |
| - name: Set final PR number | |
| id: set-pr | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| if [ "${{ steps.check-pr.outputs.exists }}" == "true" ]; then | |
| echo "pr-number=${{ inputs.issue-number }}" >> $GITHUB_OUTPUT | |
| echo "Using provided PR #${{ inputs.issue-number }}" | |
| # Add to summary for existing PR | |
| PR_URL="https://github.com/${{ github.repository }}/pull/${{ inputs.issue-number }}" | |
| echo "## 🎯 Using Existing Test PR" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "**PR #${{ inputs.issue-number }}:** $PR_URL" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "All test jobs will post coverage comments to this PR." >> $GITHUB_STEP_SUMMARY | |
| else | |
| echo "pr-number=${{ steps.create-pr.outputs.pr-number }}" >> $GITHUB_OUTPUT | |
| echo "Using auto-created PR #${{ steps.create-pr.outputs.pr-number }}" | |
| fi | |
| test-pytest-coverage-comment: | |
| name: ${{ matrix.test-name }} | |
| needs: setup | |
| runs-on: ubuntu-latest | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| # Core coverage file type tests | |
| - test-name: 'XML Coverage' | |
| pytest-xml-coverage-path: './data/coverage_1.xml' | |
| pytest-coverage-path: '' | |
| junitxml-path: './data/pytest_1.xml' | |
| - test-name: 'TXT Coverage' | |
| pytest-xml-coverage-path: '' | |
| pytest-coverage-path: './data/pytest-coverage_4.txt' | |
| junitxml-path: './data/pytest_1.xml' | |
| - test-name: 'Both XML and TXT' | |
| pytest-xml-coverage-path: './data/coverage_1.xml' | |
| pytest-coverage-path: './data/pytest-coverage_1.txt' | |
| junitxml-path: './data/pytest_1.xml' | |
| - test-name: 'Large Test Suite (493K)' | |
| pytest-xml-coverage-path: './data/coverage_2.xml' | |
| pytest-coverage-path: '' | |
| junitxml-path: './data/pytest_3.xml' | |
| - test-name: 'TXT 100% Coverage' | |
| pytest-xml-coverage-path: '' | |
| pytest-coverage-path: './data/pytest-coverage_12.txt' | |
| junitxml-path: './data/pytest_1.xml' | |
| # Feature tests | |
| - test-name: 'Text Instead Badge' | |
| pytest-xml-coverage-path: './data/coverage_1.xml' | |
| pytest-coverage-path: '' | |
| junitxml-path: './data/pytest_1.xml' | |
| text-instead-badge: 'true' | |
| - test-name: 'Hide Report' | |
| pytest-xml-coverage-path: '' | |
| pytest-coverage-path: './data/pytest-coverage_2.txt' | |
| junitxml-path: './data/pytest_1.xml' | |
| hide-report: 'true' | |
| - test-name: 'Create New Comment' | |
| pytest-xml-coverage-path: './data/coverage_2.xml' | |
| pytest-coverage-path: './data/pytest-coverage_2.txt' | |
| junitxml-path: './data/pytest_2.xml' | |
| create-new-comment: 'true' | |
| - test-name: 'Hide Comment (Outputs Only)' | |
| pytest-xml-coverage-path: '' | |
| pytest-coverage-path: './data/pytest-coverage_4.txt' | |
| junitxml-path: './data/pytest_1.xml' | |
| hide-comment: 'true' | |
| - test-name: 'All Visual + Link Options' | |
| pytest-xml-coverage-path: '' | |
| pytest-coverage-path: './data/pytest-coverage_12.txt' | |
| junitxml-path: './data/pytest_1.xml' | |
| text-instead-badge: 'true' | |
| remove-link-from-badge: 'true' | |
| remove-links-to-files: 'true' | |
| remove-links-to-lines: 'true' | |
| # Multiple files tests | |
| - test-name: 'Multiple Files (TXT only)' | |
| multiple-files: | | |
| Module A, ./data/pytest-coverage_4.txt, ./data/pytest_1.xml | |
| Module B, ./data/pytest-coverage_2.txt, ./data/pytest_2.xml | |
| - test-name: 'Multiple Files (XML + TXT mixed)' | |
| multiple-files: | | |
| XML Module, ./data/coverage_1.xml, ./data/pytest_1.xml | |
| TXT Module, ./data/pytest-coverage_4.txt, ./data/pytest_1.xml | |
| - test-name: 'Multiple Files (XML only)' | |
| multiple-files: | | |
| Service A, ./data/coverage_1.xml, ./data/pytest_1.xml | |
| Service B, ./data/coverage_2.xml, ./data/pytest_3.xml | |
| steps: | |
| - uses: actions/checkout@v5 | |
| - name: Display test configuration | |
| run: | | |
| echo "Testing: ${{ matrix.test-name }}" | |
| echo "PR Number: ${{ needs.setup.outputs.pr-number }}" | |
| - name: Run pytest-coverage-comment | |
| id: coverageComment | |
| uses: MishaKav/pytest-coverage-comment@v1 | |
| with: | |
| pytest-xml-coverage-path: ${{ matrix.pytest-xml-coverage-path }} | |
| pytest-coverage-path: ${{ matrix.pytest-coverage-path }} | |
| junitxml-path: ${{ matrix.junitxml-path }} | |
| multiple-files: ${{ matrix.multiple-files }} | |
| issue-number: ${{ needs.setup.outputs.pr-number }} | |
| hide-badge: ${{ matrix.hide-badge || 'false' }} | |
| hide-report: ${{ matrix.hide-report || 'false' }} | |
| hide-comment: ${{ matrix.hide-comment || 'false' }} | |
| create-new-comment: ${{ matrix.create-new-comment || 'false' }} | |
| remove-link-from-badge: ${{ matrix.remove-link-from-badge || 'false' }} | |
| remove-links-to-files: ${{ matrix.remove-links-to-files || 'false' }} | |
| remove-links-to-lines: ${{ matrix.remove-links-to-lines || 'false' }} | |
| text-instead-badge: ${{ matrix.text-instead-badge || 'false' }} | |
| unique-id-for-comment: ${{ matrix.test-name }} | |
| - name: Check outputs | |
| run: | | |
| echo "=== ${{ matrix.test-name }} Results ===" | |
| echo "Coverage Percentage: ${{ steps.coverageComment.outputs.coverage }}" | |
| echo "Coverage Color: ${{ steps.coverageComment.outputs.color }}" | |
| echo "Coverage Warnings: ${{ steps.coverageComment.outputs.warnings }}" | |
| echo "" | |
| echo "Test Statistics:" | |
| echo " Tests: ${{ steps.coverageComment.outputs.tests }}" | |
| echo " Errors: ${{ steps.coverageComment.outputs.errors }}" | |
| echo " Failures: ${{ steps.coverageComment.outputs.failures }}" | |
| echo " Skipped: ${{ steps.coverageComment.outputs.skipped }}" | |
| echo " Time: ${{ steps.coverageComment.outputs.time }}" | |
| echo "" | |
| echo "Coverage HTML (first 20 lines):" | |
| echo "${{ steps.coverageComment.outputs.coverageHtml }}" | head -n 20 | |
| echo "" | |
| echo "✅ Test completed successfully!" |