From 3bb360962a3310d51c8707f1937adb1639c86699 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Fri, 13 Sep 2024 17:25:40 +0200 Subject: [PATCH 1/4] test python package attributes --- test/CMakeLists.txt | 7 +++++++ test/test_module.py | 8 ++++++++ 2 files changed, 15 insertions(+) create mode 100644 test/test_module.py diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 264780c36..f402792fe 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -117,5 +117,12 @@ if(HepMC3_FOUND AND HepPDT_FOUND ) ) endif() +add_test(NAME py_test_module COMMAND python ${CMAKE_CURRENT_LIST_DIR}/test_module.py) +set_test_env(py_test_module) +set_property(TEST py_test_module APPEND PROPERTY ENVIRONMENT + ${ENVIRONMENT} + PYTHONPATH=${PROJECT_SOURCE_DIR}/python:$ENV{PYTHONPATH} +) + add_subdirectory(utils) add_subdirectory(tools) diff --git a/test/test_module.py b/test/test_module.py new file mode 100644 index 000000000..ebc3c2c64 --- /dev/null +++ b/test/test_module.py @@ -0,0 +1,8 @@ +import edm4hep + +assert edm4hep.__version__ +assert edm4hep.__name__ == "edm4hep" +assert edm4hep.__spec__ +assert edm4hep.__path__ +assert edm4hep.__file__ +assert "utils" in dir(edm4hep) From 5b8b97010fe8e0a9c6c79bbed6453d7e19a56585 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Fri, 13 Sep 2024 16:40:48 +0200 Subject: [PATCH 2/4] add python package attributes --- python/edm4hep/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/edm4hep/__init__.py b/python/edm4hep/__init__.py index a5cef54e4..1cf2df10d 100644 --- a/python/edm4hep/__init__.py +++ b/python/edm4hep/__init__.py @@ -27,5 +27,12 @@ # Make TAB completion work for utils setattr(edm4hep, 'utils', edm4hep.utils) +# set package attributes for edm4hep +edm4hep.__version__ = __version__ +edm4hep.__name__ = __name__ +edm4hep.__spec__ = __spec__ +edm4hep.__path__ = __path__ +edm4hep.__file__ = __file__ + # Make `import edm4hep` work sys.modules['edm4hep'] = edm4hep From 223e72cbe512c58b712b3d27c4b5eb93479cdfa2 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 16 Sep 2024 09:12:40 +0200 Subject: [PATCH 3/4] test values of module attributes --- test/test_module.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/test/test_module.py b/test/test_module.py index ebc3c2c64..93018a528 100644 --- a/test/test_module.py +++ b/test/test_module.py @@ -1,8 +1,29 @@ +#!/usr/bin/env python3 + +"""Check the attributes of edm4hep package""" + import edm4hep +from pathlib import Path +from ROOT import gInterpreter + +if gInterpreter.LoadFile("edm4hep/EDM4hepVersion.h") != 0: + raise ImportError("Error while loading file edm4hep/EDM4hepVersion.h") -assert edm4hep.__version__ -assert edm4hep.__name__ == "edm4hep" -assert edm4hep.__spec__ -assert edm4hep.__path__ -assert edm4hep.__file__ +# __version__ +version = edm4hep.version.build_version +assert edm4hep.__version__ == f"{version.major}.{version.minor}.{version.patch}" +# __name__ +name = "edm4hep" +assert edm4hep.__name__ == name +# __file__ +expected_origin = str(Path(__file__).parent.parent / "python" / "edm4hep" / "__init__.py") +assert edm4hep.__file__ == expected_origin +# __path__ +expected_path = [str(Path(expected_origin).parent)] +assert edm4hep.__path__ == expected_path +# __spec__ +assert edm4hep.__spec__.name == name +assert edm4hep.__spec__.origin == expected_origin +assert edm4hep.__spec__.submodule_search_locations == expected_path +# utils assert "utils" in dir(edm4hep) From d246c46a81ed39839aae1b56f69f5a183db86306 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 16 Sep 2024 10:11:01 +0200 Subject: [PATCH 4/4] make test module pass with both build and install --- test/test_module.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/test_module.py b/test/test_module.py index 93018a528..0ff4724c5 100644 --- a/test/test_module.py +++ b/test/test_module.py @@ -3,8 +3,8 @@ """Check the attributes of edm4hep package""" import edm4hep -from pathlib import Path from ROOT import gInterpreter +from importlib.util import find_spec if gInterpreter.LoadFile("edm4hep/EDM4hepVersion.h") != 0: raise ImportError("Error while loading file edm4hep/EDM4hepVersion.h") @@ -12,18 +12,15 @@ # __version__ version = edm4hep.version.build_version assert edm4hep.__version__ == f"{version.major}.{version.minor}.{version.patch}" -# __name__ + +# import attributes name = "edm4hep" +expected_spec = find_spec(name) assert edm4hep.__name__ == name -# __file__ -expected_origin = str(Path(__file__).parent.parent / "python" / "edm4hep" / "__init__.py") -assert edm4hep.__file__ == expected_origin -# __path__ -expected_path = [str(Path(expected_origin).parent)] -assert edm4hep.__path__ == expected_path -# __spec__ -assert edm4hep.__spec__.name == name -assert edm4hep.__spec__.origin == expected_origin -assert edm4hep.__spec__.submodule_search_locations == expected_path +assert edm4hep.__name__ == expected_spec.name +assert edm4hep.__spec__ == expected_spec +assert edm4hep.__path__ == expected_spec.submodule_search_locations +assert edm4hep.__file__ == expected_spec.origin + # utils assert "utils" in dir(edm4hep)