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'
      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 200 -v -rxXfE --ci ${{ inputs.pytest-extra-args }} --hypothesis-disable-deadline"

jobs:
  tests:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        # min version of dask we needs drops support for python 3.9
        python-version: ${{ inputs.package-name == 'dask' && fromJson('[''3.10'', ''3.11'', ''3.12'']') || fromJson('[''3.9'', ''3.10'', ''3.11'', ''3.12'']') }}

    steps:
    - name: Checkout array-api-compat
      uses: actions/checkout@v4
      with:
        path: array-api-compat
    - name: Checkout array-api-tests
      uses: actions/checkout@v4
      with:
        repository: data-apis/array-api-tests
        submodules: 'true'
        path: array-api-tests
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      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
      # NumPy 1.21 doesn't support Python 3.11. There doesn't seem to be a way
      # to put this in the numpy 1.21 config file.
      if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))"
      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
    - name: Run the array API testsuite (${{ inputs.package-name }})
      if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))"
      env:
        ARRAY_API_TESTS_MODULE: array_api_compat.${{ inputs.module-name || inputs.package-name }}
        ARRAY_API_TESTS_VERSION: 2023.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}
