name: test-macos

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  macos:
    strategy:
      matrix:
        os: [macos-14]
        pg: [18, 17, 16, 15, 14]
        config: [Release, Debug]

    runs-on: ${{ matrix.os }}

    name: ${{ matrix.os }} 🐘${{ matrix.pg }} (${{ matrix.config }})

    steps:
      - uses: actions/checkout@v4

      - name: Setup pg_validate_extupgrade
        uses: baptiste0928/cargo-install@v3
        with:
          crate: pg_validate_extupgrade
          git: https://github.com/rjuju/pg_validate_extupgrade

      - name: Setup PostgreSQL
        run: |
          brew install postgresql@${{ matrix.pg }}
          brew unlink postgresql@${{ matrix.pg }}
          brew link --overwrite postgresql@${{ matrix.pg }}
          brew services run postgresql@${{ matrix.pg }}

      - name: Setup PostGIS
        run: brew install postgis
        if: ${{ matrix.pg == 18 || matrix.pg == 17 || matrix.pg == 14 }}

      - name: Fix gettext linkage for PostgreSQL >= 15
        run: brew unlink gettext && brew link --overwrite --force gettext
        if: ${{ matrix.pg >= 15 }}

      - name: Export gettext build flags for PostgreSQL >= 15
        if: ${{ matrix.pg >= 15 }}
        run: |
          GETTEXT_PREFIX="$(brew --prefix gettext)"
          echo "CPPFLAGS=-I${GETTEXT_PREFIX}/include" >> "$GITHUB_ENV"
          echo "CFLAGS=-I${GETTEXT_PREFIX}/include" >> "$GITHUB_ENV"
          echo "LDFLAGS=-L${GETTEXT_PREFIX}/lib" >> "$GITHUB_ENV"

      - name: Generate
        run: cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.config }}

      - name: Build
        run: cmake --build build --config ${{ matrix.config }}

      - name: Install
        run: sudo cmake --install build --component h3-pg --config ${{ matrix.config }}

      - name: Create test database (for pg_validate_extupgrade)
        run: createdb runner

      - name: Test
        run: ctest --test-dir build --output-on-failure --build-config ${{ matrix.config }}

      - name: Print regression diffs
        if: ${{ failure() }}
        run: cat build/*/test/regression.diffs || true
