parameters:
  runIntegrationTests:

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

- task: PowerShell@2
  inputs:
    filePath: .azure-pipelines/scripts/New-RAMDisk.ps1
    arguments: "-Drive R -Size 1GB"
  displayName: Setup RAMDisk

- powershell: |
    mkdir R:\Temp
    $acl = Get-Acl "R:\Temp"
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
        "Everyone", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"
    )
    $acl.AddAccessRule($rule)
    Set-Acl "R:\Temp" $acl
  displayName: Set RAMDisk Permissions

- bash: pip install --upgrade 'virtualenv<20' setuptools tox
  displayName: Install Tox

- script: tox -e py -- -m unit -n auto --junit-xml=junit/unit-test.xml
  env:
    TEMP: "R:\\Temp"
  displayName: Tox run unit tests

- ${{ if eq(parameters.runIntegrationTests, 'true') }}:
  - powershell: |
      # Fix Git SSL errors
      pip install certifi tox
      python -m certifi > cacert.txt
      $env:GIT_SSL_CAINFO = $(Get-Content cacert.txt)

      # Shorten paths to get under MAX_PATH or else integration tests will fail
      # https://bugs.python.org/issue18199
      $env:TEMP = "R:\Temp"

      tox -e py -- -m integration -n auto --durations=5 --junit-xml=junit/integration-test.xml
    displayName: Tox run integration tests

- task: PublishTestResults@2
  displayName: Publish Test Results
  inputs:
    testResultsFiles: junit/*.xml
    testRunTitle: 'Python $(python.version)'
  condition: succeededOrFailed()
