From d0a6f79ac5da0049e353e8fb26af7fe5b382aa06 Mon Sep 17 00:00:00 2001 From: Jan Remmet Date: Tue, 13 Aug 2024 14:37:14 +0200 Subject: [PATCH] labgrid: pytestplugin: hooks: fix multiple start stop for StepLogger pytests pytester can be used to run pytests inside pytest. This results in nested calls of pytest_configure and so multiple starts and stops of the StepLogger. Only start StepLogger if it is not already active. This solution will run the cleanup hook in the "outer" pytest instance. Signed-off-by: Jan Remmet --- labgrid/logging.py | 10 +++++----- labgrid/pytestplugin/hooks.py | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/labgrid/logging.py b/labgrid/logging.py index a3807ecf1..215f0f8f8 100644 --- a/labgrid/logging.py +++ b/labgrid/logging.py @@ -127,7 +127,7 @@ def flush(self): class StepLogger: - _started = False + started = False _logger = None _serial_logger = None _length_limit = 100 @@ -144,21 +144,21 @@ def __attrs_post_init__(self): @classmethod def start(cls, length_limit=None): """starts the StepLogger""" - assert not cls._started + assert not cls.started if cls._logger is None: cls._logger = logging.getLogger("StepLogger") steps.subscribe(cls.notify) cls._serial_logger = SerialLoggingReporter() - cls._started = True + cls.started = True if length_limit is not None: cls._length_limit = length_limit @classmethod def stop(cls): """stops the StepLogger""" - assert cls._started + assert cls.started steps.unsubscribe(cls.notify) - cls._started = False + cls.started = False @classmethod def get_prefix(cls, event): diff --git a/labgrid/pytestplugin/hooks.py b/labgrid/pytestplugin/hooks.py index a701e5ccb..84a1924ae 100644 --- a/labgrid/pytestplugin/hooks.py +++ b/labgrid/pytestplugin/hooks.py @@ -62,8 +62,9 @@ def configure_pytest_logging(config, plugin): @pytest.hookimpl(trylast=True) def pytest_configure(config): - StepLogger.start() - config.add_cleanup(StepLogger.stop) + if not StepLogger.started: + StepLogger.start() + config.add_cleanup(StepLogger.stop) logging_plugin = config.pluginmanager.getplugin('logging-plugin') if logging_plugin: