Merge "Clean up how PatchPolicies works"
This commit is contained in:
commit
96de9ad126
@ -121,7 +121,7 @@ def patch_policies(thing_or_policies=None, legacy_only=False,
|
|||||||
class PatchPolicies(object):
|
class PatchPolicies(object):
|
||||||
"""
|
"""
|
||||||
Why not mock.patch? In my case, when used as a decorator on the class it
|
Why not mock.patch? In my case, when used as a decorator on the class it
|
||||||
seemed to patch setUp at the wrong time (i.e. in setup the global wasn't
|
seemed to patch setUp at the wrong time (i.e. in setUp the global wasn't
|
||||||
patched yet)
|
patched yet)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -168,42 +168,38 @@ class PatchPolicies(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
orig_setUp = cls.setUp
|
orig_setUp = cls.setUp
|
||||||
orig_tearDown = cls.tearDown
|
|
||||||
|
def unpatch_cleanup(cls_self):
|
||||||
|
if cls_self._policies_patched:
|
||||||
|
self.__exit__()
|
||||||
|
cls_self._policies_patched = False
|
||||||
|
|
||||||
def setUp(cls_self):
|
def setUp(cls_self):
|
||||||
self._orig_POLICIES = storage_policy._POLICIES
|
|
||||||
if not getattr(cls_self, '_policies_patched', False):
|
if not getattr(cls_self, '_policies_patched', False):
|
||||||
storage_policy._POLICIES = self.policies
|
self.__enter__()
|
||||||
self._setup_rings()
|
|
||||||
cls_self._policies_patched = True
|
cls_self._policies_patched = True
|
||||||
|
cls_self.addCleanup(unpatch_cleanup, cls_self)
|
||||||
orig_setUp(cls_self)
|
orig_setUp(cls_self)
|
||||||
|
|
||||||
def tearDown(cls_self):
|
|
||||||
orig_tearDown(cls_self)
|
|
||||||
storage_policy._POLICIES = self._orig_POLICIES
|
|
||||||
|
|
||||||
cls.setUp = setUp
|
cls.setUp = setUp
|
||||||
cls.tearDown = tearDown
|
|
||||||
|
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
def _patch_method(self, f):
|
def _patch_method(self, f):
|
||||||
@functools.wraps(f)
|
@functools.wraps(f)
|
||||||
def mywrapper(*args, **kwargs):
|
def mywrapper(*args, **kwargs):
|
||||||
self._orig_POLICIES = storage_policy._POLICIES
|
with self:
|
||||||
try:
|
|
||||||
storage_policy._POLICIES = self.policies
|
|
||||||
self._setup_rings()
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
finally:
|
|
||||||
storage_policy._POLICIES = self._orig_POLICIES
|
|
||||||
return mywrapper
|
return mywrapper
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self._orig_POLICIES = storage_policy._POLICIES
|
self._orig_POLICIES = storage_policy._POLICIES
|
||||||
storage_policy._POLICIES = self.policies
|
storage_policy._POLICIES = self.policies
|
||||||
self._setup_rings()
|
try:
|
||||||
|
self._setup_rings()
|
||||||
|
except: # noqa
|
||||||
|
self.__exit__()
|
||||||
|
raise
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
storage_policy._POLICIES = self._orig_POLICIES
|
storage_policy._POLICIES = self._orig_POLICIES
|
||||||
|
Loading…
x
Reference in New Issue
Block a user