name: Test Gnofract 4D

on:
  pull_request:
    types: [opened, reopened, ready_for_review, synchronize]
  push:
    branches: ["master"]

jobs:
  run:

    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-22.04]
        python: ["3.9", "3.10", "3.11", "3.12", "3.13-dev"]
        toxenv: [py]
        # Ensure ../codecov.yml uploads after all py jobs
        include:
          - os: macos-13
            python: "3.12"
            toxenv: py

    env:
      # For labelling Codecov report
      OS: ${{ matrix.os }}
      PYTHON: ${{ matrix.python }}

    steps:
      - uses: actions/checkout@v4
      - name: Install macOS packages
        if: ${{ runner.os == 'macOS' }}
        run: brew install gtk4 ninja pygobject3
        # Prevent delay caused by:
        # `brew cleanup` has not been run in 30 days, running now...
        env:
          HOMEBREW_NO_INSTALL_CLEANUP: 1
      - name: Install Ubuntu packages
        if: ${{ runner.os == 'Linux' }}
        # libgirepository1.0-dev required to build PyGObject from source
        # dbus-x11 avoids timeouts, failed to execute "dbus-launch"
        # gvfs is needed for Gio.AppInfo.launch_default_for_uri()
        # libxml2-utils provides xmllint for xml-stripblanks in gresource.xml
        run: |
          sudo apt update
          sudo apt install dbus-x11 gir1.2-gtk-4.0 gvfs libgirepository1.0-dev libxml2-utils ninja-build xvfb
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python }}
      - name: Install Tox and any other PyPI packages
        run: pip install meson pytest tox
      - name: Build
        run: |
            meson setup _build
            meson compile -C _build
      - name: Run Tox (MacOS)
        if: ${{ runner.os == 'macOS' }}
        run: tox -e ${{ matrix.toxenv }}
        continue-on-error: true  # Gio introspection is broken: https://gitlab.gnome.org/GNOME/glib/-/issues/3322
      - name: Run Tox (Linux)
        if: ${{ runner.os == 'Linux' }}
        run: xvfb-run --auto-servernum tox -e ${{ matrix.toxenv }}
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        with:
          files: ./coverage.xml
          env_vars: OS,PYTHON

  lint:

    name: ${{ matrix.toxenv }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        toxenv:
          - pylint

    steps:
      - uses: actions/checkout@v4
      - name: Install Ubuntu packages
        # libglib2.0-dev-bin provides glib-compile-resources
        run: |
          sudo apt update
          sudo apt install gir1.2-gtk-4.0 libglib2.0-dev-bin libxml2-utils ninja-build python3-gi
      - name: Install Tox and any other PyPI packages
        run: pip install meson pytest tox
      - name: Build
        run: |
            meson setup _build
            meson compile -C _build
      - name: Run ${{ matrix.toxenv }}
        run: tox -e ${{ matrix.toxenv }}
