parameters:
- name: componentName
  type: string
  default: ''
- name: os
  type: string
  default: ubuntu2204
- name: testDir
  type: string
  default: build
- name: testExecutable
  type: string
  default: ctest
- name: testParameters
  type: string
  default: --extra-verbose --output-on-failure --force-new-ctest-process --output-junit test_output.xml
- name: extraTestParameters
  type: string
  default: ''
- name: testOutputFile
  type: string
  default: test_output.xml
- name: testOutputFormat
  type: string
  default: JUnit
  values:
    - JUnit
    - NUnit
    - VSTest
    - XUnit
    - CTest
- name: testPublishResults
  type: boolean
  default: true
- name: allowComponentTestFailure
  type: object
  default:
    - amdsmi
    - HIPIFY
    - rocm_smi_lib
    - roctracer
    # the following do not use this template but allow test failures, included for completeness
    - aomp
    - ROCgdb

steps:
# run test, continue on failure to publish results
# and to publish build artifacts
- task: Bash@3
  displayName: '${{ parameters.componentName }} Test'
  continueOnError: ${{ containsValue(parameters.allowComponentTestFailure, parameters.componentName) }}
  inputs:
    targetType: inline
    script: |
      ${{ iif(eq(parameters.os, 'almalinux8'), 'source /opt/rh/gcc-toolset-14/enable', '') }}
      ${{ parameters.testExecutable }} ${{ parameters.testParameters }} ${{ parameters.extraTestParameters }}
    workingDirectory: ${{ parameters.testDir }}
- ${{ if parameters.testPublishResults }}:
  - task: PublishTestResults@2
    displayName: '${{ parameters.componentName }} Publish Results'
    condition: succeededOrFailed()
    inputs:
      searchFolder: ${{ parameters.testDir }}
      testResultsFormat: ${{ parameters.testOutputFormat }}
      testResultsFiles: '**/${{ parameters.testOutputFile }}'
