# Reusable workflow consumed by tests.yaml; used to share a single matrix across jobs.
on:
  workflow_call:
    inputs:
      runner:
        required: true
        type: string
      python-version:
        required: true
        type: string
      run-mypy:
        required: true
        type: boolean
      run-pytest:
        required: true
        type: boolean
      run-pytest-export:
        required: true
        type: boolean

defaults:
  run:
    shell: bash

env:
  PYTHONWARNDEFAULTENCODING: 'true'

jobs:
  mypy:
    name: mypy
    runs-on: ${{ inputs.runner }}
    if: inputs.run-mypy
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false

      - uses: ./.github/actions/bootstrap-poetry
        id: bootstrap-poetry
        with:
          python-version: ${{ inputs.python-version }}

      - uses: ./.github/actions/poetry-install

      - uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
        with:
          path: .mypy_cache
          key: mypy-${{ runner.os }}-py${{ steps.bootstrap-poetry.outputs.python-version }}-${{ hashFiles('pyproject.toml', 'poetry.lock') }}
          restore-keys: |
            mypy-${{ runner.os }}-py${{ steps.bootstrap-poetry.outputs.python-version }}-
            mypy-${{ runner.os }}-

      - run: poetry run mypy

  pytest:
    name: pytest
    runs-on: ${{ inputs.runner }}
    if: inputs.run-pytest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false

      - uses: ./.github/actions/bootstrap-poetry
        with:
          python-version: ${{ inputs.python-version }}

      - uses: ./.github/actions/poetry-install
        with:
          args: --with github-actions

      - run: poetry run pytest --integration -v
        env:
          POETRY_TEST_INTEGRATION_GIT_USERNAME: ${{ github.actor }}
          POETRY_TEST_INTEGRATION_GIT_PASSWORD: ${{ github.token }}

      - run: git diff --exit-code --stat HEAD

  pytest-export:
    name: pytest (poetry-plugin-export)
    runs-on: ${{ inputs.runner }}
    if: inputs.run-pytest-export
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
          path: poetry

      - uses: ./poetry/.github/actions/bootstrap-poetry
        with:
          python-version: ${{ inputs.python-version }}

      - name: Get poetry-plugin-export version
        run: |
          PLUGIN_VERSION=$(curl -s https://pypi.org/pypi/poetry-plugin-export/json | jq -r ".info.version")
          echo "Found version ${PLUGIN_VERSION}"
          echo version=${PLUGIN_VERSION} >> $GITHUB_OUTPUT
        id: poetry-plugin-export-version

      - name: Check out poetry-plugin-export
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
          path: poetry-plugin-export
          repository: python-poetry/poetry-plugin-export
          # use main for now because of poetry-core#826
          # ref: refs/tags/${{ steps.poetry-plugin-export-version.outputs.version }}

      - name: Use local poetry
        working-directory: poetry-plugin-export
        # Replace the python version to avoid conflicts
        # if the plugin still supports a wider range than Poetry itself.
        run: |
          perl -pi -e 's/^python =.*$/python = "~'"${PYTHON_VERSION}"'"/' pyproject.toml
          poetry remove --lock poetry-core  # use whatever poetry uses
          poetry add --lock --group dev ../poetry
        env:
          PYTHON_VERSION: ${{ inputs.python-version }}

      # This step can be removed after having released a poetry-plugin-export version
      # that has cffi>=1.17.0 in its lock file.
      - name: Force more recent cffi (workaround for Python 3.13)
        working-directory: poetry-plugin-export
        run: poetry update --lock cffi

      - name: Install
        working-directory: poetry-plugin-export
        run: poetry install

      - name: Run tests
        working-directory: poetry-plugin-export
        run: poetry run pytest -v

      - name: Check for clean working tree
        working-directory: poetry-plugin-export
        run: |
          git checkout -- pyproject.toml poetry.lock
          git diff --exit-code --stat HEAD
