name: tests

on:
  push:
    # Avoid using all the resources/limits available by checking only
    # relevant branches and tags. Other branches can be checked via PRs.
    # branches: [main]
    tags: ['v[0-9]*', '[0-9]+.[0-9]+*']  # Match tags that resemble a version
  pull_request:
  workflow_dispatch:  # Allow manually triggering the workflow
  schedule:
    # Run roughly every 15 days at 00:00 UTC
    # (useful to check if updates on dependencies break the package)
    - cron: '0 0 1,16 * *'

concurrency:
  group: >-
    ${{ github.workflow }}-${{ github.ref_type }}-
    ${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

env:
  VALIDATE_PYPROJECT_CACHE_REMOTE: tests/.cache


jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      wheel-distribution: ${{ steps.wheel-distribution.outputs.path }}
    steps:
      - uses: actions/checkout@v7
        with: {fetch-depth: 0}  # deep clone for setuptools-scm
      - uses: actions/setup-python@v7
        with: {python-version: "3.x"}
      - uses: astral-sh/setup-uv@v10.0.1
      - name: Run static analysis and format checkers
        run: >-
          uvx --with tox-uv
          tox -e lint,typecheck
      - name: Build package distribution files
        run: >-
          uvx --with tox-uv
          tox -e clean,build
      - name: Record the path of wheel distribution
        id: wheel-distribution
        run: echo "path=$(ls dist/*.whl)" >> $GITHUB_OUTPUT
      - name: Store the distribution files for use in other stages
        # `tests` and `publish` will use the same pre-built distributions,
        # so we make sure to release the exact same package that was tested
        uses: actions/upload-artifact@v7
        with:
          name: python-distribution-files
          path: dist/
          retention-days: 1
      - name: Download files used for testing
        run: python tools/cache_urls_for_tests.py
      - name: Store downloaded files
        uses: actions/upload-artifact@v7
        with:
          name: test-download-files
          path: ${{ env.VALIDATE_PYPROJECT_CACHE_REMOTE }}
          include-hidden-files: true
          if-no-files-found: error
          retention-days: 1

  test:
    needs: prepare
    strategy:
      matrix:
        python:
        - "3.9"   # oldest Python supported by validate-pyproject
        - "3.x"   # newest Python that is stable
        platform:
        - ubuntu-latest
        - macos-15-intel
        - windows-latest
    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with:
          python-version: ${{ matrix.python }}
          allow-prereleases: true
      - uses: astral-sh/setup-uv@v10.0.1
      - name: Retrieve pre-built distribution files
        uses: actions/download-artifact@v8
        with: {name: python-distribution-files, path: dist/}
      - name: Retrieve test download files
        uses: actions/download-artifact@v8
        with:
          name: test-download-files
          path: ${{ env.VALIDATE_PYPROJECT_CACHE_REMOTE }}
      - name: Run tests
        run: >-
          uvx --with tox-uv
          tox
          --installpkg '${{ needs.prepare.outputs.wheel-distribution }}'
          -- -n 5 -rFEx --durations 10 --color yes
      - name: Generate coverage report
        run: pipx run coverage lcov -o coverage.lcov
      - name: Upload partial coverage report
        uses: coverallsapp/github-action@master
        with:
          path-to-lcov: coverage.lcov
          github-token: ${{ secrets.github_token }}
          flag-name: ${{ matrix.platform }} - py${{ matrix.python }}
          parallel: true

  test-mamba:
    needs: prepare
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - name: Setup Micromamba
        uses: mamba-org/setup-micromamba@v3
        with:
          environment-name: test
          create-args: >-
            pip
            pipx
            tox
            curl
      - name: Retrieve pre-built distribution files
        uses: actions/download-artifact@v8
        with: {name: python-distribution-files, path: dist/}
      - name: Retrieve test download files
        uses: actions/download-artifact@v8
        with:
          name: test-download-files
          path: ${{ env.VALIDATE_PYPROJECT_CACHE_REMOTE }}
      - name: Run tests
        shell: bash -el {0}
        run: >-
          tox
          --installpkg '${{ needs.prepare.outputs.wheel-distribution }}'
          -- -n 5 -rFEx --durations 10 --color yes

  linkcheck:
    needs: finalize
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with: {python-version: "3.x"}
      - uses: astral-sh/setup-uv@v10.0.1
      - name: Retrieve pre-built distribution files
        uses: actions/download-artifact@v8
        with: {name: python-distribution-files, path: dist/}
      - name: Check links
        run: >-
          uvx --with tox-uv
          tox --installpkg dist/*.whl -e linkcheck -- -q

  finalize:
    needs: [test, test-mamba]
    runs-on: ubuntu-slim
    steps:
      - name: Finalize coverage report
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel-finished: true

  publish:
    needs: finalize
    if: ${{ github.event_name == 'push' && contains(github.ref, 'refs/tags/') }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with: {python-version: "3.x"}
      - uses: astral-sh/setup-uv@v10.0.1
      - name: Retrieve pre-built distribution files
        uses: actions/download-artifact@v8
        with: {name: python-distribution-files, path: dist/}
      - name: Publish Package
        env:
          # See: https://pypi.org/help/#apitoken
          TWINE_REPOSITORY: pypi
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
        run: >-
          uvx --with tox-uv
          tox -e publish
