Files
sigal/tests/test_cli.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
4.5 KiB
Python
Raw Normal View History

2018-01-03 23:02:47 +01:00
import logging
import os
2018-01-03 23:02:47 +01:00
from os.path import join
2014-07-22 23:59:53 +02:00
2020-03-15 22:58:03 -03:00
from click.testing import CliRunner
from sigal import build, init, serve, set_meta
2018-01-03 23:02:47 +01:00
2023-02-12 17:31:08 +01:00
TESTGAL = join(os.path.abspath(os.path.dirname(__file__)), "sample")
def test_init(tmpdir):
2023-02-12 17:31:08 +01:00
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
2023-02-12 17:31:08 +01:00
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()
2023-02-12 17:31:08 +01:00
config_file = str(tmpdir.join("sigal.conf.py"))
tmpdir.mkdir("pictures")
2018-01-03 23:02:47 +01:00
tmpdir = str(tmpdir)
cwd = os.getcwd()
try:
result = runner.invoke(init, [config_file])
assert result.exit_code == 0
2023-02-12 17:31:08 +01:00
os.symlink(join(TESTGAL, "watermark.png"), join(tmpdir, "watermark.png"))
os.symlink(
2023-02-12 17:31:08 +01:00
join(TESTGAL, "pictures", "dir2", "KeckObservatory20071020.jpg"),
join(tmpdir, "pictures", "KeckObservatory20071020.jpg"),
)
2018-01-03 23:02:47 +01:00
2023-02-12 17:31:08 +01:00
result = runner.invoke(build, ["-n", 1, "--debug"])
2018-01-03 23:02:47 +01:00
assert result.exit_code == 1
os.chdir(tmpdir)
2023-02-12 17:31:08 +01:00
result = runner.invoke(build, ["foo", "-n", 1, "--debug"])
2018-01-03 23:02:47 +01:00
assert result.exit_code == 1
2023-02-12 17:31:08 +01:00
result = runner.invoke(build, ["pictures", "pictures/out", "-n", 1, "--debug"])
2018-01-03 23:02:47 +01:00
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
"""
2023-02-12 17:31:08 +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(
2023-02-12 17:31:08 +01:00
build, ["pictures", "build", "--title", "Testing build", "-n", 1, "--debug"]
2018-01-03 23:02:47 +01:00
)
assert result.exit_code == 0
assert os.path.isfile(
2023-02-12 17:31:08 +01:00
join(tmpdir, "build", "thumbnails", "KeckObservatory20071020.jpg")
)
2023-02-12 17:31:08 +01:00
assert os.path.isfile(join(tmpdir, "build", "feed.atom"))
assert os.path.isfile(join(tmpdir, "build", "feed.rss"))
assert os.path.isfile(join(tmpdir, "build", "watermark.png"))
2018-01-03 23:02:47 +01:00
finally:
os.chdir(cwd)
# Reset logger
2023-02-12 17:31:08 +01:00
logger = logging.getLogger("sigal")
2018-01-03 23:02:47 +01:00
logger.handlers[:] = []
logger.setLevel(logging.INFO)
def test_serve(tmpdir):
2023-02-12 17:31:08 +01:00
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
2023-02-12 17:31:08 +01:00
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")