name: macOS tests

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

jobs:
  test:
    name: Build and run tests
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        python: [ 3.8 ]
        os:  [ macos-latest ]
    defaults:
      run:
        shell: bash
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.6.0
        with:
          access_token: ${{ secrets.ACTIONS }}

      - name: Checkout
        uses: actions/checkout@v2
        with:
          submodules: recursive

      - name: Cache conda and dependancies
        id: cache
        uses: actions/cache@v2
        with:
          path: |
            /usr/share/miniconda/envs/anaconda-client-env
            ~/osx-conda
            ~/.bashrc
          key: ${{ runner.os }}-${{ matrix.python }}-conda-v1-${{ hashFiles('requirements/conda_minimal_deps.txt') }}

      - name: Install Conda
        uses: conda-incubator/setup-miniconda@v2
        if: steps.cache.outputs.cache-hit != 'true'
        with:
          activate-environment: anaconda-client-env
          python-version: ${{ matrix.python }}
          channels: conda-forge
          channel-priority: strict
          auto-update-conda: true
          use-only-tar-bz2: true

      - name: Install conda deps
        if: steps.cache.outputs.cache-hit != 'true'
        shell: bash -l {0} #We need a login shell to get conda
        run: |
            conda install --yes --file=requirements/conda_minimal_deps_${{ runner.os }}.txt
            conda install --yes --file=requirements/conda_minimal_deps.txt

      - name: Fix OSX Cache Write #OSX Won't let the cache restore due to file perms
        if: steps.cache.outputs.cache-hit != 'true' && matrix.os == 'macos-latest'
        run: |
          cp -r /usr/local/miniconda/envs/anaconda-client-env ~/osx-conda

      - name: Fix OSX Cache Restore
        if: steps.cache.outputs.cache-hit == 'true' && matrix.os == 'macos-latest'
        run: |
          mkdir -p /usr/local/miniconda/envs
          sudo cp -r ~/osx-conda /usr/local/miniconda/envs/anaconda-client-env

      - name: Init conda
        shell: bash -l {0}
        run: |
          conda init bash

      - name: Fix OSX profile
        if: steps.cache.outputs.cache-hit != 'true' && matrix.os == 'macos-latest'
        run: |
          cp ~/.bash_profile ~/.bashrc

      - name: Configure and build
        # shell: bash -l {0}
        run: |
          # conda init bash
          source ~/.bashrc
          conda activate anaconda-client-env
          autoreconf --install
          ./configure
          make -j 3

      - name: Run C++ tests
        run: |
          source ~/.bashrc
          conda activate anaconda-client-env
          make check -j 2
