# Python package
# Create and test a Python package on multiple Python versions.
# Add steps that analyze code, save the dist with the build record, publish to a PyPI-compatible index, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/python

parameters:
  # defaults for any parameters that are not specified
  name: ''
  vmImage: ''


jobs:

- job: ${{ parameters.name }}Test
  pool:
    vmImage: ${{ parameters.vmImage }}
  strategy:
    matrix:
      ${{ if eq(parameters.name, 'Linux') }}:
        python_312_latest:
          python.version: '3.12'
          coverage: true
        python_311_latest:
          python.version: '3.11'
          lint: true
          CYTHON: 3.0.10
          coverage: true
        python_310_latest_install:
          python.version: '3.10'
          lint: true
          test.install: true
        python_310_latest_install_sdist:
          python.version: '3.10'
          test.install: true
          TEST_SDIST: true
        python_39:
          python.version: '3.9'
          USE_MATPLOTLIB: false
          USE_CVXOPT: false
          SCIPY: 1.8.1
          PANDAS: 1.5.3
          NUMPY: 1.23.5
          CYTHON: 3.0.10
          coverage: true
          lint: true
        python_310_legacy_blas:
          python.version: '3.10'
          use.conda: true
          coverage: true
          NUMPY: 2.0.0
          PANDAS: 2.2.2
          SCIPY: 1.13.0
          CYTHON: 3.0.10
          BLAS: "nomkl blas=*=openblas"
          PYTEST_OPTIONS: ''
        python310_numpy124:
          python.version: '3.10'
          NUMPY: 1.24.4
          SCIPY: 1.9.3
          PANDAS: 1.5.3
          MATPLOTLIB: 3.6.0
        python_312_copy_on_write:
          python.version: '3.12'
          SM_TEST_COPY_ON_WRITE: 1
        python_312_pre:
          python.version: '3.12'
          pip.pre: true
      ${{ if eq(parameters.name, 'macOS') }}:
        python310_macos_latest:
          python.version: '3.10'
        python311_macos_latest:
          python.version: '3.11'
        python312_macos_latest:
          python.version: '3.12'
    maxParallel: 10

  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'
      architecture: 'x64'
    displayName: 'Use Python $(python.version)'

  - bash: |
      echo "##vso[task.prependpath]$CONDA/bin"
      echo "##vso[task.prependpath]$CONDA/envs/statsmodels-test"
      echo "##vso[task.prependpath]$CONDA/envs/statsmodels-test/condabin"
      echo "##vso[task.prependpath]$CONDA/envs/statsmodels-test/bin"
    displayName: 'Add conda and env to PATH'
    condition: eq(variables['use.conda'], 'true')

  - script: |
      source tools/ci/azure/install-posix.sh
    displayName: 'Install dependencies'

  - script: ./lint.sh
    displayName: 'Check style'
    condition: eq(variables['lint'], 'true')

  - script: |
      echo "Installing to site packages"
      if [[ $TEST_SDIST ]]; then
        python -m build . --sdist
        TAIL=".tar.gz"
      else
        python -m pip wheel . -w ./dist -v
        TAIL=".whl"
      fi
      ls -t dist/statsmodels*${TAIL}
      PKG_NAME=$(ls -t dist/statsmodels*${TAIL})
      python -m pip install ./${PKG_NAME}
    displayName: 'Install statsmodels (site-packages)'
    condition: eq(variables['test.install'], 'true')

  - script: |
      python -m pip install -e . -vv --no-build-isolation --upgrade-strategy only-if-needed
    displayName: 'Install statsmodels (editable)'
    condition: ne(variables['test.install'], 'true')

  - script: python -m pip list
    displayName: 'List Configuration (PyPI)'
    condition: ne(variables['use.conda'], 'true')

  - script: |
      source activate statsmodels-test
      conda list
    displayName: 'List Configuration (conda)'
    condition: eq(variables['use.conda'], 'true')

  - script: |
      echo "Testing site packages"
      mkdir test_run_dir
      pushd test_run_dir
      python -c "import statsmodels; statsmodels.test(['-n', 'auto', '--junitxml=../junit/test-results.xml', '--skip-examples', '--dist', 'loadscope', '--durations=25'], exit=True)"
      LAST_EXIT_CODE=$?
      echo "Status code: ${LAST_EXIT_CODE}"
      exit ${LAST_EXIT_CODE}
      popd
    displayName: 'Run tests (site-packages)'
    condition: eq(variables['test.install'], 'true')

  - script: |
      echo "Testing editable install"
      if [[ ${COVERAGE} == "true" ]]; then
        export COVERAGE_OPTS="--cov-config .coveragerc --cov=statsmodels --cov-report xml:coverage.xml --cov-report term"
      fi
      echo pytest -m "${PYTEST_PATTERN}" --skip-examples --junitxml=junit/test-results.xml -n auto --dist loadscope --durations=25 ${COVERAGE_OPTS} statsmodels
      pytest -m "${PYTEST_PATTERN}" --skip-examples --junitxml=junit/test-results.xml -n auto --dist loadscope --durations=25 ${COVERAGE_OPTS} statsmodels
    displayName: 'Run tests (editable)'
    condition: and(ne(variables['test.install'], 'true'), ne(variables['pip.pre'], 'true'))

  - script: |
      echo "Testing pip-pre"
      if [[ ${COVERAGE} == "true" ]]; then
        export COVERAGE_OPTS="--cov-config .coveragerc --cov=statsmodels --cov-report xml:coverage.xml --cov-report term"
      fi
      echo pytest -m "${PYTEST_PATTERN}" --skip-examples --junitxml=junit/test-results.xml -n auto --dist loadscope --durations=25 ${COVERAGE_OPTS} statsmodels
      pytest -m "${PYTEST_PATTERN}" --skip-examples --junitxml=junit/test-results.xml -n auto --dist loadscope --durations=25 ${COVERAGE_OPTS} statsmodels
    displayName: 'Run tests (pip pre)'
    condition: and(ne(variables['test.install'], 'true'), eq(variables['pip.pre'], 'true'))
    continueOnError: true

  - task: PublishTestResults@2
    inputs:
      testResultsFiles: '**/test-results.xml'
      testRunTitle: 'Python $(python.version)'
    condition: succeededOrFailed()

  - task: PublishCodeCoverageResults@2
    inputs:
      summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
    condition: and(eq(variables['coverage'], 'true'), ne(variables['test.install'], 'true'))

  - bash: |
      curl -Os https://uploader.codecov.io/latest/linux/codecov
      chmod +x codecov
      ./codecov
    displayName: 'CodeCov upload'
    condition: and(eq(variables['coverage'], 'true'), ne(variables['test.install'], 'true'))
    continueOnError: true
