Revert "Remove unused fixtures"
Repropose this
This reverts commit e7853b8909
.
Change-Id: I7b21bb139ccf7379d6f1b26f1ecddaa43767a334
This commit is contained in:
@@ -160,3 +160,50 @@ class TestResponse(requests.Response):
|
||||
@property
|
||||
def text(self):
|
||||
return self.content
|
||||
|
||||
|
||||
class DisableModuleFixture(fixtures.Fixture):
|
||||
"""A fixture to provide support for unloading/disabling modules."""
|
||||
|
||||
def __init__(self, module, *args, **kw):
|
||||
super(DisableModuleFixture, self).__init__(*args, **kw)
|
||||
self.module = module
|
||||
self._finders = []
|
||||
self._cleared_modules = {}
|
||||
|
||||
def tearDown(self):
|
||||
super(DisableModuleFixture, self).tearDown()
|
||||
for finder in self._finders:
|
||||
sys.meta_path.remove(finder)
|
||||
sys.modules.update(self._cleared_modules)
|
||||
|
||||
def clear_module(self):
|
||||
cleared_modules = {}
|
||||
for fullname in sys.modules.keys():
|
||||
if (fullname == self.module or
|
||||
fullname.startswith(self.module + '.')):
|
||||
cleared_modules[fullname] = sys.modules.pop(fullname)
|
||||
return cleared_modules
|
||||
|
||||
def setUp(self):
|
||||
"""Ensure ImportError for the specified module."""
|
||||
|
||||
super(DisableModuleFixture, self).setUp()
|
||||
|
||||
# Clear 'module' references in sys.modules
|
||||
self._cleared_modules.update(self.clear_module())
|
||||
|
||||
finder = NoModuleFinder(self.module)
|
||||
self._finders.append(finder)
|
||||
sys.meta_path.insert(0, finder)
|
||||
|
||||
|
||||
class NoModuleFinder(object):
|
||||
"""Disallow further imports of 'module'."""
|
||||
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
def find_module(self, fullname, path):
|
||||
if fullname == self.module or fullname.startswith(self.module + '.'):
|
||||
raise ImportError
|
||||
|
Reference in New Issue
Block a user