name: ITK.macOS.Python

trigger:
  branches:
    include:
    - master
    - release*
variables:
  ExternalDataVersion: 5.1.0
jobs:
- job: macOS
  timeoutInMinutes: 0
  cancelTimeoutInMinutes: 300
  pool:
    vmImage: macOS-10.15
  steps:
    - checkout: self
      clean: true
      fetchDepth: 5
    - bash: |
        set -x
        if [ -n "$(System.PullRequest.SourceCommitId)" ]; then
          git checkout $(System.PullRequest.SourceCommitId)
        fi
      displayName: 'Checkout pull request HEAD'

    - bash: |
        set -x
        sudo pip3 install ninja numpy==1.20.3 typing-extensions
        sudo python3 -m pip install --upgrade setuptools
        sudo python3 -m pip install scikit-ci-addons
        sudo python3 -m pip install lxml dask distributed
      displayName: 'Install dependencies'

    - bash: |
        set -x

        git clone -b dashboard --single-branch https://github.com/InsightSoftwareConsortium/ITK.git ITK-dashboard

        curl -L https://github.com/InsightSoftwareConsortium/ITK/releases/download/v$(ExternalDataVersion)/InsightData-$(ExternalDataVersion).tar.gz -O
        cmake -E tar xfz InsightData-$(ExternalDataVersion).tar.gz
        cmake -E rename InsightToolkit-$(ExternalDataVersion)/.ExternalData/MD5 $(Build.SourcesDirectory)/.ExternalData/MD5
      workingDirectory: $(Agent.BuildDirectory)
      displayName: 'Download dashboard script and testing data'

    - bash: |
        python_executable=`which python3`
        python_root_dir=$(dirname $(dirname ${python_executable}))
        cat > dashboard.cmake << EOF
        set(CTEST_BUILD_CONFIGURATION "Release")
        set(CTEST_CMAKE_GENERATOR "Ninja")
        set(BUILD_NAME_SUFFIX "-Python")
        set(dashboard_cache "
          Python3_ROOT_DIR:PATH=${python_root_dir}
          Python3_EXECUTABLE:FILEPATH=${python_executable}
          DISABLE_MODULE_TESTS:BOOL=ON
          BUILD_SHARED_LIBS:BOOL=OFF
          BUILD_EXAMPLES:BOOL=OFF
          ITK_WRAP_PYTHON:BOOL=ON
        ")
        include(\$ENV{AGENT_BUILDDIRECTORY}/ITK-dashboard/azure_dashboard.cmake)
        EOF
        cat dashboard.cmake
      workingDirectory: $(Agent.BuildDirectory)/ITK-dashboard
      displayName: 'Configure CTest script'

    - bash: |
        set -x

        c++ --version
        cmake --version

        ctest -S $(Agent.BuildDirectory)/ITK-dashboard/dashboard.cmake -VV -j 4
      displayName: 'Build and test'
      env:
        CTEST_OUTPUT_ON_FAILURE: 1

    - script: |
        ci_addons ctest_junit_formatter $(Build.SourcesDirectory)-build > $(Agent.BuildDirectory)/JUnitTestResults.xml
      condition: succeededOrFailed()
      displayName: 'Format CTest output in JUnit format'

    - task: PublishTestResults@2
      inputs:
        testResultsFiles: "$(Agent.BuildDirectory)/JUnitTestResults.xml"
        testRunTitle: 'CTest $(Agent.OS)'
      condition: succeededOrFailed()
      displayName: 'Publish test results'
