name: main

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  check:
    env:
      WEB3_INFURA_PROJECT_ID: "1c91697c211f4fcd8c7361f4c4e1f55f"
      # not sure why, but this doesn't work currently:
      # WEB3_INFURA_PROJECT_ID: ${{ secrets.WEB3_INFURA_PROJECT_ID }}
      # WEB3_INFURA_API_SECRET: ${{ secrets.WEB3_INFURA_API_SECRET }}

    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v3

    - name: Install OS package dependencies
      run: |
        sudo apt update
        sudo apt install libenchant-2-dev libbz2-dev libsnappy-dev libunwind-dev libgirepository1.0-dev

    - name: Set up Python 3.11
      uses: actions/setup-python@v3
      with:
        python-version: '3.11'
        architecture: 'x64'

    - name: Install Python package dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements-dev.txt

    - name: Run Flake8
      run: tox -c tox.ini -e flake8

  test:
    env:
      CB_FULLTESTS: 1

      WEB3_INFURA_PROJECT_ID: "1c91697c211f4fcd8c7361f4c4e1f55f"
      # not sure why, but this doesn't work currently:
      # WEB3_INFURA_PROJECT_ID: ${{ secrets.WEB3_INFURA_PROJECT_ID }}
      # WEB3_INFURA_API_SECRET: ${{ secrets.WEB3_INFURA_API_SECRET }}

    # Test on Ubuntu, MacOS, Windows using CPython and PyPy
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-22.04]
        # os: [ubuntu-22.04, macos-latest, windows-latest]

        # python 3.11 fails with "src/twisted/test/raiser.c:198:12: fatal error: longintrepr.h: No such file or directory"
        # twisted doesn't yet support 3.11 formally: https://github.com/twisted/twisted/blob/trunk/pyproject.toml#L24
        python-version: ['3.9', '3.11', 'pypy-3.9']
        framework: ['asyncio', 'tw2403', 'twtrunk']

    # https://github.blog/changelog/2020-04-15-github-actions-new-workflow-features/
    # https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepscontinue-on-error
    continue-on-error: false

    steps:
    # Checkout sources
    - uses: actions/checkout@v3

    # Install OS packages, as we install Python packages from source:
    #
    # libenchant-dev              needed for pyenchant, needed for sphinx-spellcheck
    # libbz2-dev, libsnappy-dev   needed for compression
    # libunwind-dev               needed for vmprof
    #
    - name: Install OS package dependencies
      run: |
        sudo apt update
        sudo apt install build-essential libssl-dev libffi-dev libunwind-dev \
          libreadline-dev zlib1g-dev libbz2-dev libsqlite3-dev libncurses5-dev \
          libsnappy-dev libgirepository1.0-dev

    # Use this Python
    # https://github.com/actions/setup-python/blob/main/README.md
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v3
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install Python package dependencies
      run: |
        python -m pip install -U pip
        pip install -U -r requirements-dev.txt

    - name: Install this package
      run: |
        pip install .[all]

    - name: Prechecks
      run: |
        python -c "import autobahn; print(autobahn.__version__)"
        python -c "from autobahn import xbr; print(xbr.HAS_XBR)"
        python -c "from autobahn.testutil import FakeTransport; print(FakeTransport)"
        python -c "import os; print('WEB3_INFURA_PROJECT_ID', len(os.environ.get('WEB3_INFURA_PROJECT_ID', '')))"
        python -c "import os; print('WEB3_INFURA_API_SECRET', len(os.environ.get('WEB3_INFURA_API_SECRET', '')))"

    # does not work on py3.11
    # python -c "from web3.auto.infura import w3; print(w3.isConnected())"

    - name: Run unit tests (PyTest)
      run: |
        tox -c tox.ini -e ${{ matrix.framework }}

  docs:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v3

    - name: Install OS package dependencies
      run: |
        sudo apt update
        sudo apt install libenchant-2-dev libbz2-dev libsnappy-dev libunwind-dev libgirepository1.0-dev

    - name: Set up Python 3.11
      uses: actions/setup-python@v3
      with:
        python-version: '3.11'
        architecture: 'x64'

    - name: Install Python package dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements-dev.txt

    - name: Install this package and run Sphinx
      run: |
        pip install .[all]
        tox -c tox.ini -e sphinx
