pool:
   vmImage: 'ubuntu-latest'

trigger: none
variables:
  TESTPASSWORD: $(SQLPASSWORD)

steps: 
- task: GoTool@0
  inputs:
    version: '1.22.10'

- task: Go@0
  displayName: 'Go: install gotest.tools/gotestsum'
  inputs:
    command: 'custom'
    customCommand: 'install'
    arguments: 'gotest.tools/gotestsum@latest'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: Go@0
  displayName: 'Go: install github.com/axw/gocov/gocov'
  inputs:
    command: 'custom'
    customCommand: 'install'
    arguments: 'github.com/axw/gocov/gocov@latest'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: Go@0
  displayName: 'Go: install github.com/AlekSi/gocov-xml'
  inputs:
    command: 'custom'
    customCommand: 'install'
    arguments: 'github.com/AlekSi/gocov-xml@latest'
    workingDirectory: '$(System.DefaultWorkingDirectory)'

- task: AzureCLI@2
  inputs:
    addSpnToEnvironment: true
    azureSubscription: $(AZURESUBSCRIPTION_SERVICE_CONNECTION_NAME)
    scriptType: pscore
    scriptLocation: inlineScript
    inlineScript: |
      Write-Host "##vso[task.setvariable variable=AZURESUBSCRIPTION_CLIENT_ID;]$env:AZURESUBSCRIPTION_CLIENT_ID"
      Write-Host "##vso[task.setvariable variable=AZURESUBSCRIPTION_TENANT_ID;]$env:AZURESUBSCRIPTION_TENANT_ID"
      Write-Host "##vso[task.setvariable variable=AZURESUBSCRIPTION_SERVICE_CONNECTION_ID;]$env:AZURESUBSCRIPTION_SERVICE_CONNECTION_ID"
      gci env:* | sort-object name

- task: Docker@2
  displayName: 'Run SQL 2022 docker image'
  inputs:
    command: run
    arguments: '-m 2GB -e ACCEPT_EULA=1 -d --name sql2022 -p:1433:1433 -e SA_PASSWORD=$(TESTPASSWORD) mcr.microsoft.com/mssql/server:2022-latest'

- script: |
    ~/go/bin/gotestsum --junitfile testresults.xml -- ./... -coverprofile=coverage.txt -covermode count 
    ~/go/bin/gocov convert coverage.txt > coverage.json
    ~/go/bin/gocov-xml < coverage.json > coverage.xml
  workingDirectory: '$(Build.SourcesDirectory)'
  displayName: 'run tests'
  env:
# skipping Azure related tests due to lack of access
    SQLPASSWORD: $(SQLPASSWORD)
    SQLSERVER_DSN: $(SQLSERVER_DSN)
    AZURESERVER_DSN: $(AZURESERVER_DSN)
    AZURESUBSCRIPTION_SERVICE_CONNECTION_ID: $(AZURESUBSCRIPTION_SERVICE_CONNECTION_ID)
    AZURESUBSCRIPTION_CLIENT_ID: $(AZURESUBSCRIPTION_CLIENT_ID)
    AZURESUBSCRIPTION_TENANT_ID: $(AZURESUBSCRIPTION_TENANT_ID)
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    KEY_VAULT_NAME: $(KEY_VAULT_NAME)
  continueOnError: true
- task: PublishTestResults@2
  displayName: "Publish junit-style results"
  inputs:
    testResultsFiles: 'testresults.xml'
    testResultsFormat: JUnit
    searchFolder: '$(Build.SourcesDirectory)'
    testRunTitle: 'SQL 2022 - $(Build.SourceBranchName)'
    failTaskOnFailedTests: true
  condition: always()
  
- task: PublishCodeCoverageResults@2
  inputs:
    pathToSources: '$(Build.SourcesDirectory)'
    summaryFileLocation: $(Build.SourcesDirectory)/**/coverage.xml
    failIfCoverageEmpty: true
  condition: always()
  continueOnError: true
  
