From 36ccead469c221d83166ee91e18c3484a300fee3 Mon Sep 17 00:00:00 2001 From: A_D Date: Mon, 10 May 2021 15:29:04 +0200 Subject: [PATCH] added test for classvar logging --- tests/EDMCLogging.py/test_logging_classvar.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/EDMCLogging.py/test_logging_classvar.py diff --git a/tests/EDMCLogging.py/test_logging_classvar.py b/tests/EDMCLogging.py/test_logging_classvar.py new file mode 100644 index 00000000..9cd68b64 --- /dev/null +++ b/tests/EDMCLogging.py/test_logging_classvar.py @@ -0,0 +1,44 @@ +import sys + +sys.path += "../" # Dont ask me why for this one it breaks, it just does. +from typing import TYPE_CHECKING # noqa: E402 + +from EDMCLogging import get_plugin_logger # noqa: E402 + +if TYPE_CHECKING: + from _pytest.logging import LogCaptureFixture + +logger = get_plugin_logger('EDMCLogging.py') + + +class ClassVarLogger: + """Test class with logger attached.""" + + @classmethod + def set_logger(cls, logger): + """Set the passed logger onto the _class_.""" + ClassVarLogger.logger = logger + + +def log_stuff(msg: str): + """Wrapper logger func.""" + ClassVarLogger.logger.debug(msg) # type: ignore # its there + + +def test_class_logger(caplog: 'LogCaptureFixture'): + """ + Test that logging from a class variable doesn't explode. + + In writting a plugin that uses a class variable to hold the logger, EDMCLoggings cleverness to extract data + regarding the qualified name of a function falls flat, as a class variable does not have a qualname, and at the time + we did not check for its existence before using it. + """ + ClassVarLogger.set_logger(logger) + ClassVarLogger.logger.debug('test') # type: ignore # its there + ClassVarLogger.logger.info('test2') # type: ignore # its there + log_stuff('test3') # type: ignore # its there + + # Dont move these, it relies on the line numbres. + assert 'EDMarketConnector.EDMCLogging.py:test_logging_classvar.py:37 test' in caplog.text + assert 'EDMarketConnector.EDMCLogging.py:test_logging_classvar.py:38 test2' in caplog.text + assert 'EDMarketConnector.EDMCLogging.py:test_logging_classvar.py:25 test3' in caplog.text