name: Array API Tests

on:
  workflow_call:
    inputs:
      package-name:
        required: true
        type: string
      module-name:
        required: false
        type: string
      extra-requires:
        required: false
        type: string
      package-version:
        required: false
        type: string
        default: '>= 0'
      python-versions:
        required: true
        type: string
        description: JSON array of Python versions to test against.
      pytest-extra-args:
        required: false
        type: string
      # This is not how I would prefer to implement this but it's the only way
      # that seems possible with GitHub Actions' limited expressions syntax
      xfails-file-extra:
        required: false
        type: string
      skips-file-extra:
        required: false
        type: string
      extra-env-vars:
        required: false
        type: string
        description: Multiline string of environment variables to set for the test run.

env:
  PYTEST_ARGS: "--max-examples 1000 -v -rxXfE ${{ inputs.pytest-extra-args }} --hypothesis-disable-deadline --durations 20"

jobs:
  tests:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ${{ fromJson(inputs.python-versions) }}

    steps:
    - name: Checkout array-api-compat
      uses: actions/checkout@v6
      with:
        path: array-api-compat

    - name: Checkout array-api-tests
      uses: actions/checkout@v6
      with:
        repository: data-apis/array-api-tests
        submodules: 'true'
        path: array-api-tests

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v6
      with:
        python-version: ${{ matrix.python-version }}

    - name: Set Extra Environment Variables
      # Set additional environment variables if provided
      if: inputs.extra-env-vars
      run: |
        echo "${{ inputs.extra-env-vars }}" >> $GITHUB_ENV

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install '${{ inputs.package-name }} ${{ inputs.package-version }}' ${{ inputs.extra-requires }}
        python -m pip install -r ${GITHUB_WORKSPACE}/array-api-tests/requirements.txt
        python -m pip install pytest-xdist

    - name: Dump pip environment
      run: pip freeze

    - name: Run the array API testsuite (${{ inputs.package-name }})
      env:
        ARRAY_API_TESTS_MODULE: array_api_compat.${{ inputs.module-name || inputs.package-name }}
        ARRAY_API_TESTS_VERSION: 2024.12
        # This enables the NEP 50 type promotion behavior (without it a lot of
        # tests fail on bad scalar type promotion behavior)
        NPY_PROMOTION_STATE: weak
      run: |
        export PYTHONPATH="${GITHUB_WORKSPACE}/array-api-compat"
        cd ${GITHUB_WORKSPACE}/array-api-tests
        pytest array_api_tests/ --xfails-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.xfails-file-extra }}-xfails.txt --skips-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.skips-file-extra}}-skips.txt ${PYTEST_ARGS}
