on:
  push:
    branches:
      - master
      - main
  pull_request:
    # Only run if there are changes to Go code or related configurations
    paths:
      - "**.go"
      - "go.mod"
      - "go.sum"
      - "codecov.yml"

name: Tests
jobs:
  test-cache:
    name: "Run unit tests"
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: [1.18.x, 1.x]
    steps:
      - uses: actions/checkout@v4

      - name: Setup Go ${{ matrix.go-version }}
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}

      - name: Test with race and coverage
        run: |
          go test -race -coverprofile=coverage.out -covermode=atomic
          go tool cover -func=coverage.out

      - uses: codecov/codecov-action@v4
        with:
          files: ./coverage.out
          token: ${{ secrets.CODECOV_TOKEN }} # required

  lint:
    name: "Run static analysis"
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v4

      - name: Setup Go ${{ matrix.go-version }}
        uses: actions/setup-go@v5
        with:
          go-version: "1.x"

      - run: "go vet ./..."

      - name: Staticcheck
        uses: dominikh/staticcheck-action@v1.3.0
        with:
          install-go: false
