name: cabal-test

on:
  push:
    branches:
    - master
    - ci-*
    - release*
    paths: &trigger_path_list
    - '.github/workflows/cabal-test.yml'
    - 'Agda.cabal'
    - 'Setup.hs'
    - 'src/full/**'
    - 'src/main/**'
    - 'test/**.hs'
  pull_request:
    paths: *trigger_path_list

defaults:
  run:
    shell: bash

jobs:
  auto-cancel:
    if: |
      !contains(github.event.head_commit.message, '[skip ci]')
      && !contains(github.event.head_commit.message, '[ci skip]')
      && !contains(github.event.head_commit.message, '[github skip]')
      && !contains(github.event.head_commit.message, '[skip github]')
    runs-on: Ubuntu-latest # Required, but it can be anything here.

    steps:
    - uses: styfle/cancel-workflow-action@0.6.0
      with:
        access_token: ${{ github.token }}

  cabal:
    needs: auto-cancel

    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          # - windows-latest
          ## Andreas, 2021-08-28, giving up on running the test-suite under Windows.
          ## There is e.g. https://github.com/phile314/tasty-silver/issues/16
          ## that may be responsible that diffs are not shown,
          ## and that prevents me from running the test-suite interactively.
        ghc-ver: [9.2.1]
        cabal-ver: [latest]

    steps:
    - uses: actions/checkout@v2
      with:
        submodules: recursive

    - uses: haskell/actions/setup@v1
      id: setup-haskell
      with:
        ghc-version: ${{ matrix.ghc-ver }}
        cabal-version: ${{ matrix.cabal-ver }}

    - name: Environment settings based on the Haskell setup
      run: |
        export GHC_VER=$(ghc --numeric-version)
        export CABAL_VER=$(cabal --numeric-version)
        echo "GHC_VER   = ${GHC_VER}"
        echo "CABAL_VER = ${CABAL_VER}"
        echo "GHC_VER=${GHC_VER}"       >> ${GITHUB_ENV}
        echo "CABAL_VER=${CABAL_VER}"   >> ${GITHUB_ENV}
      # From now on, use env.{GHC|CABAL}_VER rather than matrix.{ghc|cabal}-ver!

    - name: Configure the build plan
      run: |
        cabal update
        cabal configure -O1 --enable-tests

    - uses: actions/cache@v2
      name: Cache dependencies
      id: cache
      with:
        path: |
          ${{ steps.setup-haskell.outputs.cabal-store }}
        # The file `plan.json` contains the build information.
        key: ${{ runner.os }}-cabal-test-${{ env.GHC_VER }}-${{ env.CABAL_VER }}-${{ hashFiles('**/plan.json') }}

    - name: Install dependencies
      if: ${{ !steps.cache.outputs.cache-hit }}
      run: |
        cabal build --only-dependencies --enable-tests

    # Andreas, 2021-09-02:
    # Installing Agda, instead of just building it, is to work around
    # https://github.com/haskell/cabal/issues/7577
    # which hides the build executable from the tests when there is
    # a Custom cabal setup (atm works only for Simple setup).
    # Once haskell/cabal#7577 is fixed, we can dispose of this workaround.
    - name: Install Agda
      run: |
        mkdir -p ${HOME}/bin
        cabal install --installdir=${HOME}/bin
# bin instead of .cabal/bin as this might be more portable (Windows)

    - name: Build and test Agda
      run: |
        export PATH=${HOME}/bin:${PATH}
        cabal test
