name: CI

on:
  push:
    branches:
    - main
  pull_request:

permissions:
  contents: read

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        go: [1.20.x, 1.19.x, 1.18.x]
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    - name: Setup Go
      uses: actions/setup-go@v4
      with:
        go-version: ${{ matrix.go }}
    - name: Test
      run: make test
    - name: Test with GOARCH=386
      run: env GOARCH=386 go test -v ./...
      if: matrix.os != 'macos-latest'
    - name: Lint
      run: make lint
      if: matrix.go >= '1.19.x'
    - name: Check tools
      run: make check-tools
    - name: Check go generate
      run: go generate && ! git diff | grep ^
      shell: bash
    - name: Check command examples in README.md
      run: |
        ./gojq -Rnr 'reduce inputs as $x (
          {};
          if $x|test("^ [$] .*gojq|^```")
          then
            if .command
            then .results += [{command: .command, output: .output}] | del(.command,.output)
            end | if $x|test("gojq") then .command = $x[3:] end
          elif .command then .output += ($x + "\n" | sub(" +#.*"; ""))
          end
        ) | .results[] |
          "if got=$(diff <(printf %s \(.output | @sh)) \\
            <(\(.command | gsub("gojq"; "./gojq")) 2>&1)); then
            echo ok: \(.command | @sh)
          else
            echo ng: \(.command | @sh); echo \"$got\"; exit 1
          fi"
        ' README.md | bash
      shell: bash
