---
name: Testing

# yamllint disable-line rule:truthy
on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

env:
  DEFAULT_PYTHON: "3.11"

jobs:
  pytest:
    name: Python ${{ matrix.python }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python: ["3.11", "3.12", "3.13"]
    steps:
      - name: ⤵️ Check out code from GitHub
        uses: actions/checkout@v6.0.2
      - name: 🏗 Set up Poetry
        run: pipx install poetry
      - name: 🏗 Set up Python ${{ matrix.python }}
        id: python
        uses: actions/setup-python@v6.2.0
        with:
          python-version: ${{ matrix.python }}
          cache: "poetry"
      - name: 🏗 Install workflow dependencies
        run: |
          poetry config virtualenvs.create true
          poetry config virtualenvs.in-project true
      - name: 🏗 Install dependencies
        run: poetry install --no-interaction
      - name: 🚀 Run pytest
        run: poetry run pytest --cov src tests
      - name: ⬆️ Upload coverage artifact
        uses: actions/upload-artifact@v6.0.0
        with:
          include-hidden-files: true
          name: coverage-${{ matrix.python }}
          path: .coverage

  coverage:
    runs-on: ubuntu-latest
    needs: pytest
    steps:
      - name: ⤵️ Check out code from GitHub
        uses: actions/checkout@v6.0.2
        with:
          fetch-depth: 0
      - name: ⬇️ Download coverage data
        uses: actions/download-artifact@v7.0.0
      - name: 🏗 Set up Poetry
        run: pipx install poetry
      - name: 🏗 Set up Python ${{ env.DEFAULT_PYTHON }}
        id: python
        uses: actions/setup-python@v6.2.0
        with:
          python-version: ${{ env.DEFAULT_PYTHON }}
          cache: "poetry"
      - name: 🏗 Install workflow dependencies
        run: |
          poetry config virtualenvs.create true
          poetry config virtualenvs.in-project true
      - name: 🏗 Install dependencies
        run: poetry install --no-interaction
      - name: 🚀 Process coverage results
        run: |
          poetry run coverage combine coverage*/.coverage*
          poetry run coverage xml -i
      - name: 🚀 Upload coverage report
        uses: codecov/codecov-action@v5.5.2
        with:
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}
      - name: SonarCloud Scan
        if: github.event.pull_request.head.repo.fork == false
        uses: SonarSource/sonarcloud-github-action@v5.0.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
