name: Tests master

on:
  # This avoids having duplicate builds for a pull request
  push:
    branches:
      - master
      - "*-maintenance"
  pull_request:
    branches:
      - master
      - "*-maintenance"

jobs:
  ############################################################################
  # Lint jobs
  ############################################################################
  lint:
    name: lint
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    steps:
      - uses: actions/checkout@v2
      - name: Cache pip
        uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-py-3.9-pip-${{ hashFiles('**/setup.py') }}
      - uses: actions/setup-node@v2
        with:
          node-version: "14"
          cache: "npm"
          cache-dependency-path: "**/package-lock.json"
      - name: Install node dependencies
        run: make lektor/admin/node_modules
      - uses: actions/setup-python@v2
        with:
          python-version: "3.9"
      - name: Install python dependencies
        run: python -m pip install tox
        # - name: Run pylint
        #   run: tox -e lint
      - name: Run pre-commit
        uses: pre-commit/action@v2.0.3

  ############################################################################
  # Node tests
  ############################################################################
  node:
    name: ${{ matrix.os}} node${{ matrix.node }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        node: ["14"]
        os: ["ubuntu-latest", "macos-latest", "windows-latest"]
        include:
          - node: "12"
            os: "ubuntu-latest"
          - node: "16"
            os: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node }}
          cache: "npm"
          cache-dependency-path: "**/package-lock.json"
      - name: Build frontend
        run: make
        # - name: Typecheck and run frontend tests
        #   run: make test-js

  ############################################################################
  # Python tests
  ############################################################################
  python-tests:
    name: ${{ matrix.os }} py${{ matrix.python }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ["ubuntu-latest", "macos-latest", "windows-latest"]
        python: ["2.7", "3.6", "3.9"]
        include:
          - os: "ubuntu-latest"
            path: ~/.cache/pip
          - os: "macos-latest"
            path: ~/Library/Caches/pip
          - os: "windows-latest"
            path: ~\AppData\Local\pip\Cache
          - os: "ubuntu-latest"
            path: ~/.cache/pip
            python: "3.7"
          - os: "ubuntu-latest"
            path: ~/.cache/pip
            python: "3.8"
    steps:
      - uses: actions/checkout@v2
      - name: Cache pip
        uses: actions/cache@v2
        with:
          path: ${{ matrix.path }}
          key: ${{ runner.os }}-py-${{ matrix.python }}-pip-${{ hashFiles('**/setup.py') }}
      - uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python }}
      - name: Install macOS system dependencies
        if: startsWith(runner.os, 'macos')
        run: brew install imagemagick ffmpeg
      - name: Install Windows system dependencies
        if: startsWith(runner.os, 'windows')
        run: choco install --no-progress --timeout 600 imagemagick.tool ffmpeg
        continue-on-error: true
      - name: Install python dependencies
        run: python -m pip install codecov tox
      - name: Run python tests
        run: tox -e py
      - name: Publish coverage
        run: codecov -t 39974034-91d8-48d1-9698-de48e0667a09
