Files
sigal/tests/test_compress_assets_plugin.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.7 KiB
Python
Raw Normal View History

import os
import sys
from unittest import mock
import pytest
from sigal.gallery import Gallery
from sigal.plugins import compress_assets
2023-07-15 12:58:10 +02:00
from sigal.utils import init_plugins
CURRENT_DIR = os.path.dirname(__file__)
def make_gallery(settings, tmpdir, method):
2023-02-12 17:31:08 +01:00
settings["destination"] = str(tmpdir)
# Really speed up testing
2023-02-12 17:31:08 +01:00
settings["use_orig"] = True
if "sigal.plugins.compress_assets" not in settings["plugins"]:
2023-02-12 17:31:08 +01:00
settings["plugins"] += ["sigal.plugins.compress_assets"]
# Set method
2023-02-12 17:31:08 +01:00
settings.setdefault("compress_assets_options", {})["method"] = method
compress_options = compress_assets.DEFAULT_SETTINGS.copy()
# The key was created by the previous setdefault if needed
2023-02-12 17:31:08 +01:00
compress_options.update(settings["compress_assets_options"])
init_plugins(settings)
gal = Gallery(settings)
gal.build()
return compress_options
2018-02-19 22:30:00 +01:00
def walk_destination(destination, suffixes, compress_suffix):
for path, dirs, files in os.walk(destination):
for file in files:
original_filename = os.path.join(path, file)
2023-02-12 17:31:08 +01:00
compressed_filename = "{}.{}".format(
2018-03-18 22:06:29 +01:00
os.path.join(path, file), compress_suffix
)
2018-02-19 22:30:00 +01:00
path_exists = os.path.exists(compressed_filename)
file_ext = os.path.splitext(file)[1][1:]
if file_ext in suffixes:
assert path_exists
2018-03-18 22:06:29 +01:00
assert (
os.stat(original_filename).st_mtime
<= os.stat(compressed_filename).st_mtime
)
2018-02-19 22:30:00 +01:00
else:
assert not path_exists
@pytest.mark.parametrize(
"method,compress_suffix,test_import",
2023-02-12 17:31:08 +01:00
[("gzip", "gz", None), ("zopfli", "gz", "zopfli.gzip"), ("brotli", "br", "brotli")],
)
2018-02-16 23:36:52 +01:00
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)
2018-02-19 22:30:00 +01:00
walk_destination(
2023-02-12 17:31:08 +01:00
settings["destination"], compress_options["suffixes"], compress_suffix
2018-02-19 22:30:00 +01:00
)
2021-07-07 00:12:08 +02:00
2018-02-19 22:26:56 +01:00
@pytest.mark.parametrize(
"method,compress_suffix,mask",
[
2023-02-12 17:31:08 +01:00
("zopfli", "gz", "zopfli.gzip"),
("brotli", "br", "brotli"),
("__does_not_exist__", "br", None),
2021-07-07 00:12:08 +02:00
],
2018-02-19 22:26:56 +01:00
)
def test_failed_compress(
disconnect_signals, settings, tmpdir, method, compress_suffix, mask
2018-02-19 22:26:56 +01:00
):
# 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(
2023-02-12 17:31:08 +01:00
settings["destination"], [], compress_suffix # No file should be compressed
)