[elbe-devel] [PATCH 1/2] elbevalidate: pytest: split build_dir into normal plugin
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Fri Jun 28 10:39:07 CEST 2024
It is useful to use the elbe build dir functionality without the custom
test running. Either to specify custom parameters or when integrating it
into a larget testsuite.
Split all functionality that is useful without the testrunner into its
own plugin and import that if necessary.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
elbevalidate/_pytest_plugin.py | 28 ++++++++++++++++++++++++++++
elbevalidate/pytest.py | 33 ++++++++++-----------------------
2 files changed, 38 insertions(+), 23 deletions(-)
diff --git a/elbevalidate/_pytest_plugin.py b/elbevalidate/_pytest_plugin.py
new file mode 100644
index 000000000000..764241f44aea
--- /dev/null
+++ b/elbevalidate/_pytest_plugin.py
@@ -0,0 +1,28 @@
+import pathlib
+
+import pytest
+
+
+def _elbe_build_dir(config):
+ bd = config.option.elbe_build_dir
+ if bd is None:
+ raise ValueError('--elbe-build-dir was not specified')
+ return bd
+
+
+def pytest_addoption(parser):
+ group = parser.getgroup('elbevalidate')
+ group.addoption(
+ '--elbe-build-dir',
+ dest='elbe_build_dir',
+ )
+
+
+ at pytest.fixture
+def build_dir(request):
+ return pathlib.Path(_elbe_build_dir(request.config))
+
+
+def pytest_report_header(config):
+ bd = _elbe_build_dir(config)
+ return ['elbe build dir: ' + bd]
diff --git a/elbevalidate/pytest.py b/elbevalidate/pytest.py
index 08657f5fe076..cf98b572c082 100644
--- a/elbevalidate/pytest.py
+++ b/elbevalidate/pytest.py
@@ -1,9 +1,12 @@
+import importlib
import os
-import pathlib
import sys
import pytest
+# Don't import the plugin here as pytest will perform some import-time hooks
+plugin = importlib.util.resolve_name('.._pytest_plugin', __name__)
+
class _MainModule(pytest.Module):
def _getobj(self):
@@ -14,32 +17,16 @@ class _ElbeValidationPlugin:
def __init__(self, test_script):
self.test_script = test_script
- def pytest_addoption(self, parser):
- group = parser.getgroup('elbevalidate')
- group.addoption(
- '--elbe-build-dir',
- dest='elbe_build_dir',
- )
-
- @staticmethod
- def _elbe_build_dir(config):
- bd = config.option.elbe_build_dir
- if bd is None:
- raise ValueError('--elbe-build-dir was not specified')
- return bd
-
- @pytest.fixture(name='build_dir')
- def build_dir_fixture(self, request):
- return pathlib.Path(self._elbe_build_dir(request.config))
-
- def pytest_report_header(self, config):
- bd = self._elbe_build_dir(config)
- return ['elbe build dir: ' + bd]
-
def pytest_collect_file(self, file_path, parent):
if os.fspath(file_path) == os.fspath(self.test_script):
return _MainModule.from_parent(parent, path=file_path)
+ @pytest.hookimpl
+ def pytest_cmdline_parse(self, pluginmanager, args):
+ # May already have been registered through conftest.py
+ if not pluginmanager.has_plugin(plugin):
+ pluginmanager.register(pluginmanager.import_plugin(plugin))
+
def run_with_pytest(test_script: os.PathLike, build_dir: os.PathLike):
"""
--
2.45.2
More information about the elbe-devel
mailing list