parameters:
  AdditionalTestArgs: ''
  TestMarkArgument: ''
  EnvVars: {}
  ServiceDirectory: ''
  CloudName: ''
  PythonVersion: ''
  OSVmImage: ''
  BeforeTestSteps: []
  AfterTestSteps: []
  CoverageArg: ''
  ToxTestEnv: ""
  RunCoverage: ne(variables['CoverageArg'], '--disablecov')
  ToxEnvParallel: ''
  InjectedPackages: ''
  DevFeedName: 'public/azure-sdk-for-python'
  TestProxy: false

# The variable TargetingString is set by template `eng/pipelines/templates/steps/targeting-string-resolve.yml`. This template is invoked from yml files:
#     eng/pipelines/templates/jobs/ci.tests.yml
#     eng/pipelines/templates/jobs/ci.yml
#     eng/pipelines/templates/jobs/live.test.yml

# Please use `$(TargetingString)` to refer to the python packages glob string. This was previously `${{ parameters.BuildTargetingString }}`.
steps:
  - pwsh: |
      gci -r $(Build.ArtifactStagingDirectory)
    displayName: Dump Artifact Directory

  - template: /eng/pipelines/templates/steps/use-python-version.yml
    parameters:
      versionSpec: '${{ parameters.PythonVersion }}'

  - script: |
      python -m pip install pip==20.3.3
      python -m pip install wheel==0.37.0
      pip install -r eng/ci_tools.txt
      pip --version
      pip freeze
    displayName: 'Prep Environment'

  - ${{if eq(parameters.TestProxy, true) }}:
    - template: /eng/common/testproxy/test-proxy-tool.yml
      parameters:
        runProxy: false

  - ${{if eq(variables['System.TeamProject'], 'internal') }}:
    - template: ../steps/auth-dev-feed.yml
      parameters:
        DevFeedName: ${{ parameters.DevFeedName }}

  - ${{ parameters.BeforeTestSteps }}

  - template: /eng/pipelines/templates/steps/seed-virtualenv-wheels.yml

  - task: PythonScript@0
    displayName: 'Run Tests'
    inputs:
      scriptPath: 'scripts/devops_tasks/setup_execute_tests.py'
      arguments: >-
        "$(TargetingString)"
        ${{ parameters.AdditionalTestArgs }}
        ${{ parameters.CoverageArg }}
        --mark_arg="${{ parameters.TestMarkArgument }}"
        --service="${{ parameters.ServiceDirectory }}"
        --toxenv="${{ parameters.ToxTestEnv }}"
        --injected-packages="${{ parameters.InjectedPackages }}"
        ${{ parameters.ToxEnvParallel }}
    env: ${{ parameters.EnvVars }}

  - ${{if eq(parameters.TestProxy, true) }}:
    - pwsh: |
        $files = Get-ChildItem -Path $(Build.SourcesDirectory) -Filter _proxy_log_*.log
        foreach($file in $files){
            Write-Host "##[group]$file"
            cat $file
            Write-Host "##[endgroup]"
        }
      displayName: Dump Test-Proxy Logs
      condition: succeededOrFailed()

  - ${{ parameters.AfterTestSteps }}

  - template: /eng/common/pipelines/templates/steps/publish-artifact.yml
    parameters:
      ArtifactPath: '$(Build.SourcesDirectory)/_coverage'
      ArtifactName: '$(System.StageName)_$(Agent.JobName)_coverage'
      CustomCondition: '${{ parameters.RunCoverage }}'

  - pwsh: |
      python -m pip install coverage==4.5.4
      python scripts/devops_tasks/create_coverage.py
    displayName: Create Coverage Report
    condition: and(succeeded(), ${{ parameters.RunCoverage }})

  - task: PythonScript@0
    displayName: 'Test Samples'
    condition: and(succeeded(), eq(variables['TestSamples'], 'true'))
    inputs:
      scriptPath: 'scripts/devops_tasks/setup_execute_tests.py'
      arguments: >-
        "$(TargetingString)"
        --service="${{ parameters.ServiceDirectory }}"
        --toxenv="samples"
    env: ${{ parameters.EnvVars }}

  - template: /eng/common/pipelines/templates/steps/publish-artifact.yml
    parameters:
      ArtifactPath: '$(Build.SourcesDirectory)/.logs'
      ArtifactName: '$(System.StageName)_$(Agent.JobName)_logs'

  - template: /eng/common/pipelines/templates/steps/publish-artifact.yml
    parameters:
      ArtifactPath: '$(Build.SourcesDirectory)/_tox_logs'
      ArtifactName: '$(System.StageName)_$(Agent.JobName)_tox_logs'
      CustomCondition: failed()

  - task: PublishTestResults@2
    condition: always()
    inputs:
      testResultsFiles: '**/*test*.xml'
      testRunTitle: '${{ parameters.ServiceDirectory }} ${{ parameters.CloudName }} $(Agent.JobName)'
      failTaskOnFailedTests: true

  - task: PublishCodeCoverageResults@1
    displayName: 'Publish Code Coverage to DevOps'
    continueOnError: true
    condition: and(succeeded(), ${{ parameters.RunCoverage }})
    inputs:
      codeCoverageTool: Cobertura
      summaryFileLocation: '$(Build.SourcesDirectory)/coverage.xml'
