Skip to content

Conversation

keszybz
Copy link
Member

@keszybz keszybz commented Oct 15, 2025

This seems to do the trick:

$ ninja -C build
$ PYTHONPATH=build/src/ python -c 'from systemd import id128; print(id128)'
<module 'systemd.id128' from '/home/zbyszek/src/python-systemd/build/src/systemd/id128.cpython-313-x86_64-linux-gnu.so'>
$ PYTHONPATH=build/src/ pytest build/src/systemd/test ...
============================ test session starts ============================= platform linux -- Python 3.13.4, pytest-8.3.4, pluggy-1.5.0 rootdir: /home/zbyszek/src/python-systemd
configfile: pyproject.toml
plugins: ...
collected 61 items

build/src/systemd/test/test_daemon.py ........................         [ 39%]
build/src/systemd/test/test_id128.py ....                              [ 45%]
build/src/systemd/test/test_journal.py ............................    [ 91%]
build/src/systemd/test/test_login.py .....                             [100%]

============================= 61 passed in 0.21s =============================

@keszybz keszybz requested a review from behrmann October 15, 2025 08:45
@keszybz
Copy link
Member Author

keszybz commented Oct 15, 2025

/cc @jelly

@jelly
Copy link
Contributor

jelly commented Oct 15, 2025

Neat, with this you can also introduce a test target:

build_dir = meson.current_build_dir() / 'src'
test_dir = meson.current_source_dir() / 'test'

test(
  'unit tests', 
  find_program('pytest'),
  args: [test_dir],
  env: { 'PYTHONPATH': build_dir },
)

Copy link
Contributor

@behrmann behrmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Tested it and it works. I do wonder why it didn't work when we copied in the files manually

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to propose to not ship the tests in the module, since they are not really useful to users anyway. This way this file would also not be needed.

@keszybz
Copy link
Member Author

keszybz commented Oct 15, 2025

Reworked. The new meson file in the subdirectory is dropped, the tests are run from the source directory.
A new commit to add a test() declaration is added. meson test -C build and ninja -C build test now work.

I think we want to rework the sphinx build process to not require an installation, but let's do that in #158.

@behrmann
Copy link
Contributor

Running

meson setup build
meson compile -C build
meson test -C build

fails for me, because the PYTHONPATH gets set up wrong to $PROJECT_ROOT/build/src/systemd

Since I also mentioned moving the tests out of the sources, by moving the tests up to the project root and then adding a new meson.build in src/

test(
        'unit',
        find_program(python.full_path()),
        args: [
                '-m', 'pytest',
                '-v',
                meson.project_source_root() / 'test',
              ],
        env: { 'PYTHONPATH': meson.current_build_dir() },
)

subdir('systemd')

The subdir in the top-level meson.build need to refer to that then, but all the testing related stuff in src/systemd/meson.build can be dropped.

I've not yet thought about how to do it without moving the tests up to the top-level, since python_test_dir only gets defined in src/systemd/meson.build.

I think this was needed as some point in meson, but it isn't anymore and
we dropped those in systemd a while ago.
This seems to do the trick:
$ ninja -C build
$ PYTHONPATH=build/src/ python -c 'from systemd import id128; print(id128)'
<module 'systemd.id128' from '/home/zbyszek/src/python-systemd/build/src/systemd/id128.cpython-313-x86_64-linux-gnu.so'>
$ PYTHONPATH=build/src/ pytest src/systemd/test
...
============================ test session starts =============================
platform linux -- Python 3.13.4, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/zbyszek/src/python-systemd
configfile: pyproject.toml
plugins: ...
collected 61 items

build/src/systemd/test/test_daemon.py ........................         [ 39%]
build/src/systemd/test/test_id128.py ....                              [ 45%]
build/src/systemd/test/test_journal.py ............................    [ 91%]
build/src/systemd/test/test_login.py .....                             [100%]

============================= 61 passed in 0.21s =============================
The python binary that was initially found is used to launch pytest.

It turns out to be surprisingly hard to run 'pytest' in test() with the python
binary detected using meson's python module. test() does not allow the
"program" argument to be a str, which is what python module's .full_path()
returns. So let's "find" the program again. This results in one line of noise
in meson output, but I don't see a nicer way.

'ninja -C build test' works. 'meson test -C build' is better, because it can be
called with '-v' to print details of the test.
@keszybz
Copy link
Member Author

keszybz commented Oct 15, 2025

Oh, I messed up the paths. Can you try the current version?
(I think we can move the tests, but I'm not sure if this is wanted.)

Copy link
Contributor

@behrmann behrmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked, installation into a venv works with plain python -m venv and uv. Tests run with meson setup, compile, test cycle.

@keszybz keszybz merged commit f7883c1 into systemd:main Oct 16, 2025
17 checks passed
@keszybz keszybz deleted the copy-python-files branch October 16, 2025 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants