|
| 1 | +name: Tests |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ main, master ] |
| 6 | + push: |
| 7 | + branches: [ main, master ] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: write |
| 13 | + checks: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + test: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + strategy: |
| 19 | + matrix: |
| 20 | + python-version: ["3.11", "3.12", "3.13"] |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout code |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Set up Python ${{ matrix.python-version }} |
| 27 | + uses: actions/setup-python@v5 |
| 28 | + with: |
| 29 | + python-version: ${{ matrix.python-version }} |
| 30 | + cache: 'pip' |
| 31 | + |
| 32 | + - name: Install dependencies |
| 33 | + run: | |
| 34 | + python -m pip install --upgrade pip |
| 35 | + pip install -e ".[dev]" |
| 36 | +
|
| 37 | + - name: Run linting |
| 38 | + run: | |
| 39 | + ruff check src tests |
| 40 | + continue-on-error: true |
| 41 | + |
| 42 | + - name: Run type checking |
| 43 | + run: | |
| 44 | + mypy src |
| 45 | + continue-on-error: true |
| 46 | + |
| 47 | + - name: Run tests with coverage |
| 48 | + run: | |
| 49 | + pytest tests/ \ |
| 50 | + --cov=src/devman \ |
| 51 | + --cov-report=term \ |
| 52 | + --cov-report=html \ |
| 53 | + --cov-report=json \ |
| 54 | + --cov-report=xml \ |
| 55 | + -v |
| 56 | +
|
| 57 | + - name: Generate coverage badge |
| 58 | + if: matrix.python-version == '3.13' |
| 59 | + run: | |
| 60 | + COVERAGE=$(python -c "import json; print(json.load(open('coverage.json'))['totals']['percent_covered_display'])") |
| 61 | + echo "COVERAGE=$COVERAGE" >> $GITHUB_ENV |
| 62 | + echo "Coverage: $COVERAGE%" |
| 63 | +
|
| 64 | + - name: Upload coverage reports |
| 65 | + if: matrix.python-version == '3.13' |
| 66 | + uses: actions/upload-artifact@v4 |
| 67 | + with: |
| 68 | + name: coverage-reports |
| 69 | + path: | |
| 70 | + htmlcov/ |
| 71 | + coverage.json |
| 72 | + coverage.xml |
| 73 | + .coverage |
| 74 | +
|
| 75 | + - name: Comment PR with test results |
| 76 | + if: github.event_name == 'pull_request' && matrix.python-version == '3.13' |
| 77 | + uses: actions/github-script@v7 |
| 78 | + with: |
| 79 | + script: | |
| 80 | + const fs = require('fs'); |
| 81 | +
|
| 82 | + // Read coverage data |
| 83 | + const coverage = JSON.parse(fs.readFileSync('coverage.json', 'utf8')); |
| 84 | + const totalCoverage = coverage.totals.percent_covered_display; |
| 85 | +
|
| 86 | + // Read test output |
| 87 | + let testOutput = ''; |
| 88 | + try { |
| 89 | + testOutput = fs.readFileSync('test-output.txt', 'utf8'); |
| 90 | + } catch (e) { |
| 91 | + testOutput = 'Test output not available'; |
| 92 | + } |
| 93 | +
|
| 94 | + // Build coverage table |
| 95 | + let coverageTable = '| File | Coverage |\n|------|----------|\n'; |
| 96 | + for (const [file, data] of Object.entries(coverage.files)) { |
| 97 | + const fileName = file.replace('src/devman/', ''); |
| 98 | + const fileCoverage = data.summary.percent_covered_display; |
| 99 | + coverageTable += `| \`${fileName}\` | ${fileCoverage}% |\n`; |
| 100 | + } |
| 101 | +
|
| 102 | + // Create comment body |
| 103 | + const body = `## 🧪 Test Results (Python ${{ matrix.python-version }}) |
| 104 | +
|
| 105 | + ### 📊 Coverage Summary |
| 106 | + **Total Coverage: ${totalCoverage}%** |
| 107 | +
|
| 108 | + <details> |
| 109 | + <summary>Coverage by File</summary> |
| 110 | +
|
| 111 | + ${coverageTable} |
| 112 | +
|
| 113 | + </details> |
| 114 | +
|
| 115 | + ### 📈 Coverage Details |
| 116 | + - **Lines:** ${coverage.totals.covered_lines}/${coverage.totals.num_statements} covered |
| 117 | + - **Branches:** ${coverage.totals.covered_branches}/${coverage.totals.num_branches} covered |
| 118 | + - **Missing Lines:** ${coverage.totals.missing_lines} |
| 119 | +
|
| 120 | + --- |
| 121 | + *Coverage report generated by pytest-cov*`; |
| 122 | +
|
| 123 | + // Post or update comment |
| 124 | + const { data: comments } = await github.rest.issues.listComments({ |
| 125 | + owner: context.repo.owner, |
| 126 | + repo: context.repo.repo, |
| 127 | + issue_number: context.issue.number, |
| 128 | + }); |
| 129 | +
|
| 130 | + const botComment = comments.find(comment => |
| 131 | + comment.user.type === 'Bot' && |
| 132 | + comment.body.includes('Test Results (Python ${{ matrix.python-version }})') |
| 133 | + ); |
| 134 | +
|
| 135 | + if (botComment) { |
| 136 | + await github.rest.issues.updateComment({ |
| 137 | + owner: context.repo.owner, |
| 138 | + repo: context.repo.repo, |
| 139 | + comment_id: botComment.id, |
| 140 | + body: body |
| 141 | + }); |
| 142 | + } else { |
| 143 | + await github.rest.issues.createComment({ |
| 144 | + owner: context.repo.owner, |
| 145 | + repo: context.repo.repo, |
| 146 | + issue_number: context.issue.number, |
| 147 | + body: body |
| 148 | + }); |
| 149 | + } |
| 150 | +
|
| 151 | + - name: Check coverage threshold |
| 152 | + if: matrix.python-version == '3.13' |
| 153 | + run: | |
| 154 | + COVERAGE=$(python -c "import json; print(float(json.load(open('coverage.json'))['totals']['percent_covered_display']))") |
| 155 | + THRESHOLD=70 |
| 156 | + echo "Coverage: $COVERAGE%" |
| 157 | + echo "Threshold: $THRESHOLD%" |
| 158 | + if (( $(echo "$COVERAGE < $THRESHOLD" | bc -l) )); then |
| 159 | + echo "⚠️ Warning: Coverage $COVERAGE% is below threshold $THRESHOLD%" |
| 160 | + exit 0 # Don't fail the build, just warn |
| 161 | + else |
| 162 | + echo "✅ Coverage $COVERAGE% meets threshold $THRESHOLD%" |
| 163 | + fi |
| 164 | +
|
| 165 | + nix-build: |
| 166 | + runs-on: ubuntu-latest |
| 167 | + steps: |
| 168 | + - name: Checkout code |
| 169 | + uses: actions/checkout@v4 |
| 170 | + |
| 171 | + - name: Install Nix |
| 172 | + uses: cachix/install-nix-action@v25 |
| 173 | + with: |
| 174 | + nix_path: nixpkgs=channel:nixos-unstable |
| 175 | + |
| 176 | + - name: Build with Nix |
| 177 | + run: nix build .#devman |
| 178 | + |
| 179 | + - name: Test devman CLI |
| 180 | + run: | |
| 181 | + ./result/bin/devman --help || echo "Binary not in expected location" |
0 commit comments