From 3e80722d9475831eed76db31a63fccdb1162cf10 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Fri, 14 Jan 2022 13:35:16 +0100 Subject: [PATCH] More tests for the writer module --- sigal/gallery.py | 10 +++++---- sigal/utils.py | 12 +++++++---- sigal/writer.py | 3 +-- tests/test_gallery.py | 48 +++++++++++++++++++++++++++++-------------- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/sigal/gallery.py b/sigal/gallery.py index 16b0a20..9319b82 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -310,12 +310,14 @@ class Video(Media): """The date from the Date metadata if available, or from the file date.""" if 'date' in self.meta: try: - self.logger.debug("Reading date from image metadata : %s", - self.src_filename) + self.logger.debug( + "Reading date from image metadata : %s", self.src_filename + ) return datetime.fromisoformat(self.meta['date'][0]) except Exception: - self.logger.debug("Reading date from image metadata failed : %s", - self.src_filename) + self.logger.debug( + "Reading date from image metadata failed : %s", self.src_filename + ) # If no date is found in the metadata, return the file date. return self._get_file_date() diff --git a/sigal/utils.py b/sigal/utils.py index c939637..1bbfa97 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -84,10 +84,14 @@ def read_markdown(filename): text = f.read() if MD is None: - MD = Markdown(extensions=['markdown.extensions.extra', - 'markdown.extensions.meta', - 'markdown.extensions.tables'], - output_format='html5') + MD = Markdown( + extensions=[ + 'markdown.extensions.extra', + 'markdown.extensions.meta', + 'markdown.extensions.tables', + ], + output_format='html5', + ) else: MD.reset() # When https://github.com/Python-Markdown/markdown/pull/672 diff --git a/sigal/writer.py b/sigal/writer.py index 789c791..5358b09 100644 --- a/sigal/writer.py +++ b/sigal/writer.py @@ -117,8 +117,7 @@ class AbstractWriter: if os.path.isdir(self.theme_path): shutil.rmtree(self.theme_path) - copytree(os.path.join(THEMES_PATH, 'default', 'static'), - self.theme_path) + copytree(os.path.join(THEMES_PATH, 'default', 'static'), self.theme_path) copytree(os.path.join(self.theme, 'static'), self.theme_path) if self.settings["user_css"]: diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 6b1c355..3efc9aa 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -1,6 +1,7 @@ import datetime import logging import os +import re from os.path import join import pytest @@ -273,17 +274,20 @@ def test_medias_sort(settings): ] -def test_gallery(settings, tmpdir): +def test_gallery(settings, tmp_path, caplog): "Test the Gallery class." - with open(str(tmpdir.join('my.css')), mode='w') as f: - f.write('color: red') - - settings['destination'] = str(tmpdir) - settings['user_css'] = str(tmpdir.join('my.css')) + settings['destination'] = str(tmp_path) + settings['user_css'] = str(tmp_path / 'my.css') settings['webm_options'] = ['-missing-option', 'foobar'] - gal = Gallery(settings, ncpu=1) + + gal.build() + assert re.match(r'CSS file .* could not be found', caplog.records[3].message) + + with open(tmp_path / 'my.css', mode='w') as f: + f.write('color: red') + gal.build() mycss = os.path.join(settings['destination'], 'static', 'my.css') @@ -308,27 +312,41 @@ def test_gallery(settings, tmpdir): logger.setLevel(logging.INFO) -def test_custom_theme(settings, tmp_path): +def test_custom_theme(settings, tmp_path, caplog): theme_path = tmp_path / 'mytheme' tpl_path = theme_path / 'templates' + + 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) + + with pytest.raises(Exception, match='Impossible to find the theme'): + gal.build() + tpl_path.mkdir(parents=True) (theme_path / 'static').mkdir(parents=True) + with pytest.raises(SystemExit): + gal.build() + assert caplog.records[0].message.startswith( + 'The template album.html was not found in template folder' + ) + 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(""" + 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()