name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches_ignore: []

jobs:
  formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Setup black environment
        run: conda create --quiet --name black black

      - name: Check formatting
        run: |
          export PATH="/usr/share/miniconda/bin:$PATH"
          source activate black
          black --check snakemake tests/*.py

      - name: Comment PR
        if: github.event_name == 'pull_request' && failure()
        uses: marocchino/sticky-pull-request-comment@v1.1.0
        with:
          message: 'Please format your code with [black](https://black.readthedocs.io): `black snakemake tests/*.py`.'
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  testing:
    runs-on: ubuntu-latest
    needs: formatting
    steps:
      - uses: actions/checkout@v1
      
      - name: Setup Snakemake environment
        run: conda env create --quiet --name snakemake --file test-environment.yml

      - name: Setup apt dependencies
        run: sudo apt install -y singularity-container stress
        
      - name: Setup iRODS
        run: |
          docker build -t irods-server tests/test_remote_irods
          docker run -d -p 1247:1247 --name provider irods-server -i run_irods
          sleep 10
          docker exec -u irods provider iput /incoming/infile
          cp -r tests/test_remote_irods/setup-data ~/.irods

      - name: Run tests
        env:
          CI: true
        run: |
          # enable coverage recording for subprocesses
          echo -e "try:\n    import coverage\n    coverage.process_startup()\nexcept:\n    pass" > sitecustomize.py
          export COVERAGE_PROCESS_START=.coveragerc

          # activate conda env
          export PATH="/usr/share/miniconda/bin:$PATH"
          source activate snakemake
          
          # run tests
          export AWS_DEFAULT_REGION=us-east-1
          export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}
          export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}
          coverage run -m pytest tests/test*.py -v -x

          # collect coverage report
          #coverage combine
          #coverage xml

          #- name: Upload coverage report
          #uses: codecov/codecov-action@v1.0.3
          #with:
          #token: ${{secrets.CODECOV_TOKEN}}

      - name: Build container image
        run: docker build .
