laforge has submitted this change. ( 
https://gerrit.osmocom.org/c/pysim/+/43539?usp=email )

Change subject: tests: stop test_log from leaking the print callback
......................................................................

tests: stop test_log from leaking the print callback

PySimLogger.setup() installs a process-global print callback.
PySimLogger_Test sets one, a helper that asserts the message equals a global
expected_message, and never removes it, so from the moment test_log runs,
every PySimLogger message emitted anywhere in the process is checked against
whatever string that global happens to hold.

Fortunately unittest discovery runs modules in sorted order, and today
the PySimLogger users that log during tests all sort before test_log, so
this only breaks as soon as I try to add tests, just like anything else
breaks as soon as I try to use it.

Change-Id: I481e2c443fe0f412380b0f1acf6da5971ffca147
---
M tests/unittests/test_log.py
1 file changed, 11 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved
  dexter: Looks good to me, but someone else must approve




diff --git a/tests/unittests/test_log.py b/tests/unittests/test_log.py
index a8e38dd..ac651ae 100755
--- a/tests/unittests/test_log.py
+++ b/tests/unittests/test_log.py
@@ -37,6 +37,17 @@

 class PySimLogger_Test(unittest.TestCase):

+    def setUp(self):
+        # PySimLogger.setup() is global, so a print callback left installed 
here fires for
+        # every PySimLogger message emitted by any test module that runs later 
in the same process
+        # ... where it asserts against a stale 'expected_message' and fails a 
test that has nothing
+        # to do with logging. Great fun!
+        # Restore before each test.
+        saved = (PySimLogger.print_callback, PySimLogger.verbose)
+        def _restore():
+            PySimLogger.print_callback, PySimLogger.verbose = saved
+        self.addCleanup(_restore)
+
     def __test_01_safe_defaults_one(self, callback, message:str):
         # When log messages are sent to an unconfigured PySimLogger class, we 
expect the unmodified message being
         # logged to stdout, just as if it were printed via a normal print() 
statement.

--
To view, visit https://gerrit.osmocom.org/c/pysim/+/43539?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I481e2c443fe0f412380b0f1acf6da5971ffca147
Gerrit-Change-Number: 43539
Gerrit-PatchSet: 1
Gerrit-Owner: Hoernchen <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <[email protected]>
Gerrit-Reviewer: laforge <[email protected]>

Reply via email to