name: Tests

on:
  push:
    branches:
    - main
  pull_request:
    branches:
    - main

jobs:
  build-and-test:
    strategy:
      matrix:
        go-version: [1.17, 1.18, 1.19]
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}

    steps:
    - name: Install Go
      uses: actions/setup-go@c4a742cab115ed795e34d4513e2cf7d472deb55f
      with:
        go-version: ${{ matrix.go-version }}

    - name: Check out code
      uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8

    - name: Check formatting
      if: ${{ matrix.go-version == '1.19' && matrix.os == 'ubuntu-latest' }}
      run: diff -u <(echo -n) <(gofmt -d .)

    - name: Check Go modules
      if: ${{ matrix.go-version == '1.19' && matrix.os == 'ubuntu-latest' }}
      run: |
        go mod tidy
        git diff --exit-code

    - name: Build (cross-compile)
      if: matrix.os == 'ubuntu-latest'
      run: |
        GOOS=darwin go build ./...
        GOOS=dragonfly go build ./...
        GOOS=freebsd go build ./...
        GOOS=linux go build ./...
        GOOS=netbsd go build ./...
        GOOS=openbsd go build ./...
        GOOS=solaris go build ./...
        GOOS=windows go build ./...

    - name: Test (native)
      run: go test -v ./...
