# -*- coding:utf-8 -*-

import os
import pytest

from sigal import init_plugins
from sigal.gallery import Gallery
from sigal.plugins import compress_assets

CURRENT_DIR = os.path.dirname(__file__)


def make_gallery(settings, tmpdir, method):
    settings['destination'] = str(tmpdir)
    # Really speed up testing
    settings['use_orig'] = True
    if "sigal.plugins.compress_assets" not in settings["plugins"]:
        settings['plugins'] += ["sigal.plugins.compress_assets"]

    # Set method
    settings.setdefault('compress_assets_options', {})['method'] = method

    compress_options = compress_assets.DEFAULT_SETTINGS.copy()
    # The key was created by the previous setdefault if needed
    compress_options.update(settings['compress_assets_options'])

    init_plugins(settings)
    gal = Gallery(settings)
    gal.build()

    return compress_options


@pytest.mark.parametrize("method,compress_suffix,test_import",
                         [('gzip', 'gz', None),
                          ('zopfli', 'gz', 'zopfli.gzip'),
                          ('brotli', 'br', 'brotli')])
def test_compress(disconnect_signals, settings, tmpdir, method,
                  compress_suffix, test_import):
    if test_import:
        pytest.importorskip(test_import)

    # Compress twice to test compression skip based on mtime
    for _ in range(2):
        compress_options = make_gallery(settings, tmpdir, method)

        suffixes = compress_options['suffixes']

        for path, dirs, files in os.walk(settings['destination']):
            for file in files:
                original_filename = os.path.join(path, file)
                compressed_filename = '{}.{}'.format(os.path.join(path, file), compress_suffix)
                path_exists = os.path.exists(compressed_filename)
                file_ext = os.path.splitext(file)[1][1:]
                if file_ext in suffixes:
                    assert path_exists
                    assert os.stat(original_filename).st_mtime <= os.stat(compressed_filename).st_mtime
                else:
                    assert not path_exists
                assert path_exists if file_ext in suffixes else not path_exists


@@ -27,16 +27,27 @@
 port os%0A
+import sys%0A
 import p
@@ -203,16 +203,676 @@
 le__)%0A%0A%0A
+class ModuleMasker:%0A%0A    def __init__(self):%0A        self._modules = %5B%5D%0A%0A    def mask(self, *modules):%0A        self._modules = modules%0A        # If the module have been previously imported,%0A        # We should force a reload on next import call.%0A        for m in modules:%0A            try:%0A                del sys.modules%5Bm%5D%0A            except KeyError:%0A                pass%0A%0A    def find_module(self, fullname, path=None):%0A        if fullname in self._modules:%0A            raise ImportError%0A%0A%0A@pytest.fixture(scope='function')%0Adef mask_modules():%0A    masker = ModuleMasker()%0A    sys.meta_path.append(masker)%0A    yield masker%0A    sys.meta_path.remove(masker)%0A%0A%0A
 def make

Add a module masker fixture. This is handy to make some module disappear to test an import failure.