name: "Go: Run Tests"
on:
  push:
    paths:
      - "go/**"
      - .github/workflows/go-tests.yml
      - .github/actions/**
      - codeql-workspace.yml
    branches:
      - main
      - "rc/*"
  pull_request:
    paths:
      - "go/**"
      - .github/workflows/go-tests.yml
      - .github/actions/**
      - codeql-workspace.yml

env:
  GO_VERSION: '~1.22.0'

permissions:
  contents: read

jobs:
  test-linux:
    if: github.repository_owner == 'github'
    name: Test Linux (Ubuntu)
    runs-on: ubuntu-latest-xl
    steps:
      - name: Set up Go ${{ env.GO_VERSION }}
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}
          cache: false
        id: go

      - name: Check out code
        uses: actions/checkout@v4

      - name: Set up CodeQL CLI
        uses: ./.github/actions/fetch-codeql

      - name: Enable problem matchers in repository
        shell: bash
        run: 'find .github/problem-matchers -name \*.json -exec echo "::add-matcher::{}" \;'

      - name: Build
        run: |
          cd go
          make

      - name: Check that all Go code is autoformatted
        run: |
          cd go
          make check-formatting

      - name: Compile qhelp files to markdown
        run: |
          cd go
          env QHELP_OUT_DIR=qhelp-out make qhelp-to-markdown

      - name: Upload qhelp markdown
        uses: actions/upload-artifact@v3
        with:
          name: qhelp-markdown
          path: go/qhelp-out/**/*.md

      - name: Cache compilation cache
        id: query-cache
        uses: ./.github/actions/cache-query-compilation
        with:
          key: go-qltest

      - name: Test
        run: |
          cd go
          make test cache="${{ steps.query-cache.outputs.cache-dir }}"
