resources:
- repo: self

trigger:
  batch: true
  branches:
    include:
      - '*'

pr:
  branches:
    include:
      - '*'

jobs:
  - job: AutomationTest
    timeoutInMinutes: 20

    pool:
      vmImage: 'ubuntu-16.04'
    strategy:
      matrix:
        Python36:
          python.version: '3.6'
          tox_env: 'py36'
        Python37:
          python.version: '3.7'
          tox_env: 'py37'
        Python38:
          python.version: '3.8'
          tox_env: 'py38'
    steps:
      - task: UsePythonVersion@0
        displayName: 'Use Python $(python.version)'
        inputs:
          versionSpec: '$(python.version)'
      - bash: pip install --upgrade pip tox
        displayName: 'Install pip and tox'
      - bash: |
          set -ev

          tox
        displayName: 'Run Test'
        env:
          TOXENV: $(tox_env)

  - job: BuildPythonWheel
    condition: succeeded()
    pool:
      image: 'ubuntu-16.04'
    steps:
      - task: UsePythonVersion@0
        displayName: Use Python 3.6
        inputs:
          versionSpec: 3.6
      - bash: |
          set -ev

          : "${BUILD_STAGINGDIRECTORY:?BUILD_STAGINGDIRECTORY environment variable not set}"
          : "${BUILD_SOURCESDIRECTORY:=$(cd $(dirname $0); cd ../../; pwd)}"

          cd "${BUILD_SOURCESDIRECTORY}"

          echo "Build knack"
          pip install -U pip setuptools wheel
          python setup.py bdist_wheel -d "${BUILD_STAGINGDIRECTORY}"
          python setup.py sdist -d "${BUILD_STAGINGDIRECTORY}"
        displayName: Build Wheel
      - task: PublishPipelineArtifact@0
        displayName: 'Publish Artifact: pypi'
        inputs:
          TargetPath: $(Build.ArtifactStagingDirectory)
          ArtifactName: pypi
      - task: DownloadPipelineArtifact@1
        displayName: 'Download PyPI Packages'
        inputs:
          TargetPath: '$(Build.ArtifactStagingDirectory)/pypi'
          artifactName: pypi
      - bash: |
          set -ev

          cd $BUILD_ARTIFACTSTAGINGDIRECTORY/pypi
          pwd
          ls -la
        displayName: Test Build Wheel
