trigger:
- main
- dev
- feature/*
- hotfix/*
- bugfix/*
- release/*

pr:
  autoCancel: true
  drafts: false
parameters:
  # https://stackoverflow.com/a/70046417
  - name: matrix
    type: object
    default:
    # - job_name: 'UbuntuPython38'
    #   py: '3.8'
    #   img: 'ubuntu-latest'
    #   every_time: false
    #   publish_coverage: false
    - job_name: 'UbuntuPython311'
      py: '3.11'
      img: 'ubuntu-latest'
      every_time: true
      publish_coverage: true
    # - job_name: 'WindowsPython38'
    #   py: '3.8'
    #   img: 'windows-latest'
    #   every_time: false
    #   publish_coverage: false
    - job_name: 'WindowsPython311'
      py: '3.11'
      img: 'windows-latest'
      every_time: false
      publish_coverage: false
    # - job_name: 'MacOSPython38'
    #   py: '3.8'
    #   img: 'macOS-latest'
    #   every_time: false
    #   publish_coverage: false
    - job_name: 'MacOSPython311'
      py: '3.11'
      img: 'macOS-latest'
      every_time: false
      publish_coverage: false

stages:
  - stage: Test
    jobs:
    - ${{ each item in parameters.matrix }}:
      - job: ${{ item.job_name }}
        timeoutInMinutes: 90
        cancelTimeoutInMinutes: 1
        pool:
          vmImage: ${{ item.img }}
        condition: and(succeeded(), or( eq(variables['Build.Reason'], 'PullRequest'), ${{ item.every_time }} ))

        steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: ${{ item.py }}
            displayName: 'Use Python ${{ item.py }}'

          - script: |
              python -m pip install pre-commit
              pre-commit install
              pre-commit run --all-files
            displayName: 'Run pre-commits'

          - script: |
              python -m pip install --upgrade pip==23.0.1
              pip install wheel==0.38.4 --upgrade
              pip install setuptools==66 --upgrade
              pip install build==0.10.0
              pip install pytest-azurepipelines
            displayName: 'Install build dependencies'

          - script: |
              python -m build
            displayName: 'Build PrimAITE'

          - script: |
              PRIMAITE_WHEEL=$(ls ./dist/primaite*.whl)
              python -m pip install $PRIMAITE_WHEEL[dev,rl]
            displayName: 'Install PrimAITE'
            condition: or(eq( variables['Agent.OS'], 'Linux' ), eq( variables['Agent.OS'], 'Darwin' ))

          - script: |
              forfiles /p dist\ /m *.whl /c "cmd /c python -m pip install @file[dev,rl]"
            displayName: 'Install PrimAITE'
            condition: eq( variables['Agent.OS'], 'Windows_NT' )

          - script: |
              primaite setup
            displayName: 'Perform PrimAITE Setup'

          - task: UseDotNet@2
            displayName: 'Install dotnet dependencies'
            inputs:
              packageType: 'sdk'
              version: '2.1.x'

          - script: |
              python run_test_and_coverage.py
            displayName: 'Run tests and code coverage'

          # Run the notebooks
          - script: |
              pytest --nbmake -n=auto src/primaite/notebooks --junit-xml=./notebook-tests/notebooks.xml
              notebooks_exit_code=$?
              pytest --nbmake -n=auto src/primaite/simulator/_package_data --junit-xml=./notebook-tests/package-notebooks.xml
              package_notebooks_exit_code=$?
              # Fail step if either of these do not have exit code 0
              if [ $notebooks_exit_code -ne 0 ] || [ $package_notebooks_exit_code -ne 0 ]; then
                exit 1
              fi
            displayName: 'Run notebooks on Linux and macOS'
            condition: or(eq(variables['Agent.OS'], 'Linux'), eq(variables['Agent.OS'], 'Darwin'))

          # Run notebooks
          - script: |
              pytest --nbmake -n=auto src/primaite/notebooks --junit-xml=./notebook-tests/notebooks.xml
              set notebooks_exit_code=%ERRORLEVEL%
              pytest --nbmake -n=auto src/primaite/simulator/_package_data --junit-xml=./notebook-tests/package-notebooks.xml
              set package_notebooks_exit_code=%ERRORLEVEL%
              rem Fail step if either of these do not have exit code 0
              if %notebooks_exit_code% NEQ 0 exit /b 1
              if %package_notebooks_exit_code% NEQ 0 exit /b 1
            displayName: 'Run notebooks on Windows'
            condition: eq(variables['Agent.OS'], 'Windows_NT')

          - task: PublishTestResults@2
            condition: succeededOrFailed()
            displayName: 'Publish Test Results'
            inputs:
              testRunner: JUnit
              testResultsFiles: |
                'junit/**.xml'
                'notebook-tests/**.xml'
              testRunTitle: 'Publish test results'
              failTaskOnFailedTests: true

          - publish: $(System.DefaultWorkingDirectory)/htmlcov/
            # publish the html report - so we can debug the coverage if needed
            condition: ${{ item.publish_coverage }} # should only be run once
            artifact: coverage_report

          - task: PublishCodeCoverageResults@2
            # publish the code coverage so it can be viewed in the run coverage page
            condition: ${{ item.publish_coverage }} # should only be run once
            inputs:
              codeCoverageTool: Cobertura
              summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
