Skip to content

CI: Test coverage reporting #35

CI: Test coverage reporting

CI: Test coverage reporting #35

Workflow file for this run

name: Pull Request Tests
on: [pull_request]
jobs:
test:
if: "!contains(github.event.head_commit.message, '[skip ci]')"
runs-on: ubuntu-latest
steps:
# SETUP
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }} # Ensure branch is checked out, not detached state (so we can push a commit later)
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.9
cache: 'pip'
- name: Add checkout directory to PYTHONPATH
run: echo "PYTHONPATH=$(pwd):$PYTHONPATH" >> $GITHUB_ENV
- name: Cache pip environment
id: cache-pip-env
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ hashFiles('pyproject.toml') }}
restore-keys: pip-
- name: Install dependencies
id: install-dependencies
run: |
pip install .[tests,dev]
pip uninstall dagrunner -y
# TESTS (inc. test coverage)
- name: Run pytest + coverage report gen
run: pytest --cov=dagrunner --cov-report=term | tee coverage_output.txt
# TESTS (main branch)
- name: Cache ref branch coverage report
id: cache-ref-coverage
uses: actions/cache@v4
with:
path: ref/coverage_output.txt
key: ref-${{ github.event.pull_request.base.sha }}
- name: Checkout ref branch
if: steps.cache-ref-coverage.outputs.cache-hit != 'true'
uses: actions/checkout@v4
with:
path: ref
ref: ${{ github.base_ref }}
- name: Run tests with coverage for ref branch
if: steps.cache-ref-coverage.outputs.cache-hit != 'true'
run: |
cd ref
pytest --cov=dagrunner --cov-report=term | tee coverage_output.txt
# TESTS (compare coverage)
- name: Compare coverage
run: |
echo "pr_coverage_total=$(grep TOTAL coverage_output.txt | awk '{print $NF}' | awk '{print substr($0, 1, length($0)-1)}')" | tee -a $GITHUB_ENV
echo "ref_coverage_total=$(grep TOTAL ref/coverage_output.txt | awk '{print $NF}' | awk '{print substr($0, 1, length($0)-1)}')" | tee -a $GITHUB_ENV
if (( $pr_coverage_total < $ref_coverage_total )); then
echo "COVERAGE_DECREASED=true" | tee -a $GITHUB_ENV
else
echo "COVERAGE_DECREASED=false" | tee -a $GITHUB_ENV
fi
- name: Comment coverage report
if: env.COVERAGE_DECREASED == 'true'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const comment = "The test coverage has decreased from '${{ env.main_coverage_total }}%' to '${{ env.pr_coverage_total }}%'.\nPlease review test coverage. Summary report uploaded as artifact.";
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
- name: Upload coverage report
if: env.COVERAGE_DECREASED == 'true'
uses: actions/upload-artifact@v4
with:
name: coverage-report-pr
path: coverage_output.txt
# PRE-COMMIT
- name: Python interpreter version sha (PYSHA)
run: echo "PYSHA=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
- name: Cache pre-commit
uses: actions/cache@v3
id: pre-commit-cache
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PYSHA }}|${{ hashFiles('.pre-commit-config.yaml') }}
- name: pre-commit install
run: |
pre-commit install
- name: pre-commit run
run: |
pre-commit run --all-files
# DOCUMENTATION
- name: Build documentation
run: |
./docs/gen_docs dagrunner ./docs
- name: Check if documentation has changed
id: check-docs
run: |
git diff --quiet --exit-code || echo "::set-output name=changed::true"
# https://github.com/orgs/community/discussions/26560#discussioncomment-3531273
- name: Commit and push documentation changes
if: steps.check-docs.outputs.changed == 'true'
run: |
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
git commit -am "Automated reference documentation update for PR ${{ github.event.number }} [skip ci]"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}