From 9a9b4bb232549c0802c74593454e2451a62d9e3d Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Fri, 12 Feb 2021 10:14:23 -0500 Subject: [PATCH] Use a monkey patching library for monkey patching --- src/allmydata/test/eliotutil.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/allmydata/test/eliotutil.py b/src/allmydata/test/eliotutil.py index f91d76e03..2a5c85ebf 100644 --- a/src/allmydata/test/eliotutil.py +++ b/src/allmydata/test/eliotutil.py @@ -36,6 +36,10 @@ from eliot.testing import ( check_for_errors, ) +from twisted.python.monkey import ( + MonkeyPatcher, +) + from ..util.jsonbytes import BytesJSONEncoder @@ -115,13 +119,16 @@ class EliotLoggedRunTest(object): # and things do not go well when we add too much extra behavior here. # For example, exceptions raised here often just kill the whole # runner. + patcher = MonkeyPatcher() # So, grab the test method. name = self.case._testMethodName original = getattr(self.case, name) + decorated = with_logging(ensure_text(self.case.id()), original) + patcher.addPatch(self.case, name, decorated) try: - # Patch in a decorated version of it. - setattr(self.case, name, with_logging(ensure_text(self.case.id()), original)) + # Patch it in + patcher.patch() # Then use the rest of the machinery to run it. return self._run_tests_with_factory( self.case, @@ -130,7 +137,7 @@ class EliotLoggedRunTest(object): ).run(result) finally: # Clean up the patching for idempotency or something. - delattr(self.case, name) + patcher.restore() def with_logging(