name: tests

on: [push, pull_request]

jobs:

  linting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      - uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip
          restore-keys: ${{ runner.os }}-pip
      - name: Install Poetry
        uses: snok/install-poetry@v1.1.1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true
          # virtualenvs-path: ~/.venv

      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v2
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
      - name: Ensure cache is healthy
        if: steps.cache.outputs.cache-hit == 'true'
        shell: bash
        run: timeout 10s poetry run pip --version || rm -rf .venv

      # install dependencies if cache does not exist
      - name: Check cache and install dependencies
        run: poetry install

      - name: Run linters
        run: |
          source .venv/bin/activate
          # flake8 peeringdb_server
          black . --check
          isort .

  test:
    needs: linting
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@v2
      - name: setup dev environment
        run: touch ./Ctl/dev/.env
      - name: Start the DB
        run: ./Ctl/dev/compose.sh up -d database
      - name: build PeeringDB
        run: ./Ctl/dev/compose.sh build peeringdb
      - name: run tests
        run: ./Ctl/dev/run.sh run_tests
