name: Tests

on:
  push:
  pull_request:

jobs:
  static_tests:
    runs-on: ubuntu-latest

    defaults:
      run:
        shell: bash -l {0}

    steps:
      - uses: actions/checkout@v2

      - uses: conda-incubator/setup-miniconda@v2
        with:
          python-version: "3.9"
          mamba-version: "*"
          channels: conda-forge,bioconda,defaults
          channel-priority: true
          activate-environment: V-pipe
          auto-update-conda: true

      - name: Install dependencies
        run: mamba install --yes snakemake-minimal snakefmt

      - name: Code formatting
        run: snakefmt --check .

      - name: Setup project
        run: |
          mkdir -p /tmp/project/hiv/  # virus does not matter
          cd /tmp/project/hiv/
          $GITHUB_WORKSPACE/init_project.sh -n

      - name: Linting
        env:
          PYTHONUNBUFFERED: 1
        uses: snakemake/snakemake-github-action@v1.19.0
        with:
          directory: /tmp/project/hiv/
          snakefile: 'workflow/Snakefile'
          args: '--lint'
        continue-on-error: true  # TODO: fix missing cohort consensus

  runtime_tests:
    needs: static_tests

    strategy:
      max-parallel: 5
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: ["3.8", "3.9"]
        virus: ["hiv", "sars-cov-2"]

    runs-on: ${{ matrix.os }}

    defaults:
      run:
        shell: bash -l {0}

    steps:
      - uses: actions/checkout@v2

      - uses: conda-incubator/setup-miniconda@v2
        with:
          python-version: ${{ matrix.python-version }}
          mamba-version: "*"
          channels: conda-forge,bioconda,defaults
          channel-priority: true
          activate-environment: V-pipe
          auto-update-conda: true

      - name: Install dependencies
        run: mamba install --yes snakemake-minimal

      - name: Setup project
        run: |
          mkdir -p /tmp/project/${{ matrix.virus }}/
          cd /tmp/project/${{ matrix.virus }}/
          $GITHUB_WORKSPACE/init_project.sh -n

      - name: Run tests
        working-directory: ./tests
        run: ./regression_tests.sh ${{ matrix.virus }}

      - name: Archive code coverage results
        uses: actions/upload-artifact@v2
        with:
          name: regression test output and diffs
          path: |
            /tmp/v-pipe_tests/*
          if-no-files-found: ignore
