From 8a52667118bf580a6dd2a693109f922b6cb61cd2 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Fri, 14 Jan 2022 09:36:14 +0100 Subject: [PATCH] Add test for filters (ref #452) --- sigal/writer.py | 1 + tests/test_gallery.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/sigal/writer.py b/sigal/writer.py index 8d79822..789c791 100644 --- a/sigal/writer.py +++ b/sigal/writer.py @@ -93,6 +93,7 @@ class AbstractWriter: # handle optional filters.py filters_py = os.path.join(self.theme, "filters.py") if os.path.exists(filters_py): + self.logger.info('Loading filters file: %s', filters_py) module_spec = importlib.util.spec_from_file_location('filters', filters_py) mod = importlib.util.module_from_spec(module_spec) sys.modules['filters'] = mod diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 1135574..6b1c355 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -308,6 +308,40 @@ def test_gallery(settings, tmpdir): logger.setLevel(logging.INFO) +def test_custom_theme(settings, tmp_path): + + theme_path = tmp_path / 'mytheme' + tpl_path = theme_path / 'templates' + tpl_path.mkdir(parents=True) + (theme_path / 'static').mkdir(parents=True) + + with open(tpl_path / 'album.html', mode='w') as f: + f.write(""" {{ settings.title|myfilter }} """) + with open(tpl_path / 'album_list.html', mode='w') as f: + f.write(""" {{ settings.title|myfilter }} """) + with open(theme_path / 'filters.py', mode='w') as f: + f.write(""" +def myfilter(value): + return f'{value} is very nice' +""") + + settings['destination'] = str(tmp_path / 'build') + settings['source'] = os.path.join(settings['source'], 'encryptTest') + settings['theme'] = str(theme_path) + settings['title'] = 'My gallery' + + gal = Gallery(settings, ncpu=1) + gal.build() + + out_html = os.path.join(settings['destination'], 'index.html') + assert os.path.isfile(out_html) + + with open(out_html) as f: + html = f.read() + + assert 'My gallery is very nice' in html + + def test_gallery_max_img_pixels(settings, tmpdir, monkeypatch): "Test the Gallery class with the max_img_pixels setting." # monkeypatch is used here to reset the value to the PIL default.