name: Continuous Integration
on:
  schedule:
    - cron: '0 0 * * 2'
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
    paths:
      - .github/workflows/ci.yml
      - "enaml/**"
      - "examples/**"
      - "tests/**"
      - setup.py
      - pyproject.toml

jobs:
  tests:
    name: Unit tests
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ['3.8', '3.9', '3.10']
        qt-version: [5, 6]
        qt-binding: [pyqt, pyside]
        exclude:
          - python-version: '3.10'
            qt-version: 5
            qt-binding: pyside
          - python-version: '3.8'
            qt-version: 6
      fail-fast: false
    steps:
      - name: Install linux only test dependency
        if: matrix.os == 'ubuntu-latest'
        shell: bash
        # Install all dependencies needed to run Qt on linux (taken from the qt website
        # and completed by adding package marked missing when running with
        # QT_DEBUG_PLUGINS=1)
        run: |
          sudo apt-get update --fix-missing
          sudo apt-get install -y libfontconfig1-dev libfreetype6-dev libx11-dev libxext-dev libxfixes-dev libxi-dev libxrender-dev libxcb1-dev libx11-xcb-dev libxcb-glx0-dev libxcb-keysyms1-dev libxcb-image0-dev libxcb-shm0-dev libxcb-icccm4-dev libxcb-sync0-dev libxcb-xfixes0-dev libxcb-shape0-dev libxcb-randr0-dev libxcb-render-util0-dev libxcb-xinerama0-dev libxcb-xkb-dev libxkbcommon-x11-dev libxcb-xtest0-dev  libegl1-mesa xvfb --fix-missing
          sudo apt-get install -y herbstluftwm scrot
      - name: Checkout branch
        uses: actions/checkout@v2
      - name: Get history and tags for SCM versioning to work
        run: |
          git fetch --prune --unshallow
          git fetch --depth=1 origin +refs/tags/*:refs/tags/*
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v3
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install numpy
          pip install git+https://github.com/MatthieuDartiailh/bytecode@main
          pip install git+https://github.com/nucleic/atom@main
          pip install git+https://github.com/nucleic/kiwi@main
      - name: Install project (no-extras)
        if: matrix.python-version != '3.9'
        env:
          CPPFLAGS: --coverage
        run: |
          pip install -e .[qt${{ matrix.qt-version }}-${{ matrix.qt-binding }}]
      - name: Install project (with-extras pyqt)
        if: matrix.python-version == '3.9' && matrix.qt-binding == 'pyqt'
        run: |
          pip install -e .[qt${{ matrix.qt-version }}-${{ matrix.qt-binding }},ipython-qt,matplotlib-qt,scintilla-qt${{ matrix.qt-version }}-pyqt,webview-qt${{ matrix.qt-version }}-pyqt]
      - name: Install project (with-extras pyside)
        if: matrix.python-version == '3.9' && matrix.qt-binding != 'pyqt'
        run: |
          pip install -e .[qt${{ matrix.qt-version }}-${{ matrix.qt-binding }},ipython-qt,matplotlib-qt]
      - name: Install test requirements
        run: |
          pip install -r test_requirements.txt
      - name: Run tests (Windows, Mac)
        if: matrix.os != 'ubuntu-latest'
        run: python -X dev -m pytest tests --cov enaml --cov-report xml -rs
      - name: Run tests (Linux)
        if: matrix.os == 'ubuntu-latest'
        shell: bash -l {0}
        run: |
          pip uninstall python3-xlib --yes
          pip install python-xlib
          export DISPLAY=:99.0
          /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -screen 0 1920x1200x24 -ac +extension GLX +render -noreset
          sleep 3
          herbstluftwm &
          sleep 1
          python -X dev -m pytest tests --cov enaml --cov-report xml -rs
      - name: Generate C++ coverage reports
        if: (github.event_name != 'schedule' && matrix.os != 'windows-latest')
        run: |
          bash -c "find . -type f -name '*.gcno' -exec gcov -pb --all-blocks {} +" || true
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v2
        if: (github.event_name != 'schedule' && matrix.os != 'windows-latest')
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          flags: unittests
          name: codecov-umbrella
          fail_ci_if_error: true
