toward coverage badge #9
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 is a GitHub Actions workflow configuration file that runs Pytest on pushes and pull requests to the main and dev branches. It sets up a matrix of Python versions (3.8 and 3.9) to test against, checks out the code, installs dependencies, and runs the tests using Pytest. | |
name: pytest | |
on: | |
push: | |
branches: | |
- main | |
- dev | |
pull_request: | |
branches: | |
- main | |
- dev | |
jobs: | |
test: | |
# runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
os: [macos-latest, ubuntu-latest, windows-latest] # Specify the OS versions to test against | |
python-version: ["3.8", "3.9"] # Specify the Python versions to test against | |
runs-on: ${{ matrix.os }} # Use the OS from the matrix | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Python ${{ matrix.python-version }} | |
uses: actions/setup-python@v3 | |
with: | |
python-version: ${{ matrix.python-version }} # Use the version from the matrix | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install .[dev] | |
# pip install pytest # Add any other dependencies your project needs | |
- name: Run tests with coverage | |
run: | | |
pytest -v --cov=src/rattlesnake --cov-report=xml --cov-report=term | |
- name: Upload coverage to artifact (Ubuntu + Python 3.9 only) | |
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.9' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: coverage-report | |
path: coverage.xml | |
retention-days: 7 | |
# Separate job for badge generation (only runs after tests pass) | |
coverage-badge: | |
needs: test # Only run if tests pass | |
runs-on: ubuntu-latest | |
# if: github.ref == 'refs/heads/main' # Only create badge on main branch | |
if: github.ref == 'refs/heads/dev' # Only create badge on dev branch | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Python 3.9 | |
uses: actions/setup-python@v3 | |
with: | |
python-version: "3.9" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install .[dev] | |
# pip install pytest-cov coverage-badge # already included in dev dependencies | |
- name: Generate coverage report | |
run: | | |
pytest --cov=src/rattlesnake --cov-report=xml | |
- name: Generate coverage badge | |
run: | | |
# Create badges directory if it doesn't exist | |
mkdir -p badges | |
# Extract coverage percentage | |
COVERAGE=$(python -c "import xml.etree.ElementTree as ET; tree = ET.parse('coverage.xml'); root = tree.getroot(); print(f\"{float(root.attrib['line-rate']) * 100:.1f}\")") | |
echo "Coverage: $COVERAGE%" | |
# Determine badge color based on coverage | |
if (( $(echo "$COVERAGE >= 90" | bc -l) )); then | |
COLOR="brightgreen" | |
elif (( $(echo "$COVERAGE >= 80" | bc -l) )); then | |
COLOR="green" | |
elif (( $(echo "$COVERAGE >= 70" | bc -l) )); then | |
COLOR="yellow" | |
elif (( $(echo "$COVERAGE >= 60" | bc -l) )); then | |
COLOR="orange" | |
else | |
COLOR="red" | |
fi | |
# Download badge using shields.io | |
curl -o badges/coverage.svg "https://img.shields.io/badge/coverage-${COVERAGE}%25-${COLOR}.svg" | |
- name: Commit badge | |
run: | | |
git config --local user.email "action@github.com" | |
git config --local user.name "GitHub Action" | |
git add badges/coverage.svg | |
git diff --staged --quiet || git commit -m "Update coverage badge [skip ci]" | |
git push | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |