parameters:
  jobName: ""
  displayName: ""
  imageName: ""

jobs:
- job: ${{ parameters.jobName }}
  displayName: ${{ parameters.displayName }}
  pool:
    vmImage: ${{ parameters.imageName }}
  steps:
    - checkout: self
      clean: true

    - task: DownloadPipelineArtifact@2
      inputs:
        source: specific
        project: PowerShellCore
        pipeline: '696'
        preferTriggeringPipeline: true
        runVersion: latestFromBranch
        runBranch: '$(Build.SourceBranch)'
        artifact: finalResults
        patterns: '**/*.nupkg'
        path: '$(Pipeline.Workspace)/releasePipeline/finalResults'

    - task: DownloadPipelineArtifact@2
      inputs:
        source: specific
        project: PowerShellCore
        pipeline: '696'
        preferTriggeringPipeline: true
        runVersion: latestFromBranch
        runBranch: '$(Build.SourceBranch)'
        artifact: metadata
        path: '$(Pipeline.Workspace)/releasePipeline/metadata'

    - pwsh: |
        $dotnetMetadataPath = "$(Build.SourcesDirectory)/DotnetRuntimeMetadata.json"
        $dotnetMetadataJson = Get-Content $dotnetMetadataPath -Raw | ConvertFrom-Json

        # Channel is like: $Channel = "5.0.1xx-preview2"
        $Channel = $dotnetMetadataJson.sdk.channel

        $sdkVersion = (Get-Content "$(Build.SourcesDirectory)/global.json" -Raw | ConvertFrom-Json).sdk.version
        Import-Module "$(Build.SourcesDirectory)/build.psm1" -Force

        Find-Dotnet

        if(-not (Get-PackageSource -Name 'dotnet' -ErrorAction SilentlyContinue))
        {
            $nugetFeed = ([xml](Get-Content $(Build.SourcesDirectory)/nuget.config -Raw)).Configuration.packagesources.add | Where-Object { $_.Key -eq 'dotnet' } | Select-Object -ExpandProperty Value
            Register-PackageSource -Name 'dotnet' -Location $nugetFeed -ProviderName NuGet
            Write-Verbose -Message "Register new package source 'dotnet'" -verbose
        }

        ## Install latest version from the channel
        #Install-Dotnet -Channel "$Channel" -Version $sdkVersion

        Start-PSBootstrap

        Write-Verbose -Message "Installing .NET SDK completed." -Verbose

      displayName: Install .NET

    - pwsh: |
        $env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
        Import-Module "$(Build.SourcesDirectory)/build.psm1" -Force
        Start-PSBootstrap -Force

        $localLocation = "$(Pipeline.Workspace)/releasePipeline/finalResults"
        $xmlElement = @"
          <add key=`"local`" value=`"$localLocation`" />
          <add key="dotnet" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v2" />
        </packageSources>
        "@

        $releaseVersion = Get-Content "$(Pipeline.Workspace)/releasePipeline/metadata/release.json" | ConvertFrom-Json | Select-Object -ExpandProperty 'ReleaseVersion'

        Set-Location -Path $(Build.SourcesDirectory)/test/hosting

        Get-ChildItem

        ## register the packages download directory in the nuget file
        $nugetConfigContent = Get-Content ./NuGet.Config -Raw
        $updateNugetContent = $nugetConfigContent.Replace("</packageSources>", $xmlElement)

        $updateNugetContent | Out-File ./NuGet.Config -Encoding ascii

        Get-Content ./NuGet.Config

        dotnet --info
        dotnet restore
        dotnet test /property:RELEASE_VERSION=$releaseVersion --test-adapter-path:. "--logger:xunit;LogFilePath=$(System.DefaultWorkingDirectory)/test-hosting.xml"

      displayName: Restore and execute tests

    - task: PublishTestResults@2
      displayName: 'Publish Test Results **\test-hosting.xml'
      inputs:
        testResultsFormat: XUnit
        testResultsFiles: '**\test-hosting.xml'
