name: Run Tests

on:
  push:
    branches:
      - master
    paths:
      - '**.go'
      - 'go.*'
      - '_fixture/**'
      - '.github/**'
      - 'codecov.yml'
  pull_request:
    branches:
      - master
    paths:
      - '**.go'
      - 'go.*'
      - '_fixture/**'
      - '.github/**'
      - 'codecov.yml'
  workflow_dispatch:

env:
  # run static analysis only with the latest Go version
  LATEST_GO_VERSION: "1.21"

jobs:
  test:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        # Each major Go release is supported until there are two newer major releases. https://golang.org/doc/devel/release.html#policy
        # Echo tests with last four major releases
        go: ["1.18","1.19","1.20","1.21"]
    name: ${{ matrix.os }} @ Go ${{ matrix.go }}
    runs-on: ${{ matrix.os }}
    steps:
      - name: Set up Go ${{ matrix.go }}
        uses: actions/setup-go@v4
        with:
          go-version: ${{ matrix.go }}

      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - name: Run Tests
        run: |
          go test -race --coverprofile=coverage.coverprofile --covermode=atomic ./...

      - name: Upload coverage to Codecov
        if: success() && matrix.go == env.LATEST_GO_VERSION && matrix.os == 'ubuntu-latest'
        uses: codecov/codecov-action@v3
        with:
          fail_ci_if_error: false
