name: Mac tests

on:
  push:
    branches:
      - master
      - 5.x
      - 4.x
    paths:
      - '.github/scripts/*.sh'
      - '.github/workflows/*.yml'
      - 'requirements/*.txt'
      - 'MANIFEST.in'
      - '**.bat'
      - '**.py'
      - '**.sh'

  pull_request:
    branches:
      - master
      - 5.x
      - 4.x
    paths:
      - '.github/scripts/*.sh'
      - '.github/workflows/*.yml'
      - 'requirements/*.txt'
      - 'MANIFEST.in'
      - '**.bat'
      - '**.py'
      - '**.sh'

jobs:
  build:
    name: Py${{ matrix.PYTHON_VERSION }}, ${{ matrix.INSTALL_TYPE }}, ${{ matrix.TEST_TYPE }}
    runs-on: macos-latest
    env:
      CI: 'true'
      CODECOV_TOKEN: "56731c25-9b1f-4340-8b58-35739bfbc52d"
      OS: 'macos'
      PYTHON_VERSION: ${{ matrix.PYTHON_VERSION }}
      RUN_SLOW: ${{ matrix.TEST_TYPE == 'slow' }}
      USE_CONDA: ${{ matrix.INSTALL_TYPE == 'conda' }}
    strategy:
      fail-fast: false
      matrix:
        INSTALL_TYPE: ['conda']
        PYTHON_VERSION: ['3.7']
        TEST_TYPE: ['fast', 'slow']
    steps:
      - name: Checkout Pull Requests
        if: github.event_name == 'pull_request'
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - name: Checkout Push
        if: github.event_name != 'pull_request'
        uses: actions/checkout@v2
      - name: Fetch branches
        run: git fetch --prune --unshallow
      - name: Check build skips
        if: github.event_name == 'pull_request'
        shell: bash -l {0}
        run: source .github/scripts/check_skip.sh
      - name: Check build runs
        if: github.event_name != 'pull_request'
        shell: bash -l {0}
        run: echo "RUN_BUILD=true" >> $GITHUB_ENV
      - name: Cache conda
        uses: actions/cache@v1
        env:
          # Increase this value to reset cache if requirements/*.txt has not changed
          CACHE_NUMBER: 0
        with:
          path: ~/conda_pkgs_dir
          key: ${{ runner.os }}-cacheconda-install${{ matrix.INSTALL_TYPE }}-${{ matrix.PYTHON_VERSION }}-${{ env.CACHE_NUMBER }}-${{ hashFiles('requirements/*.txt') }}
      - name: Cache pip
        if: env.RUN_BUILD == 'true'
        uses: actions/cache@v1
        with:
          path: ~/Library/Caches/pip
          key: ${{ runner.os }}-cachepip-install${{ matrix.INSTALL_TYPE }}-${{ env.CACHE_NUMBER }}-${{ hashFiles('requirements/*.txt') }}
      - name: Install Conda
        if: env.RUN_BUILD == 'true'
        uses: conda-incubator/setup-miniconda@v2
        with:
          activate-environment: test
          auto-update-conda: true
          auto-activate-base: false
          python-version: ${{ matrix.PYTHON_VERSION }}
          use-only-tar-bz2: true
      - name: Create test environment
        if: env.RUN_BUILD == 'true'
        shell: bash -l {0}
        run: bash -l .github/scripts/install.sh
      - name: Show test environment
        if: env.RUN_BUILD == 'true'
        shell: bash -l {0}
        run: |
          conda info
          conda list
      - name: Run manifest checks
        if: env.RUN_BUILD == 'true'
        shell: bash -l {0}
        run: check-manifest
      - name: Run tests
        if: env.RUN_BUILD == 'true'
        shell: bash -l {0}
        run: python runtests.py || python runtests.py || python runtests.py || python runtests.py || python runtests.py
      - name: Coverage
        if: env.RUN_BUILD == 'true'
        shell: bash -l {0}
        run: codecov
