name: ci

on:
  push:
    branches:
      - master
      - maint
      - ci/all
      - ci/github
  pull_request:
    branches:
      - master
      - maint
  workflow_dispatch:

jobs:

  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        py-version:
          - "2.7"
          - "3.5"
          - "3.6"
          - "3.7"
          - "3.8"
          - "3.9"
          - "3.10"
          - "pypy-2.7"
          - "pypy-3.6"
          - "pypy-3.7"
        py-arch:
          - x86
          - x64
        mpi:
          - mpich
          - openmpi
          - msmpi
        os:
          - ubuntu-18.04
          - macos-10.15
          - windows-2019
        exclude:
          - os: ubuntu-18.04
            py-arch: x86
          - os: ubuntu-18.04
            mpi: msmpi
          - os: macos-10.15
            py-version: pypy-2.7
          - os: macos-10.15
            py-version: pypy-3.6
          - os: macos-10.15
            py-version: pypy-3.7
          - os: macos-10.15
            py-arch: x86
          - os: macos-10.15
            mpi: msmpi
          - os: windows-2019
            mpi: mpich
          - os: windows-2019
            mpi: openmpi
          - os: windows-2019
            py-version: pypy-2.7
          - os: windows-2019
            py-version: pypy-3.6
            py-arch: x86
          - os: windows-2019
            py-version: pypy-3.7
            py-arch: x86

    steps:

    - name: Configure hostname
      if:   runner.os == 'Linux' || runner.os == 'macOS'
      run:  echo 127.0.0.1 `hostname` | sudo tee -a /etc/hosts > /dev/null

    - name: Activate MSVC
      if:   runner.os == 'Windows' && matrix.py-version == '2.7'
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: ${{ matrix.py-arch }}

    - name: Configure MSVC
      if:   runner.os == 'Windows' && matrix.py-version == '2.7'
      run:  |
        Add-Content $Env:GITHUB_ENV MSSdk=1
        Add-Content $Env:GITHUB_ENV DISTUTILS_USE_SDK=1

    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup MPI (${{ matrix.mpi }})
      uses: mpi4py/setup-mpi@v1
      with:
        mpi: ${{ matrix.mpi }}

    - name: Use Python ${{ matrix.py-version }} ${{ matrix.py-arch }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.py-version }}
        architecture: ${{ matrix.py-arch }}

    - name: Install packaging tools
      run:  python -m pip install --upgrade setuptools pip wheel
    - name: Install build dependencies
      run:  python -m pip install --upgrade cython
    - name: Build package
      run:  python -m pip wheel -vvv --wheel-dir=dist .
    - name: Upload package artifacts
      uses: actions/upload-artifact@v2
      with:
        name: mpi4py-${{matrix.mpi}}-${{matrix.os}}
        path: dist

    - name: Install test dependencies
      run:  python -m pip install --upgrade numpy cffi pyyaml
    - name: Install package for testing
      run:  python -m pip install --no-index --find-links=dist mpi4py
    - name: Test package
      run:  mpiexec -n 1 python test/runtests.py -v
    - name: Test package
      run:  mpiexec -n 2 python test/runtests.py -v -f -e spawn
    - name: Test subpackage
      run:  mpiexec -n 1 python demo/futures/test_futures.py -v
    - name: Test subpackage
      run:  mpiexec -n 2 python demo/futures/test_futures.py -v
    - name: Uninstall package after testing
      run:  python -m pip uninstall --yes mpi4py

  lint:
    runs-on: ubuntu-20.04

    steps:

    - name: Checkout
      uses: actions/checkout@v2

    - name: Use Python
      uses: actions/setup-python@v2
      with:
        python-version: 3

    - name: Install package
      run:  python -m pip install .
      env:
        MPICFG: nompi

    - name: Install lint dependencies
      run:  python -m pip install
              pycodestyle
              pydocstyle
              flake8
              pylint
              mypy

    - name: pycodestyle
      run:  pycodestyle src/mpi4py

    - name: pydocstyle
      run:  pydocstyle src/mpi4py

    - name: flake8
      run:  flake8 src/mpi4py

    - name: pylint
      run:  pylint mpi4py

    - name: mypy (stubtest)
      run:  stubtest mpi4py
              --mypy-config-file=conf/mypy.ini
              --allowlist=conf/mypy.stubtest.allow.txt

    - name: mypy (typecheck)
      run:  mypy -p mpi4py
              --config-file=conf/mypy.ini
