From 3fe48b05f31e53432379d38171ad446c02fdf1f2 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Thu, 4 Jan 2018 00:15:47 +0100 Subject: [PATCH] more tests for plugins --- sigal/plugins/media_page.py | 3 --- sigal/plugins/nomedia.py | 12 ++++++++---- sigal/plugins/watermark.py | 19 ++++++++++--------- tests/test_cli.py | 34 +++++++++++++++++++++++++++++++++- tests/test_gallery.py | 3 ++- tests/test_nomedia_plugin.py | 27 --------------------------- tests/test_plugins.py | 31 +++++++++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 45 deletions(-) delete mode 100644 tests/test_nomedia_plugin.py create mode 100644 tests/test_plugins.py diff --git a/sigal/plugins/media_page.py b/sigal/plugins/media_page.py index 52d7c20..0152899 100644 --- a/sigal/plugins/media_page.py +++ b/sigal/plugins/media_page.py @@ -32,7 +32,6 @@ previous/next :class:`~sigal.gallery.Media` objects. """ import codecs -import logging import os from sigal import signals @@ -40,8 +39,6 @@ from sigal.writer import Writer from sigal.utils import url_from_path from sigal.pkgmeta import __url__ as sigal_link -logger = logging.getLogger(__name__) - class PageWriter(Writer): '''A writer for writing media pages, based on writer''' diff --git a/sigal/plugins/nomedia.py b/sigal/plugins/nomedia.py index e3774b9..48c7a16 100644 --- a/sigal/plugins/nomedia.py +++ b/sigal/plugins/nomedia.py @@ -60,18 +60,22 @@ def _remove_albums_with_subdirs(albums, keysToRemove, prefix=""): for keyToRemove in keysToRemove: for key in list(albums.keys()): if key.startswith(prefix + keyToRemove): - # subdirs' target directories have already been created, remove them first + # subdirs' target directories have already been created, + # remove them first try: album = albums[key] if album.medias: - os.rmdir(os.path.join(album.dst_path, album.settings['thumb_dir'])) + os.rmdir(os.path.join(album.dst_path, + album.settings['thumb_dir'])) if album.medias and album.settings['keep_orig']: - os.rmdir(os.path.join(album.dst_path, album.settings['orig_dir'])) + os.rmdir(os.path.join(album.dst_path, + album.settings['orig_dir'])) os.rmdir(album.dst_path) except OSError: - # directory was created and populated with images in a previous run => keep it + # directory was created and populated with images in a + # previous run => keep it pass # now remove the album from the surrounding album/gallery diff --git a/sigal/plugins/watermark.py b/sigal/plugins/watermark.py index 0478859..ad2ed08 100644 --- a/sigal/plugins/watermark.py +++ b/sigal/plugins/watermark.py @@ -27,21 +27,19 @@ Settings: - ``watermark``: path to the watermark image. - ``watermark_position``: the watermark position either 'scale' or 'tile' or - a 2-tuple giving the upper left corner, or - a 4-tuple defining the left, upper, right, and lower pixel coordinate, or - `None` (same as (0, 0)). - If a 4-tuple is given, the size of the pasted image must match the size of the region. + a 2-tuple giving the upper left corner, or + a 4-tuple defining the left, upper, right, and lower pixel coordinate, or + `None` (same as (0, 0)). + If a 4-tuple is given, the size of the pasted image must match the size of + the region. - ``watermark_opacity``: the watermark opacity (0.0 to 1.0). """ - import logging -from PIL import ImageDraw, Image, ImageEnhance +from PIL import Image, ImageEnhance from sigal import signals -logger = logging.getLogger(__name__) - def reduce_opacity(im, opacity): """Returns an image with reduced opacity.""" @@ -76,7 +74,8 @@ def watermark(im, mark, position, opacity=1): w = int(mark.size[0] * ratio) h = int(mark.size[1] * ratio) mark = mark.resize((w, h)) - layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2)) + layer.paste(mark, (int((im.size[0] - w) / 2), + int((im.size[1] - h) / 2))) else: layer.paste(mark, position) # composite the watermark with the layer @@ -84,6 +83,7 @@ def watermark(im, mark, position, opacity=1): def add_watermark(img, settings=None): + logger = logging.getLogger(__name__) logger.debug('Adding watermark to %r', img) mark = Image.open(settings['watermark']) position = settings.get('watermark_position', 'scale') @@ -92,6 +92,7 @@ def add_watermark(img, settings=None): def register(settings): + logger = logging.getLogger(__name__) if settings.get('watermark'): signals.img_resized.connect(add_watermark) else: diff --git a/tests/test_cli.py b/tests/test_cli.py index 46dd871..261aa52 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,11 +1,13 @@ # -*- coding: utf-8 -*- +import blinker +import io import logging import os from click.testing import CliRunner from os.path import join -from sigal import init, build, serve, set_meta +from sigal import init, build, serve, set_meta, signals TESTGAL = join(os.path.abspath(os.path.dirname(__file__)), 'sample') @@ -34,6 +36,8 @@ def test_build(tmpdir): try: result = runner.invoke(init, [config_file]) assert result.exit_code == 0 + os.symlink(join(TESTGAL, 'watermark.png'), + join(tmpdir, 'watermark.png')) os.symlink(join(TESTGAL, 'pictures', 'dir2', 'exo20101028-b-full.jpg'), join(tmpdir, 'pictures', 'exo20101028-b-full.jpg')) @@ -49,6 +53,25 @@ def test_build(tmpdir): '-n', 1, '--debug']) assert result.exit_code == 1 + with io.open(config_file) as f: + text = f.read() + + text += """ +theme = 'colorbox' +plugins = ['sigal.plugins.adjust', 'sigal.plugins.copyright', + 'sigal.plugins.watermark', 'sigal.plugins.feeds', + 'sigal.plugins.media_page' 'sigal.plugins.nomedia', + 'sigal.plugins.extended_caching'] +copyright = u"© An example copyright message" +copyright_text_font = "foobar" +watermark = "watermark.png" +watermark_position = "scale" +watermark_opacity = 0.3 +""" + + with io.open(config_file, 'w') as f: + f.write(text) + result = runner.invoke(build, ['pictures', 'build', '-n', 1, '--debug']) assert result.exit_code == 0 @@ -60,6 +83,15 @@ def test_build(tmpdir): logger = logging.getLogger('sigal') logger.handlers[:] = [] logger.setLevel(logging.INFO) + # Reset plugins + for name in dir(signals): + if not name.startswith('_'): + try: + sig = getattr(signals, name) + if isinstance(sig, blinker.Signal): + sig.receivers.clear() + except Exception: + pass def test_serve(tmpdir): diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 0e1647e..67019b1 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -225,7 +225,8 @@ def test_medias_sort(settings): settings['medias_sort_reverse'] = False a = Album('dir1/test2', settings, album['subdirs'], album['medias'], gal) a.sort_medias(settings['medias_sort_attr']) - assert [im.filename for im in a.images] == ['archlinux-kiss-1024x640.png', '21.jpg', '22.jpg'] + assert [im.filename for im in a.images] == [ + 'archlinux-kiss-1024x640.png', '21.jpg', '22.jpg'] def test_gallery(settings, tmpdir): diff --git a/tests/test_nomedia_plugin.py b/tests/test_nomedia_plugin.py deleted file mode 100644 index 0a76116..0000000 --- a/tests/test_nomedia_plugin.py +++ /dev/null @@ -1,27 +0,0 @@ -# -*- coding:utf-8 -*- - -import os - -from sigal.gallery import Gallery -from sigal import init_plugins - -CURRENT_DIR = os.path.dirname(__file__) - -def test_nomedia_plugin(settings, tmpdir): - - settings['destination'] = str(tmpdir) - if "plugins"in settings: - if not "sigal.plugins.nomedia" in settings["plugins"]: - settings['plugins'] += ["sigal.plugins.nomedia"] - else: - settings["plugins"] = ["sigal.plugins.nomedia"] - - init_plugins(settings) - gal = Gallery(settings) - gal.build() - - for path, dirs, files in os.walk(os.path.join(str(tmpdir), "nomedia")): - assert "ignore" not in path - - for file in files: - assert "ignore" not in file \ No newline at end of file diff --git a/tests/test_plugins.py b/tests/test_plugins.py new file mode 100644 index 0000000..d648ee8 --- /dev/null +++ b/tests/test_plugins.py @@ -0,0 +1,31 @@ +# -*- coding:utf-8 -*- + +import os + +from sigal.gallery import Gallery +from sigal import init_plugins + +CURRENT_DIR = os.path.dirname(__file__) + + +def test_plugins(settings, tmpdir): + + settings['destination'] = str(tmpdir) + if "sigal.plugins.nomedia" not in settings["plugins"]: + settings['plugins'] += ["sigal.plugins.nomedia"] + if "sigal.plugins.media_page" not in settings["plugins"]: + settings['plugins'] += ["sigal.plugins.media_page"] + + init_plugins(settings) + gal = Gallery(settings) + gal.build() + + out_html = os.path.join(settings['destination'], + 'dir2', 'exo20101028-b-full.jpg.html') + assert os.path.isfile(out_html) + + for path, dirs, files in os.walk(os.path.join(str(tmpdir), "nomedia")): + assert "ignore" not in path + + for file in files: + assert "ignore" not in file