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.10', '3.11', '3.12', '3.13', '3.14-dev']
        qt-version: [5, 6]
        qt-binding: [pyqt]
        exclude:
          - python-version: '3.10'
            os: macos-latest
          - python-version: '3.11'
            qt-version: 5
          - python-version: '3.12'
            qt-version: 5
          - python-version: '3.13'
            qt-version: 5
          - python-version: '3.14-dev'
            qt-version: 5
      fail-fast: false
    steps:
      - name: Checkout branch
        uses: actions/checkout@v5
      - name: Install linux only test dependency
        if: matrix.os == 'ubuntu-latest'
        # Install all dependencies needed to run Qt on linux (taken from the qt website)
        # https://doc.qt.io/qt-6/linux.html
        # https://doc.qt.io/qt-6/linux-requirements.html
        run: |
          sudo apt-get update --fix-missing;
          cat ci/qt6_linux.txt | xargs sudo apt -y install
          sudo apt install -y xvfb herbstluftwm scrot
      - 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@v6
        with:
          python-version: ${{ matrix.python-version }}
          cache: 'pip'
          cache-dependency-path: 'test_requirements.txt'
      - name: Install dependencies
        # Skip pegen deps because of https://github.com/we-like-parsers/pegen/issues/53
        run: |
          python -m pip install --upgrade pip setuptools
          pip install setuptools-scm[toml]
          pip install numpy
          pip install git+https://github.com/nucleic/cppy@main
          pip install git+https://github.com/nucleic/atom@main
          pip install git+https://github.com/MatthieuDartiailh/bytecode@main
          pip install git+https://github.com/nucleic/kiwi@main
      - name: Install project (no-extras)
        if: matrix.python-version != '3.10'
        env:
          CPPFLAGS: --coverage
        # Use legacy command to install the library to be able to measure
        # coverage of C++ code.
        run: |
          pip install .[qt${{ matrix.qt-version }}-${{ matrix.qt-binding }}]
          pip uninstall enaml --yes
          python setup.py develop
      - name: Install project (with-extras pyqt)
        if: matrix.python-version == '3.10' && matrix.qt-binding == 'pyqt'
        env:
          CPPFLAGS: --coverage
        # Use legacy command to install the library to be able to measure
        # coverage of C++ code.
        run: |
          pip install .[qt${{ matrix.qt-version }}-${{ matrix.qt-binding }},ipython-qt,matplotlib-qt,scintilla-qt${{ matrix.qt-version }}-pyqt,webview-qt${{ matrix.qt-version }}-pyqt]
          pip uninstall enaml --yes
          python setup.py develop
      - 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 -v --cov --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 -v --cov --cov-report xml -rs
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          flags: unittests
          name: codecov-umbrella
          fail_ci_if_error: true
          verbose: true
