name: CI tests

on:
  push:
    branches:
    - '*'
    - '!rc/*'
    paths-ignore:
    - 'doc/*.md'
    - 'MANUAL.txt'
    - '*.md'
    - 'RELEASE_CHECKLIST'
    - 'BUGS'
    - 'README.template'
    - 'hie.yaml'
    - '*.nix'
    - 'tools/**'
    - 'linux/**'
    - 'macos/**'
    - 'windows/**'
    - 'man/**'
  pull_request:
    paths-ignore:
    - 'doc/*.md'
    - 'MANUAL.txt'
    - '*.md'
    - 'RELEASE_CHECKLIST'
    - 'BUGS'
    - 'README.template'
    - 'hie.yaml'
    - '*.nix'
    - 'tools/**'
    - 'linux/**'
    - 'macos/**'
    - 'windows/**'
    - 'man/**'

permissions:
  contents: read

jobs:
  linux:

    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        versions:
          - ghc: '8.6.5'
            cabal: 'latest'
            cabalopts: '-f-embed_data_files'
          - ghc: '8.8.4'
            cabal: 'latest'
            cabalopts: '-f-lua -f-server --enable-benchmarks'
          - ghc: '8.10.7'
            cabal: 'latest'
            cabalopts: ''
          - ghc: '9.0.2'
            cabal: 'latest'
            cabalopts: ''
          - ghc: '9.2.5'
            cabal: 'latest'
            cabalopts: ''
          - ghc: '9.4.4'
            cabal: 'latest'
            cabalopts: ''
          - ghc: '9.4.4'
            cabal: 'latest'
            cabalopts: '--allow-newer --constrain "mtl >= 2.3.1"'
    steps:
    - uses: actions/checkout@v3

    # needed by memory
    - name: Install numa
      run: sudo apt-get install libnuma-dev

    - name: Install cabal/ghc
      run: |
        ghcup install ghc --set ${{ matrix.versions.ghc }}
        ghcup install cabal --set ${{ matrix.versions.cabal }}

    # declare/restore cached things

    - name: Cache cabal global package db
      id:   cabal-global
      uses: actions/cache@v3
      with:
        path: |
          ~/.cabal
        key: ${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-global-${{ secrets.CACHE_VERSION }}

    - name: Cache cabal work
      id:   cabal-local
      uses: actions/cache@v3
      with:
        path: |
          dist-newstyle
        key: ${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-local-${{ secrets.CACHE_VERSION }}

    - name: Update cabal
      run: |
          cabal update

    - name: Build and test
      run: |
          cabal test ${{ matrix.versions.cabalopts }} --enable-tests --disable-optimization --ghc-options=-Werror all

  windows:

    runs-on: windows-2019

    steps:
    - uses: actions/checkout@v3

    # declare/restore cached things

    - name: Cache stack global package db
      id:   stack-global-package-db
      uses: actions/cache@v3
      with:
        path: |
          C:\Users\runneradmin\AppData\Roaming\stack\
        key: ${{ runner.os }}-appdata-roaming-stack-${{ hashFiles('stack.yaml') }}

    # stack's local package dbs for the project and each package
    - name: Cache .stack-work
      uses: actions/cache@v3
      with:
        path: |
          .stack-work
        key: ${{ runner.os }}-stack-work-${{ hashFiles('stack.yaml') }}-${{ secrets.CACHE_VERSION }}
        restore-keys: ${{ runner.os }}-stack-work-${{ secrets.CACHE_VERSION }}

    - name: Install dependencies
      run: |
          stack update
          stack install happy
          stack test --dependencies-only --fast
    - name: Build and test
      shell: cmd
      run: |
          stack test --fast --test-arguments="--hide-successes --ansi-tricks=false"

# We no longer run the macos tests, to make CI faster.
# macos:

#   runs-on: macos-11
#   strategy:
#     fail-fast: true
#     matrix:
#       versions:
#         - ghc: '8.8.4'
#           cabal: '3.2'

#   steps:
#   - uses: actions/checkout@v3

#   - name: Install cabal/ghc
#     run: |
#       ghcup install ghc --set ${{ matrix.versions.ghc }}
#       ghcup install cabal ${{ matrix.versions.cabal }}

#   # declare/restore cached things

#   - name: Cache cabal global package db
#     id:   cabal-global
#     uses: actions/cache@v3
#     with:
#       path: |
#         ~/.cabal
#       key: ${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-global-${{ secrets.CACHE_VERSION }}

#   - name: Cache cabal work
#     id:   cabal-local
#     uses: actions/cache@v3
#     with:
#       path: |
#         dist-newstyle
#       key: ${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-local-${{ secrets.CACHE_VERSION }}

#   - name: Install dependencies
#     run: |
#         cabal v2-update
#         cabal v2-build --dependencies-only --enable-tests --disable-optimization
#   - name: Build and test
#     run: |
#         cabal v2-build --enable-tests --disable-optimization 2>&1 | tee build.log
#         # fail if warnings in local build
#         ! grep -q ": *[Ww]arning:" build.log || exit 1
#         cabal v2-test --disable-optimization --test-option=--hide-successes --test-option=--ansi-tricks=false
