name: CI

on:
  pull_request:
    branches:
      - master
      - dev
  workflow_dispatch:

jobs:
  linux:
    name: Test - Ubuntu
    runs-on: ubuntu-latest
    steps:
      - name: Install rar
        run: sudo apt-get -y install unrar

      - name: Clone nixtlats
        uses: actions/checkout@v2
        with:
          path: nixtlats

      - name: Cache conda
        uses: actions/cache@v1
        env:
          CACHE_NUMBER: 0
        with:
          path: ~/conda_pkgs_dir
          key: ${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('nixtlats/environment.yml') }}

      - name: Set up environment
        uses: conda-incubator/setup-miniconda@v2
        with:
          mamba-version: "*"
          channels: pytorch,conda-forge,defaults
          channel-priority: true
          activate-environment: nixtlats
          environment-file: nixtlats/environment.yml
          use-only-tar-bz2: true

      - name: Install nixtlats
        shell: bash -l {0}
        run: pip install nixtlats/

      - name: Read all notebooks
        shell: bash -l {0}
        working-directory: nixtlats
        run: nbdev_read_nbs

      - name: Check if all notebooks are cleaned
        shell: bash -l {0}
        working-directory: nixtlats
        run: |
          echo "Check we are starting with clean git checkout"
          if [ -n "$(git status -uno -s)" ]; then echo "git status is not clean"; false; fi
          echo "Trying to strip out notebooks"
          nbdev_clean_nbs
          echo "Check that strip out was unnecessary"
          git status -s # display the status to see which nbs need cleaning up
          if [ -n "$(git status -uno -s)" ]; then echo -e "!!! Detected unstripped out notebooks\n!!!Remember to run nbdev_install_git_hooks"; false; fi

      - name: Check if there is no diff library/notebooks
        shell: bash -l {0}
        working-directory: nixtlats
        run: |
          if [ -n "$(nbdev_diff_nbs)" ]; then echo -e "!!! Detected difference between the notebooks and the library"; false; fi

      - name: Run tests
        shell: bash -l {0}
        working-directory: nixtlats
        run: |
          nbdev_test_nbs --fname "nbs/data_datasets__*"
          nbdev_test_nbs --timing True
