Files
sigal/tests/test_zip.py

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

75 lines
2.2 KiB
Python
Raw Normal View History

2020-03-15 22:58:03 -03:00
import os
2013-07-30 09:00:38 +02:00
import zipfile
from sigal import init_plugins
2013-07-30 09:00:38 +02:00
from sigal.gallery import Gallery
from sigal.settings import read_settings
CURRENT_DIR = os.path.dirname(__file__)
2023-02-12 17:31:08 +01:00
SAMPLE_DIR = os.path.join(CURRENT_DIR, "sample")
SAMPLE_SOURCE = os.path.join(SAMPLE_DIR, "pictures")
2013-07-30 09:00:38 +02:00
2023-02-12 17:31:08 +01:00
def make_gallery(source_dir="dir1", **kwargs):
default_conf = os.path.join(SAMPLE_DIR, "sigal.conf.py")
2013-07-30 09:00:38 +02:00
settings = read_settings(default_conf)
2023-02-12 17:31:08 +01:00
settings["source"] = os.path.join(SAMPLE_SOURCE, source_dir)
2013-07-30 09:00:38 +02:00
settings.update(kwargs)
init_plugins(settings)
2013-12-18 22:16:23 +01:00
return Gallery(settings, ncpu=1)
2013-07-30 09:00:38 +02:00
def test_zipped_correctly(tmpdir):
outpath = str(tmpdir)
2023-02-12 17:31:08 +01:00
gallery = make_gallery(destination=outpath, zip_gallery="archive.zip")
2013-07-30 09:00:38 +02:00
gallery.build()
2023-02-12 17:31:08 +01:00
zipf = os.path.join(outpath, "test1", "archive.zip")
2019-08-26 01:05:27 +02:00
assert os.path.isfile(zipf)
2013-07-30 09:00:38 +02:00
2023-02-12 17:31:08 +01:00
zip_file = zipfile.ZipFile(zipf, "r")
expected = (
2023-02-12 17:31:08 +01:00
"11.jpg",
"CMB_Timeline300_no_WMAP.jpg",
"flickr_jerquiaga_2394751088_cc-by-nc.jpg",
"example.gif",
)
2013-07-30 09:00:38 +02:00
for filename in zip_file.namelist():
assert filename in expected
zip_file.close()
2023-02-12 17:31:08 +01:00
assert os.path.isfile(os.path.join(outpath, "test2", "archive.zip"))
2019-08-26 01:05:27 +02:00
def test_not_zipped(tmpdir):
# test that the zip file is not created when the .nozip_gallery file
# is present
outpath = str(tmpdir)
gallery = make_gallery(
2023-02-12 17:31:08 +01:00
destination=outpath, zip_gallery="archive.zip", source_dir="dir2"
2019-08-26 01:05:27 +02:00
)
gallery.build()
2023-02-12 17:31:08 +01:00
assert not os.path.isfile(os.path.join(outpath, "archive.zip"))
2013-07-30 09:00:38 +02:00
def test_no_archive(tmpdir):
outpath = str(tmpdir)
2019-08-26 01:05:27 +02:00
gallery = make_gallery(destination=outpath, zip_gallery=False)
2013-07-30 09:00:38 +02:00
gallery.build()
2023-02-12 17:31:08 +01:00
assert not os.path.isfile(os.path.join(outpath, "test1", "archive.zip"))
assert not os.path.isfile(os.path.join(outpath, "test2", "archive.zip"))
def test_correct_filename(tmpdir, caplog):
2023-02-12 17:31:08 +01:00
caplog.set_level("ERROR")
outpath = str(tmpdir)
gallery = make_gallery(destination=outpath, zip_gallery=True)
gallery.build()
assert caplog.records[0].message == "'zip_gallery' should be set to a filename"
2023-02-12 17:31:08 +01:00
assert not os.path.isfile(os.path.join(outpath, "test1", "archive.zip"))
assert not os.path.isfile(os.path.join(outpath, "test2", "archive.zip"))