From 99c7af208b5da797b0499a590ed2c908b60a7d07 Mon Sep 17 00:00:00 2001 From: Glandos Date: Sun, 11 Mar 2018 21:08:55 +0100 Subject: [PATCH] Remove custom code to mask modules, use builtin features. Note that it uses unittest.mock that is available from Python 3.3 --- tests/test_compress_assets_plugin.py | 53 ++++++---------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/tests/test_compress_assets_plugin.py b/tests/test_compress_assets_plugin.py index f1173ad..5a34383 100644 --- a/tests/test_compress_assets_plugin.py +++ b/tests/test_compress_assets_plugin.py @@ -2,6 +2,9 @@ import os import sys + +from unittest import mock + import pytest from sigal import init_plugins @@ -11,43 +14,6 @@ from sigal.plugins import compress_assets CURRENT_DIR = os.path.dirname(__file__) -class ErrorLoader: - def load_module(self, fullname): - raise ImportError - - -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 globals()[m] - except KeyError: - pass - try: - del sys.modules[m] - except KeyError: - pass - - def find_module(self, fullname, path=None): - if fullname in self._modules: - return ErrorLoader() - - -@pytest.fixture(scope='function') -def mask_modules(): - masker = ModuleMasker() - sys.meta_path.insert(0, masker) - yield masker - sys.meta_path.remove(masker) - - def make_gallery(settings, tmpdir, method): settings['destination'] = str(tmpdir) # Really speed up testing @@ -105,10 +71,11 @@ def test_compress(disconnect_signals, settings, tmpdir, method, [('zopfli', 'gz', 'zopfli.gzip'), ('brotli', 'br', 'brotli'), ('__does_not_exist__', 'br', None)]) -def test_failed_compress(mask_modules, disconnect_signals, settings, tmpdir, +def test_failed_compress(disconnect_signals, settings, tmpdir, method, compress_suffix, mask): - mask_modules.mask(mask) - make_gallery(settings, tmpdir, method) - walk_destination(settings['destination'], - [], # No file should be compressed - compress_suffix) + # See https://medium.com/python-pandemonium/how-to-test-your-imports-1461c1113be1 + with mock.patch.dict(sys.modules, {mask: None}): + make_gallery(settings, tmpdir, method) + walk_destination(settings['destination'], + [], # No file should be compressed + compress_suffix)