#############################
## GitHub Actions CI Tests ##
#############################
#
# This is a reusable workflow to make CI tests more modular.
# See: https://docs.github.com/en/actions/using-workflows/reusing-workflows
#
# Called by ci-tests.yml
# This one does the python tests
#

name: Modular SQLFluff python test workflow

on:
  workflow_call:
    inputs:
      python-version:
        required: true
        type: string
      marks:
        required: false
        type: string
        default: "not integration"
      coverage:
        required: false
        type: boolean
        default: false
    secrets:
      gh_token:
        required: true

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.python-version }}-${{ inputs.marks }}-${{ inputs.coverage }}
  cancel-in-progress: true

jobs:
  modular-python-test:
    runs-on: ubuntu-latest
    name: py${{ inputs.python-version }}
    steps:
    - uses: actions/checkout@v4

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: ${{ inputs.python-version }}
        cache: 'pip'
        cache-dependency-path: |
          setup.cfg
          requirements_dev.txt

    - name: Install dependencies
      run: pip install tox

    - name: Parse Python Version
      id: py_version
      run: |
        PYVERSION=$(echo "${{ inputs.python-version }}" | sed -e 's/\.//g')
        echo "PYVERSION=$PYVERSION" >> $GITHUB_OUTPUT

    # Run test process (with or without coverage).
    # Arguments after the "--" are passed through to pytest:
    #   --cov=...       The library to include in coverage reporting.
    #   -n 2            Runs with two parallel processes.
    #   test            The path to detect tests within.
    #   -m ...          The pytest marks to filter tests.
    #   --durations=16  Displays the 16 slowest runs to help with performance debugging.
    - name: Run the tests (with coverage)
      # NOTE: We have a separate job for coverage reporting because
      # it impacts performance and slows the test suite significantly.
      if: ${{ inputs.coverage }}
      run: tox -e py${{ steps.py_version.outputs.PYVERSION }} -- --cov=sqlfluff -n 2 test -m "${{ inputs.marks }}" --durations=16 --verbosity=0
    - name: Run the tests (without coverage)
      if: ${{ !inputs.coverage }}
      run: tox -e py${{ steps.py_version.outputs.PYVERSION }} -- -n 2 test -m "${{ inputs.marks }}" --durations=16 --verbosity=0

    - name: Rename coverage files with suffix
      # NOTE: We do this because we're using the same tox environment for multiple
      # test jobs and we need to make sure that their coverage files don't collide.s
      id: cov_suffix
      if: ${{ inputs.coverage }}
      run: |
        COVSUFFIX=$(echo "${{ inputs.marks }}" | sed -e 's/ /-/g')
        echo "COVSUFFIX=$COVSUFFIX" >> $GITHUB_OUTPUT
        for file in .coverage.*; do mv "$file" "$file.$COVSUFFIX"; done;

    - name: Upload coverage data (github)
      uses: actions/upload-artifact@v4
      if: ${{ inputs.coverage }}
      with:
        name: coverage-data-py${{ inputs.python-version }}-${{ inputs.marks }}
        path: ".coverage.*"
        if-no-files-found: ignore
        include-hidden-files: true
