Add a module masker fixture.

This is handy to make some module disappear to test an import failure.
This commit is contained in:
Glandos
2018-02-19 22:25:20 +01:00
parent 43139092ab
commit 2d8f32cab4

View File

@@ -1,6 +1,7 @@
# -*- coding:utf-8 -*-
import os
import sys
import pytest
from sigal import init_plugins
@@ -10,6 +11,34 @@ from sigal.plugins import compress_assets
CURRENT_DIR = os.path.dirname(__file__)
class ModuleMasker:
def __init__(self):
self._modules = []
def mask(self, *modules):
self._modules = modules
# If the module have been previously imported,
# We should force a reload on next import call.
for m in modules:
try:
del sys.modules[m]
except KeyError:
pass
def find_module(self, fullname, path=None):
if fullname in self._modules:
raise ImportError
@pytest.fixture(scope='function')
def mask_modules():
masker = ModuleMasker()
sys.meta_path.append(masker)
yield masker
sys.meta_path.remove(masker)
def make_gallery(settings, tmpdir, method):
settings['destination'] = str(tmpdir)
# Really speed up testing