|
| 1 | +name: Python CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, develop ] |
| 6 | + paths: |
| 7 | + - 'src/**' |
| 8 | + - 'tests/**' |
| 9 | + - 'requirements.txt' |
| 10 | + - '.github/workflows/python-ci.yml' |
| 11 | + pull_request: |
| 12 | + branches: [ main, develop ] |
| 13 | + paths: |
| 14 | + - 'src/**' |
| 15 | + - 'tests/**' |
| 16 | + - 'requirements.txt' |
| 17 | + - '.github/workflows/python-ci.yml' |
| 18 | + |
| 19 | +jobs: |
| 20 | + test: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + strategy: |
| 24 | + matrix: |
| 25 | + python-version: ['3.12'] |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Set up Python ${{ matrix.python-version }} |
| 32 | + uses: actions/setup-python@v5 |
| 33 | + with: |
| 34 | + python-version: ${{ matrix.python-version }} |
| 35 | + |
| 36 | + - name: Cache pip dependencies |
| 37 | + uses: actions/cache@v4 |
| 38 | + with: |
| 39 | + path: ~/.cache/pip |
| 40 | + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} |
| 41 | + restore-keys: | |
| 42 | + ${{ runner.os }}-pip- |
| 43 | +
|
| 44 | + - name: Install dependencies |
| 45 | + run: | |
| 46 | + python -m pip install --upgrade pip |
| 47 | + pip install -r requirements.txt |
| 48 | + pip install pytest pytest-cov flake8 black isort |
| 49 | +
|
| 50 | + - name: Lint with flake8 |
| 51 | + run: | |
| 52 | + # stop the build if there are Python syntax errors or undefined names |
| 53 | + flake8 src --count --select=E9,F63,F7,F82 --show-source --statistics |
| 54 | + # exit-zero treats all errors as warnings |
| 55 | + flake8 src --count --exit-zero --max-complexity=10 --max-line-length=88 --statistics |
| 56 | +
|
| 57 | + - name: Check code formatting with black |
| 58 | + run: | |
| 59 | + black --check src tests |
| 60 | +
|
| 61 | + - name: Check import sorting with isort |
| 62 | + run: | |
| 63 | + isort --check-only src tests |
| 64 | +
|
| 65 | + - name: Run tests with pytest |
| 66 | + run: | |
| 67 | + if [ "$(find tests/ -name "*.py" -exec grep -l "def test_" {} \; | wc -l)" -gt 0 ]; then |
| 68 | + pytest tests/ --cov=src --cov-report=xml --cov-report=term-missing |
| 69 | + else |
| 70 | + echo "No tests found with test functions, skipping test execution" |
| 71 | + echo "Creating empty coverage.xml for consistency" |
| 72 | + echo '<?xml version="1.0" ?><coverage version="0.0" timestamp="0" lines-valid="0" lines-covered="0" line-rate="0.0"></coverage>' > coverage.xml |
| 73 | + fi |
| 74 | +
|
| 75 | + - name: Upload coverage to Codecov |
| 76 | + uses: codecov/codecov-action@v4 |
| 77 | + if: matrix.python-version == '3.12' |
| 78 | + with: |
| 79 | + file: ./coverage.xml |
| 80 | + flags: unittests |
| 81 | + name: codecov-umbrella |
| 82 | + fail_ci_if_error: false |
0 commit comments