name: tests

on: [push, pull_request]

jobs:

  linting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      - uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip
          restore-keys: ${{ runner.os }}-pip
      - name: Install uv
        uses: astral-sh/setup-uv@v2
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
          # virtualenvs-path: ~/.venv
      - name: Load cached venv
        id: cached-uv-dependencies
        uses: actions/cache@v2
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/uv.lock') }}
      # install dependencies if cache does not exist
      - name: Check cache and install dependencies
        run: uv sync --all-extras --dev
        if: steps.cached-uv-dependencies.outputs.cache-hit != 'true'
      - name: Run linters
        run: |
          uv run pre-commit run --all-files

  test:
    needs: linting
    strategy:
      fail-fast: true
      matrix:
        os: [ "ubuntu-latest", "macos-latest" ]
        python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Check out repository
        uses: actions/checkout@v2
      - name: Set up python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install uv
        uses: astral-sh/setup-uv@v2
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
      - name: Load cached venv
        id: cached-uv-dependencies
        uses: actions/cache@v2
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/uv.lock') }}
      # install dependencies if cache does not exist
      - name: Check cache and install dependencies
        run: uv sync --all-extras --dev
        if: steps.cached-uv-dependencies.outputs.cache-hit != 'true'
      - name: Run tests
        run: |
          uv run tox
      # upload coverage stats
      - name: Upload coverage
        uses: codecov/codecov-action@v1
        with:
          file: ./coverage.xml
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}
