gtg icon indicating copy to clipboard operation
gtg copied to clipboard

GHA: Get pytest to run tests again

Open picsel2 opened this issue 1 year ago • 1 comments

This is a follow up on #999

The goal is to get pytest running again. Most tests fail spectacularly but at least we know know which are yet to be fixed. 🙌

picsel2 avatar Mar 01 '24 00:03 picsel2

It doesn't block the current changes, but I'm still getting import failures when running the tests locally:

$ ./run-tests 
==================================================================== test session starts =====================================================================
platform linux -- Python 3.11.6, pytest-8.0.1, pluggy-1.4.0
rootdir: /home/kevin/src/gtg
configfile: pyproject.toml
collected 151 items / 3 errors                                                                                                                               

=========================================================================== ERRORS ===========================================================================
___________________________________________________ ERROR collecting tests/backend/backend_caldav_test.py ____________________________________________________
ImportError while importing test module '/home/kevin/src/gtg/tests/backend/backend_caldav_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/backend/backend_caldav_test.py:11: in <module>
    from GTG.core.datastore import Datastore
GTG/core/datastore.py:37: in <module>
    import GTG.core.info as info
E   ModuleNotFoundError: No module named 'GTG.core.info'

(and 2 more) It seems to not like the fact that the extension of https://github.com/getting-things-gnome/gtg/blob/master/GTG/core/info.py.in is .py.in maybe?

~It might be a Python 3.11 thing - I haven't taken the time to install 3.8 locally.~ Nope, confirmed not a Python 3.11 thing - it fails the same way on Python 3.8.

Anyway, I can fix that separately.

SqAtx avatar Mar 02 '24 19:03 SqAtx

tldr

Install the project in a prefix of your choice using meson and add the directory with the installed package to PYTHONPATH. Run the tests with PYTHONSAFEPATH=1 PYTHONPATH="${prefix}/lib/python3.11/site-packages" ./run-tests

The lenghty explanation

Yeah... running locally is somewhat more complicated since GTG uses Meson to build its sources.

Configuring info.py

The file info.py.in is a template that gets filled (also called configured) by the meson setup step. The resulting file is placed into the build directory. This is the file we actually want Python to find and use.

Installing the project

Next we have to tell Python of the extra location to search for. Unfortunately it is not enough to just add the build directory to PYTHONPATH.

The sources currently are a non-namespace package (PEP420). This means here that we need all sources to be merged in one location since Python stops searching at the first GTG directory found, even if it does not contain a info.py file.

So instead we do a meson install and use the installed location in PYTHONPATH.

Disabling implicit search paths

You now might notice that Python still picks up the git sources, although PYTHONPATH points to the installed directory. The reason is that Python silently adds the current working directory in front sys.path, rendering our previous efforts useless. But you are in luck because Python 3.11 added a new flag that you can enable by setting PYTHONSAFEPATH=1. It disables the implicit addition of the current directory to the search path. Exactly what we need!

picsel2 avatar Mar 05 '24 14:03 picsel2

Thank you for working on this! Great to see the checks green again :)

diegogangl avatar Apr 01 '24 18:04 diegogangl