---
name: Python

# HINT: Sync this paths with the egrep in step check_files
on:
  push:
    branches: [ "master", "main" ]
    paths:
      - 'pyproject.toml'
      - 'setup.cfg'
      - '**.py'
      - '.github/workflows/*.yml'

  pull_request:
    branches: [ "master", "main" ]
    paths:
      - 'pyproject.toml'
      - 'setup.cfg'
      - '**.py'
      - '.github/workflows/*.yml'

permissions:
  contents: read

concurrency:
  # only cancel in-progress runs of the same workflow
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true


jobs:
  check-files:
    runs-on: ubuntu-latest
    outputs:
      can_run: ${{ steps.check_files.outputs.can_run }}

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: GitHub variables
        id: gh-vars
        run: |
          for var in GITHUB_WORKFLOW GITHUB_ACTION GITHUB_ACTIONS GITHUB_REPOSITORY GITHUB_EVEN_NAME GITHUB_EVENT_PATH GITHUB_WORKSPACE GITHUB_SHA GITHUB_REF GITHUB_HEAD_REF GITHUB_BASE_REF; do
            echo "$var = ${!var}"
          done

      - name: Check for file changes
        id: check_files
        run: |
          # ${{ github.event.after }} ${{ github.event.before }}
          can_run=$(git diff --name-only HEAD~1 HEAD | \
           egrep -q '.github/workflows/|pyproject.toml|setup.cfg|\.py$' && echo 1 || echo 0)
          echo "can_run=$can_run"
          echo "can_run=$can_run" >> $GITHUB_OUTPUT

  skip_test:
    runs-on: ubuntu-latest
    needs: check-files
    timeout-minutes: 2
    if: ${{ needs.check-files.outputs.can_run == '0' }}

    steps:
      - name: Skip test
        run: |
          echo "Nothing to do as no TOML, Python, or YAML file has been changed.
          "
          echo "Skipping."

  check:
    runs-on: ubuntu-latest
    needs: check-files
    # Timout of 15min
    timeout-minutes: 15
    # needs.check-files.outputs.can_run
    if: ${{ needs.check-files.outputs.can_run == '1' }}

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
          cache: 'pip'
      - name: Install dependencies
        run: |
          python3 -m pip install --upgrade pip setuptools>60 setuptools-scm>=60
          pip install tox tox-gh-actions
      - name: Check
        run: |
          tox -e checks

  tests:
    needs: check
    runs-on: ${{ matrix.os }}
    strategy:
      max-parallel: 5
      fail-fast: true
      matrix:
        python-version: ["3.7",
                         "3.8",
                         "3.9",
                         "3.10",
                         "3.11",
                         # "3.12-dev"
                         ]
        os: [ubuntu-latest, "macos-latest"]

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }} for ${{ matrix.os }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          cache: 'pip'
      - name: Install dependencies
        run: |
          python3 -m pip install --upgrade pip
          pip install tox tox-gh-actions
      - name: Test with tox
        run: |
          tox
