name: Multi-OS Unit and Integration Tests

on:
  workflow_dispatch: # manually
    inputs:
      go-version:
        description: The Go version to use
        required: true
        type: string
      runs-on:
        description: The OS to run the tests on
        required: true
        type: string
  workflow_call:
    inputs:
      go-version:
        required: true
        type: string
      runs-on:
        required: true
        type: string

env:
  DD_APPSEC_WAF_TIMEOUT: 1m # Increase time WAF time budget to reduce CI flakiness
  GOEXPERIMENT: synctest # TODO: remove once go1.25 is the minimum supported version

permissions:
  contents: read

jobs:
  test-multi-os:
    runs-on: "${{ inputs.runs-on }}"
    env:
      REPORT: gotestsum-report.xml # path to where test results will be saved
      DD_APPSEC_WAF_TIMEOUT: 1h
    steps:
      - name: Normalize github.workspace for Windows
        if: ${{ runner.os == 'Windows' }}
        shell: pwsh
        run: |
          $normalizedPath = "${{ github.workspace }}" -replace '\\', '/'
          "normalized_workspace=$normalizedPath" >> $env:GITHUB_ENV
      - name: Normalize github.workspace for non-Windows OSes
        if: ${{ runner.os != 'Windows' }}
        shell: pwsh
        run: |
          "normalized_workspace=${{ github.workspace }}" >> $env:GITHUB_ENV
      - name: Restore repo cache
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: .git
          key: gitdb-${{ github.repository_id }}-${{ github.sha }}
      - name: Checkout
        if: always()
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
        with:
          ref: ${{ github.sha }}
          clean: false
      - name: Setup Go and development tools
        uses: ./.github/actions/setup-go
        with:
          go-version: ${{ inputs.go-version }}
          tools-dir: ${{ env.normalized_workspace }}/_tools
          tools-bin: ${{ env.normalized_workspace }}/bin
      - name: Mac OS Coreutils
        if: inputs.runs-on == 'macos-latest'
        run: brew install coreutils
      - name: "Runner: Test Core and Contrib (No Integration Tests)"
        shell: bash
        run: |
          export PATH="${{ github.workspace }}/bin:${PATH}"
          go list ./... | grep -v -e google.golang.org/api -e sarama -e confluent-kafka-go -e cmemprof | sort >packages.txt
          # shellcheck disable=SC2046
          gotestsum --junitfile "${REPORT}" -- $(cat packages.txt) -v -coverprofile=coverage.txt -covermode=atomic -timeout 15m
      - name: Upload the results to Datadog CI App
        if: always()
        continue-on-error: true
        uses: ./.github/actions/dd-ci-upload
        with:
          dd-api-key: ${{ secrets.DD_CI_API_KEY }}
          path: ./
          tags: go:${{ inputs.go-version }},arch:${{ runner.arch }},os:${{ runner.os }}

      - name: Upload Coverage
        if: always()
        shell: bash
        run: bash <(curl -s https://codecov.io/bash) -t ${{ secrets.CODECOV_TOKEN }}
