name: Testsuite

on:
  [push, pull_request]

defaults:
  run:
    shell: bash

jobs:
  linter:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        python-version:  [3.8]
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Get pip cache dir
      id: pip-cache
      run: |
        echo "::set-output name=dir::$(pip cache dir)"
    - name: pip cache
      uses: actions/cache@v2
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: py${{ matrix.python-version }}-${{ matrix.os }}-pip
    - name: Install linter
      run: python -m pip install flake8
    - name: Check syntax and style
      run: python -m flake8 .

  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest]
        python-version: [3.6, 3.7, 3.8, 3.9, "3.10", pypy3]
        exclude:
          - os: windows-latest
            python-version: pypy3
    env:
      OS: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Get pip cache dir
      id: pip-cache
      run: |
        python -m pip install -U pip # to ensure version > 20 to have cache dir
        echo "::set-output name=dir::$(pip cache dir)"
    - name: pip cache
      uses: actions/cache@v2
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: py${{ matrix.python-version }}-${{ matrix.os }}-pip
    - name: Test environment setup
      run: |
        python -m pip install wheel
        python -m pip install pytest pytest-cov tox
    - name: Run tests
      run: |
        TOX_PYTHON_VERSION=$(if [ ${{ matrix.python-version }} = pypy3 ]; then echo "pypy3"; else echo py${{ matrix.python-version }} | tr -d .-; fi)
        COV_CMD=$(if [ ${{ matrix.python-version }} = 3.8 ]; then echo "--cov=./pytest_order/ --cov-report=xml"; else echo ; fi) tox -e $(tox -l | grep $TOX_PYTHON_VERSION | paste -sd "," -)
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v1
      if: ${{ success() && matrix.python-version == 3.8 }}
      with:
        env_vars: OS
        name: codecov-pytest-order

  deploy:
    if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')

    runs-on: ubuntu-latest

    needs: [linter, test]

    steps:
    - uses: actions/checkout@v2
      with:
        path: main
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: "3.8"
    - name: Install wheel
      run: |
        python -m pip install --upgrade pip
        pip install wheel
    - name: Build package
      run: |
        cd main
        python setup.py sdist bdist_wheel
    - name: Publish package to PyPI
      uses: pypa/gh-action-pypi-publish@master
      with:
        packages_dir: main/dist/
        user: __token__
        password: ${{ secrets.PYPI_API_TOKEN_PYTEST_ORDER }}
    - name: Create release docs
      run: |
        pip install sphinx
        cd main/docs
        make html
    - name: Checkout gh-pages
      uses: actions/checkout@v2
      with:
        ref: gh-pages
        path: doc
    - name: Copy and commit changes
      run: |
        cp -r main/docs/build/html/* doc/stable
        cd doc
        git config user.name "CI Build"
        git config user.email "pytest_order@gmail.com"
        git add stable/*
        git commit -a -m "Release documentation"
        git push
