From 2d8f32cab48a3932cc754839c6598058dcc3b5dc Mon Sep 17 00:00:00 2001 From: Glandos Date: Mon, 19 Feb 2018 22:25:20 +0100 Subject: [PATCH] Add a module masker fixture. This is handy to make some module disappear to test an import failure. --- tests/test_compress_assets_plugin.py | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_compress_assets_plugin.py b/tests/test_compress_assets_plugin.py index 7d06b79..fef979f 100644 --- a/tests/test_compress_assets_plugin.py +++ b/tests/test_compress_assets_plugin.py @@ -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