name: Tests

on:
  push:
    branches: [main, 6.x]
  pull_request:

concurrency:
  group: tests-${{ github.ref }}
  cancel-in-progress: true

jobs:
  run-tests:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: ["ubuntu-latest", "macos-latest", "windows-latest"]
        python-version: ["3.7", "3.10"]
        include:
          - os: "windows-latest"
            python-version: "3.8"
          - os: "macos-latest"
            python-version: "3.9"
          - os: "ubuntu-latest"
            python-version: "pypy-3.8"
      fail-fast: false
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2
      - name: Run base setup actions
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1

      - name: Install Linux dependencies
        if: startsWith(runner.os, 'Linux')
        run: |
          sudo apt-get update
          sudo apt-get install texlive-plain-generic inkscape texlive-xetex

          # pandoc is not up to date in the ubuntu repos, so we install directly
          wget https://github.com/jgm/pandoc/releases/download/2.17.1.1/pandoc-2.17.1.1-1-amd64.deb && sudo dpkg -i pandoc-2.17.1.1-1-amd64.deb

      - name: Install package dependencies
        shell: bash
        run: |
          pip install codecov --user
          pip install -e ".[execute,serve,test]" --user
          python -m ipykernel.kernelspec --user
          # many things installed in --users need this to be in $PATH
          echo "/Users/runner/.local/bin" >> $GITHUB_PATH

      - name: List installed packages
        run: |
          pip freeze
          pip check

      - name: Run tests with coverage
        if: ${{ !startsWith(matrix.python-version, 'pypy') && !startsWith(runner.os, 'Windows')  }}
        shell: bash
        run: |
          # See https://github.com/pyppeteer/pyppeteer/pull/321
          pip install -U websockets --user
          python -m pytest --cov nbconvert -vv

      - name: Run tests on pypy and Windows
        if: ${{ startsWith(matrix.python-version, 'pypy') || startsWith(runner.os, 'Windows')  }}
        shell: bash
        run: |
          # See https://github.com/pyppeteer/pyppeteer/pull/321
          pip install -U websockets --user
          python -m pytest -vv

      - name: Code coverage
        run: codecov

  # Run "pre-commit run --all-files --hook-stage=manual"
  pre-commit:
    runs-on: ubuntu-20.04
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v2
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: pre-commit/action@v2.0.0
        with:
          extra_args: --all-files --hook-stage=manual
      - name: Help message if pre-commit fail
        if: ${{ failure() }}
        run: |
          echo "You can install pre-commit hooks to automatically run formatting"
          echo "on each commit with:"
          echo "    pre-commit install"
          echo "or you can run by hand on staged files with"
          echo "    pre-commit run"
          echo "or after-the-fact on already committed files with"
          echo "    pre-commit run --all-files --hook-stage=manual"

  check_manifest:
    timeout-minutes: 20
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Check Manifest
        run: |
          pip install check-manifest
          check-manifest --ignore "share/**"

  test_minimum_versions:
    name: Test Minimum Versions
    timeout-minutes: 20
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
        with:
          python_version: "3.7"
      - name: Install miniumum versions
        uses: jupyterlab/maintainer-tools/.github/actions/install-minimums@v1
      - name: Run the unit tests
        run: |
          pytest -vv -W default || pytest -vv -W default --lf

  test_prereleases:
    name: Test Prereleases
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Install the Python dependencies
        run: |
          pip install --pre -e ".[test]"
      - name: List installed packages
        run: |
          pip freeze
          pip check
      - name: Run the tests
        run: |
          pytest -vv || pytest -vv --lf

  make_sdist:
    name: Make SDist
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v2
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/make-sdist@v1

  test_sdist:
    runs-on: ubuntu-latest
    needs: [make_sdist]
    name: Install from SDist and Test
    timeout-minutes: 20
    steps:
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/test-sdist@v1
