parameters:
  name: 'RunTestsInHelix'
  dependsOn: ''
  condition: ''
  testSuite: ''
  # If a Pipeline runs this template more than once, this parameter should be unique per build flavor to differentiate the
  # the different test runs:
  helixType: 'test/devtest'
  artifactName: 'drop'
  maxParallel: 4
  rerunPassesRequiredToAvoidFailure: 5
  taefQuery: ''
  configuration: ''
  platform: ''
  # if 'useBuildOutputFromBuildId' is set, we will default to using a build from this pipeline:
  useBuildOutputFromPipeline: $(System.DefinitionId)
  openHelixTargetQueues: 'windows.10.amd64.client19h1.open.xaml'
  closedHelixTargetQueues: 'windows.10.amd64.client19h1.xaml'

jobs:
- job: ${{ parameters.name }}
  dependsOn: ${{ parameters.dependsOn }}
  condition: ${{ parameters.condition }}
  pool:
    vmImage: 'windows-2019'
  timeoutInMinutes: 120
  strategy:
    maxParallel: ${{ parameters.maxParallel }}
  variables:
    buildConfiguration: ${{ parameters.configuration }}
    buildPlatform: ${{ parameters.platform }}
    openHelixTargetQueues: ${{ parameters.openHelixTargetQueues }}
    closedHelixTargetQueues: ${{ parameters.closedHelixTargetQueues }}
    artifactsDir: $(Build.SourcesDirectory)\Artifacts
    taefPath: $(Build.SourcesDirectory)\build\Helix\packages\Microsoft.Taef.10.60.210621002\build\Binaries\$(buildPlatform)
    helixCommonArgs: '/binaryLogger:$(Build.SourcesDirectory)/${{parameters.name}}.$(buildPlatform).$(buildConfiguration).binlog /p:HelixBuild=$(Build.BuildId).$(buildPlatform).$(buildConfiguration) /p:Platform=$(buildPlatform) /p:Configuration=$(buildConfiguration) /p:HelixType=${{parameters.helixType}} /p:TestSuite=${{parameters.testSuite}} /p:ProjFilesPath=$(Build.ArtifactStagingDirectory) /p:rerunPassesRequiredToAvoidFailure=${{parameters.rerunPassesRequiredToAvoidFailure}}'

  steps:
  - task: CmdLine@1
    displayName: 'Display build machine environment variables'
    inputs:
      filename: 'set'

  - task: NuGetToolInstaller@0
    displayName: 'Use NuGet 5.2.0'
    inputs:
      versionSpec: 5.2.0

  - task: 333b11bd-d341-40d9-afcf-b32d5ce6f23b@2
    displayName: 'NuGet restore build/Helix/packages.config'
    inputs:
      restoreSolution: build/Helix/packages.config
      feedsToUse: config
      nugetConfigPath: nuget.config
      restoreDirectory: packages

  - task: DownloadBuildArtifacts@0
    condition:
      and(succeeded(),eq(variables['useBuildOutputFromBuildId'],''))
    inputs:
      artifactName: ${{ parameters.artifactName }}
      downloadPath: '$(artifactsDir)'

  - task: DownloadBuildArtifacts@0
    condition:
      and(succeeded(),ne(variables['useBuildOutputFromBuildId'],''))
    inputs:
      buildType: specific
      buildVersionToDownload: specific
      project: $(System.TeamProjectId)
      pipeline: ${{ parameters.useBuildOutputFromPipeline }}
      buildId: $(useBuildOutputFromBuildId)
      artifactName: ${{ parameters.artifactName }}
      downloadPath: '$(artifactsDir)'

  - task: CmdLine@1
    displayName: 'Display Artifact Directory payload contents'
    inputs:
      filename: 'dir'
      arguments: '/s $(artifactsDir)'

  - task: powershell@2
    displayName: 'PrepareHelixPayload.ps1'
    inputs:
      targetType: filePath
      filePath: build\Helix\PrepareHelixPayload.ps1
      arguments: -Platform '$(buildPlatform)' -Configuration '$(buildConfiguration)' -ArtifactName '${{ parameters.artifactName }}'

  - task: CmdLine@1
    displayName: 'Display Helix payload contents'
    inputs:
      filename: 'dir'
      arguments: '/s $(Build.SourcesDirectory)\HelixPayload'

  - task: PowerShell@2
    displayName: 'Make artifact directories'
    inputs:
      targetType: inline
      script: |
        New-Item -ItemType Directory -Force -Path "$(Build.ArtifactStagingDirectory)\$(BuildConfiguration)\"
        New-Item -ItemType Directory -Force -Path "$(Build.ArtifactStagingDirectory)\$(BuildConfiguration)\$(BuildPlatform)\"

  - template: helix-createprojfile-steps.yml
    parameters:
      condition: and(succeeded(),eq('${{ parameters.testSuite }}','DevTestSuite'))
      testFilePath: '$(artifactsDir)\${{ parameters.artifactName }}\$(buildConfiguration)\$(buildPlatform)\Test\TerminalApp.LocalTests.dll'
      outputProjFileName: 'RunTestsInHelix-TerminalAppLocalTests.proj'
      testSuite: '${{ parameters.testSuite }}'
      taefQuery: ${{ parameters.taefQuery }}

  - template: helix-createprojfile-steps.yml
    parameters:
      condition: and(succeeded(),eq('${{ parameters.testSuite }}','DevTestSuite'))
      testFilePath: '$(artifactsDir)\${{ parameters.artifactName }}\$(buildConfiguration)\$(buildPlatform)\Test\SettingsModel.LocalTests.dll'
      outputProjFileName: 'RunTestsInHelix-SettingsModelLocalTests.proj'
      testSuite: '${{ parameters.testSuite }}'
      taefQuery: ${{ parameters.taefQuery }}


  - template: helix-createprojfile-steps.yml
    parameters:
      condition: and(succeeded(),eq('${{ parameters.testSuite }}','DevTestSuite'))
      testFilePath: '$(artifactsDir)\${{ parameters.artifactName }}\$(buildConfiguration)\$(buildPlatform)\Test\Conhost.UIA.Tests.dll'
      outputProjFileName: 'RunTestsInHelix-HostTestsUIA.proj'
      testSuite: '${{ parameters.testSuite }}'
      taefQuery: ${{ parameters.taefQuery }}

  - template: helix-createprojfile-steps.yml
    parameters:
      condition: and(succeeded(),or(eq('${{ parameters.testSuite }}','PgoInstrumentationSuite'),eq('${{ parameters.testSuite }}','DevTestSuite')))
      testFilePath: '$(artifactsDir)\${{ parameters.artifactName }}\$(buildConfiguration)\$(buildPlatform)\Test\WindowsTerminal.UIA.Tests.dll'
      outputProjFileName: 'RunTestsInHelix-WindowsTerminalUIATests.proj'
      testSuite: '${{ parameters.testSuite }}'
      taefQuery: ${{ parameters.taefQuery }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish generated .proj files'
    inputs:
      PathtoPublish: $(Build.ArtifactStagingDirectory)
      artifactName: ${{ parameters.artifactName }}

  - task: DotNetCoreCLI@2
    displayName: 'Run tests in Helix (open queues)'
    condition: and(succeeded(),eq(variables['System.CollectionUri'],'https://dev.azure.com/ms/'))
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    inputs:
      command: custom
      projects: build\Helix\RunTestsInHelix.proj
      custom: msbuild
      arguments: '$(helixCommonArgs) /p:IsExternal=true /p:Creator=Terminal /p:HelixTargetQueues=$(openHelixTargetQueues)'

  - task: DotNetCoreCLI@2
    displayName: 'Run tests in Helix (closed queues)'
    condition: and(succeeded(),ne(variables['System.CollectionUri'],'https://dev.azure.com/ms/'))
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
      HelixAccessToken: $(HelixApiAccessToken)
    inputs:
      command: custom
      projects: build\Helix\RunTestsInHelix.proj
      custom: msbuild
      arguments: '$(helixCommonArgs) /p:HelixTargetQueues=$(closedHelixTargetQueues)'
