name: CI testing

# see: https://help.github.com/en/actions/reference/events-that-trigger-workflows
on:  # Trigger the workflow on push or pull request,  but only for the master branch
  push: {}
  pull_request:
    branches: [master]

jobs:

  pytest:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-20.04, windows-2019, macOS-10.15]
        python-version: [3.6, 3.9]
        requires: ['latest']
        include:
          - {os: "ubuntu-18.04", python-version: 2.7, requires: 'latest'}
          - {os: "ubuntu-18.04", python-version: 3.6, requires: 'oldest'}

    # Timeout: https://stackoverflow.com/a/59076067/4521646
    timeout-minutes: 35

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}

    - name: update OS
      if: runner.os == 'Linux'
      run: |
        sudo apt-get install -y openslide-tools

    - name: Update Pip
      run: |
        pip install --quiet "pip>=20.1" --upgrade --user  # needed for get pip cacher folder

    - name: Set py2.7 dependencies
      if: matrix.python-version == 2.7
      run: |
        cp -r require-py27.txt requirements.txt
        # required for matplotlib @py2
        pip install -U backports.functools_lru_cache

    - name: Set min. dependencies
      if: matrix.requires == 'oldest'
      run: |
        for fpath in ('requirements.txt', 'tests/requirements.txt'):
            req = open(fpath).read().replace('>=', '==')
            open(fpath, 'w').write(req)
      shell: python

    # Note: This uses an internal pip API and may not always work
    # https://github.com/actions/cache/blob/master/examples.md#multiple-oss-in-a-workflow
    - name: Get pip cache
      id: pip-cache
      run: |
        python -c "from pip._internal.locations import USER_CACHE_DIR; print('::set-output name=dir::' + USER_CACHE_DIR)"

    - name: Cache pip
      uses: actions/cache@v2
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: ${{ runner.os }}-pip-py${{ matrix.python-version }}-${{ matrix.requires }}-${{ hashFiles('requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-py${{ matrix.python-version }}-${{ matrix.requires }}-

    - name: Install dependencies
      run: |
        pip install $(grep "numpy" requirements.txt)
        # v46 crashes openslide-python install
        pip install "setuptools<46" -U
        pip install --requirement requirements.txt --upgrade
        pip install --requirement tests/requirements.txt --quiet --upgrade
        python --version
        pip --version
        pip list
      shell: bash

    - name: Tests and coverage
      env:
        DISPLAY: ""
      run: |
        coverage run --source birl -m pytest birl tests bm_dataset bm_experiments bm_ANHIR -v --durations=25 --junitxml=junit/test-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}.xml
        # flake8 .

    - name: Upload pytest test results
      uses: actions/upload-artifact@master
      with:
        name: pytest-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}
        path: junit/test-results-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.requires }}.xml
      # Use always() to always run this step to publish test results when there are test failures
      if: always()

    - name: Statistics
      if: success()
      run: |
        coverage report
        coverage xml

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v1
      if: success()
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        file: coverage.xml
        fail_ci_if_error: false

    - name: Run sample experiment
      run: |
        mkdir results
        touch configs/sample_config.yaml
        python bm_experiments/bm_comp_perform.py -o ./results -n 1
        python birl/bm_template.py -t ./data-images/pairs-imgs-lnds_mix.csv -o ./results --visual --unique -cfg configs/sample_config.yaml
        # remove target landmarks from histol. tissue
        rm ./data-images/*_/*/*_HE.csv
        python birl/bm_template.py -n anhir -t ./data-images/pairs-imgs-lnds_histol.csv -d ./data-images -o ./results --preprocessing matching-rgb gray -cfg configs/sample_config.yaml
        python bm_experiments/evaluate_experiment.py -d ./data-images -e ./results/BmTemplate_anhir --visual
      shell: bash
