# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt

name: "Python Nightly Tests"

on:
  push:
    branches:
      - "**/*nightly*"
  schedule:
    # Run at 2:22am early every morning Eastern time (6/7:22 UTC)
    # so that we get tips of CPython development tested.
    # https://crontab.guru/#22_7_%2a_%2a_%2a
    - cron: "22 7 * * *"
  workflow_dispatch:

defaults:
  run:
    shell: bash

env:
  PIP_DISABLE_PIP_VERSION_CHECK: 1
  COVERAGE_IGOR_VERBOSE: 2
  HYPOTHESIS_PROFILE: ci

permissions:
  contents: read

concurrency:
  group: "${{ github.workflow }}-${{ github.ref }}"
  cancel-in-progress: true

jobs:
  tests:
    name: "${{ matrix.python-version }}"
    runs-on: "${{ matrix.os }}"
    # If it doesn't finish in an hour, it's not going to. Don't spin for six
    # hours needlessly.
    timeout-minutes: 60

    env:
      TOX_GH_MAJOR_MINOR: "${{ matrix.python-version }}"

    strategy:
      matrix:
        os:
          - "ubuntu-latest"
        python-version:
          # When changing this list, be sure to check the [gh] list in
          # tox.ini so that tox will run properly. PYVERSIONS
          - "3.13"
          - "3.13t"
          - "3.14"
          - "3.14t"
          - "3.15"
          - "3.15t"
          - "3.16"
          - "3.16t"
      fail-fast: false

    steps:
      - name: "Check out the repo"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install system dependencies"
        run: |
          sudo sed -i.bak 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/ubuntu.sources
          sudo apt-get update
          sudo apt-get build-dep -y python3

      - name: "Install pyenv"
        run: |
          curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

      - name: "Build Python ${{ matrix.python-version }}-dev"
        env:
          PYVER: "${{ matrix.python-version }}-dev"
        run: |
          set -xe
          export PYENV_ROOT="$HOME/.pyenv"
          export PATH="$PYENV_ROOT/bin:$PATH"
          echo "$PYENV_ROOT/shims:$PYENV_ROOT/bin" >> "$GITHUB_PATH"
          pyenv install "$PYVER" -v
          pyenv global "$PYVER"

      - name: "Show diagnostic info"
        run: |
          set -xe
          python -VV
          python -m site
          python -m sysconfig
          python -c "import sys; print('GIL:', getattr(sys, '_is_gil_enabled', lambda: True)())"
          python -m pip --version
          python -m coverage debug sys
          python -m coverage debug sqlite
          python -m coverage debug pybehave
          env | sort

      - name: "Check the Python version"
        env:
          EXPECTED: "${{ matrix.python-version }}"
        run: |
          python -c "
          import sys
          print(f'{sys.version = }')
          assert sys.version.startswith('$EXPECTED'.strip('t'))
          assert getattr(sys, '_is_gil_enabled', lambda: True)() == (not '$EXPECTED'.endswith('t'))
          "

      - name: "Install dependencies"
        run: |
          python -m pip install -r requirements/tox.pip

      - name: "Run tox"
        run: |
          python -m tox -v -- -rfsEX
