From 72e58f0eef05e88caae18d55fc44d6ae45bd0f59 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Wed, 3 Jan 2018 23:02:47 +0100 Subject: [PATCH] Add test for the build command --- sigal/__init__.py | 2 +- sigal/log.py | 15 +++++++++----- tests/test_cli.py | 50 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/sigal/__init__.py b/sigal/__init__.py index af754ff..6a14cf3 100644 --- a/sigal/__init__.py +++ b/sigal/__init__.py @@ -52,7 +52,7 @@ def main(): resize images, create thumbnails with some options, generate html pages. """ - pass + pass # pragma: no cover @main.command() diff --git a/sigal/log.py b/sigal/log.py index ac82406..41e229d 100644 --- a/sigal/log.py +++ b/sigal/log.py @@ -65,11 +65,16 @@ def init_logging(name, level=logging.INFO): logger = logging.getLogger(name) logger.setLevel(level) - if os.isatty(sys.stdout.fileno()) and not sys.platform.startswith('win'): - formatter = ColoredFormatter() - elif level == logging.DEBUG: - formatter = Formatter('%(levelname)s - %(message)s') - else: + try: + if os.isatty(sys.stdout.fileno()) and \ + not sys.platform.startswith('win'): + formatter = ColoredFormatter() + elif level == logging.DEBUG: + formatter = Formatter('%(levelname)s - %(message)s') + else: + formatter = Formatter('%(message)s') + except Exception: + # This fails when running tests with click (test_build) formatter = Formatter('%(message)s') handler = logging.StreamHandler() diff --git a/tests/test_cli.py b/tests/test_cli.py index 395519a..46dd871 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,11 +1,13 @@ # -*- coding: utf-8 -*- +import logging import os from click.testing import CliRunner +from os.path import join -from sigal import init -from sigal import serve -from sigal import set_meta +from sigal import init, build, serve, set_meta + +TESTGAL = join(os.path.abspath(os.path.dirname(__file__)), 'sample') def test_init(tmpdir): @@ -22,6 +24,44 @@ def test_init(tmpdir): "keep it safe.\n") +def test_build(tmpdir): + runner = CliRunner() + config_file = str(tmpdir.join('sigal.conf.py')) + tmpdir.mkdir('pictures') + tmpdir = str(tmpdir) + cwd = os.getcwd() + + try: + result = runner.invoke(init, [config_file]) + assert result.exit_code == 0 + os.symlink(join(TESTGAL, 'pictures', 'dir2', 'exo20101028-b-full.jpg'), + join(tmpdir, 'pictures', 'exo20101028-b-full.jpg')) + + result = runner.invoke(build, ['-n', 1, '--debug']) + assert result.exit_code == 1 + + os.chdir(tmpdir) + + result = runner.invoke(build, ['foo', '-n', 1, '--debug']) + assert result.exit_code == 1 + + result = runner.invoke(build, ['pictures', 'pictures/out', + '-n', 1, '--debug']) + assert result.exit_code == 1 + + result = runner.invoke(build, ['pictures', 'build', + '-n', 1, '--debug']) + assert result.exit_code == 0 + assert os.path.isfile(join(tmpdir, 'build', 'thumbnails', + 'exo20101028-b-full.jpg')) + finally: + os.chdir(cwd) + # Reset logger + logger = logging.getLogger('sigal') + logger.handlers[:] = [] + logger.setLevel(logging.INFO) + + def test_serve(tmpdir): config_file = str(tmpdir.join('sigal.conf.py')) runner = CliRunner() @@ -34,6 +74,7 @@ def test_serve(tmpdir): result = runner.invoke(serve, ['-c', config_file]) assert result.exit_code == 1 + def test_set_meta(tmpdir): testdir = tmpdir.mkdir("test") @@ -53,7 +94,8 @@ def test_set_meta(tmpdir): result = runner.invoke(set_meta, [str(testdir), "title", "testing"]) assert result.exit_code == 2 - result = runner.invoke(set_meta, [str(testdir.join("non-existant.jpg")), "title", "testing"]) + result = runner.invoke(set_meta, [str(testdir.join("non-existant.jpg")), + "title", "testing"]) assert result.exit_code == 1 result = runner.invoke(set_meta, [str(testfile), "title", "testing"])