Files
sigal/tests/test_cli.py

140 lines
4.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-03 23:02:47 +01:00
import logging
import os
2014-07-22 23:59:53 +02:00
from click.testing import CliRunner
2018-01-03 23:02:47 +01:00
from os.path import join
2014-07-22 23:59:53 +02:00
2018-02-16 23:36:52 +01:00
from sigal import init, build, serve, set_meta
2018-01-03 23:02:47 +01:00
TESTGAL = join(os.path.abspath(os.path.dirname(__file__)), 'sample')
def test_init(tmpdir):
config_file = str(tmpdir.join('sigal.conf.py'))
2014-07-22 23:59:53 +02:00
runner = CliRunner()
result = runner.invoke(init, [config_file])
assert result.exit_code == 0
assert result.output.startswith('Sample config file created:')
assert os.path.isfile(config_file)
2014-07-22 23:59:53 +02:00
result = runner.invoke(init, [config_file])
assert result.exit_code == 1
assert result.output == ("Found an existing config file, will abort to "
"keep it safe.\n")
2015-08-07 00:35:07 +02:00
2018-02-16 23:36:52 +01:00
def test_build(tmpdir, disconnect_signals):
2018-01-03 23:02:47 +01:00
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
2018-01-04 00:15:47 +01:00
os.symlink(join(TESTGAL, 'watermark.png'),
join(tmpdir, 'watermark.png'))
2018-01-03 23:02:47 +01:00
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
2018-03-05 00:45:41 +01:00
with open(config_file) as f:
2018-01-04 00:15:47 +01:00
text = f.read()
text += """
theme = 'colorbox'
2018-01-07 00:59:41 +01:00
files_to_copy = (('../watermark.png', 'watermark.png'),)
2018-01-04 00:15:47 +01:00
plugins = ['sigal.plugins.adjust', 'sigal.plugins.copyright',
'sigal.plugins.watermark', 'sigal.plugins.feeds',
'sigal.plugins.media_page' 'sigal.plugins.nomedia',
'sigal.plugins.extended_caching']
2018-01-05 00:32:13 +01:00
copyright = "An example copyright message"
2018-01-04 00:15:47 +01:00
copyright_text_font = "foobar"
watermark = "watermark.png"
watermark_position = "scale"
watermark_opacity = 0.3
2018-01-07 00:38:35 +01:00
rss_feed = {'feed_url': 'http://example.org/feed.rss', 'nb_items': 10}
atom_feed = {'feed_url': 'http://example.org/feed.atom', 'nb_items': 10}
2018-01-04 00:15:47 +01:00
"""
2018-03-05 00:45:41 +01:00
with open(config_file, 'w') as f:
2018-01-04 00:15:47 +01:00
f.write(text)
2018-01-03 23:02:47 +01:00
result = runner.invoke(build, ['pictures', 'build',
2018-01-07 00:59:41 +01:00
'--title', 'Testing build',
2018-01-03 23:02:47 +01:00
'-n', 1, '--debug'])
assert result.exit_code == 0
assert os.path.isfile(join(tmpdir, 'build', 'thumbnails',
'exo20101028-b-full.jpg'))
2018-01-07 00:38:35 +01:00
assert os.path.isfile(join(tmpdir, 'build', 'feed.atom'))
assert os.path.isfile(join(tmpdir, 'build', 'feed.rss'))
2018-01-07 00:59:41 +01:00
assert os.path.isfile(join(tmpdir, 'build', 'watermark.png'))
2018-01-03 23:02:47 +01:00
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()
result = runner.invoke(init, [config_file])
assert result.exit_code == 0
result = runner.invoke(serve)
assert result.exit_code == 2
result = runner.invoke(serve, ['-c', config_file])
assert result.exit_code == 1
2018-01-03 23:02:47 +01:00
def test_set_meta(tmpdir):
testdir = tmpdir.mkdir("test")
testfile = tmpdir.join("test.jpg")
testfile.write("")
runner = CliRunner()
result = runner.invoke(set_meta, [str(testdir), "title", "testing"])
assert result.exit_code == 0
assert result.output.startswith("1 metadata key(s) written to")
assert os.path.isfile(str(testdir.join("index.md")))
assert testdir.join("index.md").read() == "Title: testing\n"
# Run again, should give file exists error
result = runner.invoke(set_meta, [str(testdir), "title", "testing"])
assert result.exit_code == 2
2018-01-03 23:02:47 +01:00
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"])
assert result.exit_code == 0
assert result.output.startswith("1 metadata key(s) written to")
assert os.path.isfile(str(tmpdir.join("test.md")))
assert tmpdir.join("test.md").read() == "Title: testing\n"
2018-01-07 00:59:41 +01:00
result = runner.invoke(set_meta, [str(testfile), "title"])
assert result.exit_code == 1
assert result.output.startswith("Need an even number of arguments")