name: Test

on:
  workflow_dispatch:
  push:
    branches:
      - main
      - master
  pull_request:
    paths-ignore:
      - '**/*.md'

permissions:
  contents: read

env:
  PIP_DISABLE_PIP_VERSION_CHECK: 1

jobs:
  file-consistency:
    name: Check file consistency
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      - run: pip install $(grep tomli== requirements-tests.txt)
      - run: ./tests/check_consistent.py

  new-syntax:
    name: Ensure new syntax usage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      - run: ./tests/check_new_syntax.py

  flake8:
    name: Lint with flake8
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      - run: pip install $(grep flake8 requirements-tests.txt)
      - run: flake8

  pytype:
    name: Run pytype against the stubs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: "3.10"
      - run: pip install -r requirements-tests.txt
      - run: ./tests/pytype_test.py --print-stderr

  mypy:
    name: Run mypy against typeshed
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform: ["linux", "win32", "darwin"]
        python-version: ["2.7", "3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
      fail-fast: false
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      - run: pip install $(grep tomli== requirements-tests.txt) $(grep mypy== requirements-tests.txt) termcolor
      - run: ./tests/mypy_test.py --platform=${{ matrix.platform }} --python-version=${{ matrix.python-version }}

  pyright:
    name: Run pyright against typeshed
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-platform: ["Linux", "Windows", "Darwin"]
        python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
      fail-fast: false
    env:
      PYRIGHT_VERSION: 1.1.247 # Must match pyright_test.py.
    steps:
      - uses: actions/checkout@v2
      - uses: jakebailey/pyright-action@v1
        with:
          version: ${{ env.PYRIGHT_VERSION }}
          python-platform: ${{ matrix.python-platform }}
          python-version: ${{ matrix.python-version }}
          no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.
          project: ./pyrightconfig.stricter.json
      - uses: jakebailey/pyright-action@v1
        with:
          version: ${{ env.PYRIGHT_VERSION }}
          python-platform: ${{ matrix.python-platform }}
          python-version: ${{ matrix.python-version }}
          no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.

  stubtest-third-party:
    name: Check third party stubs with stubtest
    runs-on: ubuntu-20.04
    if: github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install dependencies
        run: pip install $(grep tomli== requirements-tests.txt) termcolor
      - name: Run stubtest
        run: |
          STUBS=$(
            git diff --name-only origin/${{ github.base_ref }} HEAD |
            # Uncomment the following to (very slowly) run on all third party stubs:
            # git ls-files |
            egrep ^stubs/ | cut -d "/" -f 2 | sort -u | (while read stub; do [ -d stubs/$stub ] && echo $stub || true; done)
          )
          if test -n "$STUBS"; then
            echo "Testing $STUBS..."
            APT_PACKAGES=$(python tests/get_apt_packages.py $STUBS)
            if test -n "$APT_PACKAGES"; then
              echo "Installing apt packages: $APT_PACKAGES"
              sudo apt update && sudo apt install -y $APT_PACKAGES
            fi
            python tests/stubtest_third_party.py $STUBS
          else
            echo "Nothing to test"
          fi
