name: Unit tests

on: [push, pull_request]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version:
        - '3.9'
        - '3.10'
        - '3.11'
        - '3.12-dev'
        - '3.14-dev'
        - 'pypy-3.9'
        - 'pypy-3.10'

    env:
      # TODO: get this dynamically
      PORTAGE_VERSION: "3.0.70"

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        set -xe
        python -VV
        python -m site
        python -m pip install --upgrade pip
        # setuptools needed for 3.12+ because of https://github.com/mesonbuild/meson/issues/7702.
        python -m pip install meson ninja setuptools pytest
    - name: Install portage
      run: |
        mkdir portage
        wget -qO - "https://github.com/gentoo/portage/archive/portage-${PORTAGE_VERSION}.tar.gz" | tar xz -C portage --strip-components=1
        sudo groupadd -g 250 portage
        sudo useradd -g portage -d /var/tmp/portage -s /bin/false -u 250 portage
    - name: Setup gentoo env (required by portage)
      run: |
        sudo mkdir -p /var/db/repos/gentoo /etc/portage /var/cache/distfiles
        wget -qO - "https://github.com/gentoo-mirror/gentoo/archive/master.tar.gz" | sudo tar xz -C /var/db/repos/gentoo --strip-components=1
        sudo wget "https://www.gentoo.org/dtd/metadata.dtd" -O /var/cache/distfiles/metadata.dtd
        sudo wget "https://raw.githubusercontent.com/gentoo/portage/refs/heads/master/cnf/repos.conf" -O /etc/portage/repos.conf
        sudo ln -s /var/db/repos/gentoo/profiles/default/linux/amd64/23.0/systemd /etc/portage/make.profile
    - name: Test meson install --destdir /tmp/install-root
      run: |
        echo -e "[binaries]\npython = '$(command -v python)'" > /tmp/native.ini
        meson setup -Dtests=true --native-file /tmp/native.ini /tmp/build .
        meson install -C /tmp/build --destdir /tmp/install-root
    - name: Run tests for ${{ matrix.python-version }}
      run: |
        export PYTHONPATH="${PWD}/portage/lib"${PYTHONPATH:+:}${PYTHONPATH}
        export PATH="${PWD}/portage/bin":${PATH}
        meson test -C /tmp/build --verbose
