Files
sigal/tests/test_utils.py

130 lines
3.9 KiB
Python
Raw Normal View History

import os
from pathlib import Path
2020-03-15 22:58:03 -03:00
from sigal import utils
CURRENT_DIR = os.path.dirname(__file__)
2023-02-12 17:31:08 +01:00
SAMPLE_DIR = os.path.join(CURRENT_DIR, "sample")
def test_copy(tmpdir):
2023-02-12 17:31:08 +01:00
filename = "KeckObservatory20071020.jpg"
src = os.path.join(SAMPLE_DIR, "pictures", "dir2", filename)
dst = str(tmpdir.join(filename))
utils.copy(src, dst)
assert os.path.isfile(dst)
2023-02-12 17:31:08 +01:00
filename = "m57_the_ring_nebula-587px.jpg"
src = os.path.join(SAMPLE_DIR, "pictures", "dir2", filename)
dst = str(tmpdir.join(filename))
utils.copy(src, dst, symlink=True)
assert os.path.islink(dst)
assert os.readlink(dst) == src
2023-02-12 17:31:08 +01:00
filename = "KeckObservatory20071020.jpg"
src = os.path.join(SAMPLE_DIR, "pictures", "dir2", filename)
utils.copy(src, dst, symlink=True)
assert os.path.islink(dst)
assert os.readlink(dst) == src
2023-02-12 17:31:08 +01:00
filename = "KeckObservatory20071020.jpg"
src = os.path.join(SAMPLE_DIR, "pictures", "dir2", filename)
2019-02-01 13:42:56 -05:00
dst = str(tmpdir.join(filename))
2019-02-01 12:43:52 -05:00
utils.copy(src, dst, symlink=True, rellink=True)
assert os.path.islink(dst)
2019-02-01 13:42:56 -05:00
assert os.path.join(os.path.dirname(CURRENT_DIR)), os.readlink(dst) == src
# get absolute path of the current dir plus the relative dir
2023-02-12 17:31:08 +01:00
src = str(tmpdir.join("foo.txt"))
dst = str(tmpdir.join("bar.txt"))
p = Path(src)
p.touch()
p.chmod(0o444)
utils.copy(src, dst)
utils.copy(src, dst)
2023-07-15 12:58:10 +02:00
def test_force(tmpdir):
2023-07-15 12:58:10 +02:00
assert utils.should_reprocess_album("Gallery/New Pics", "New Pics", False) is False
assert utils.should_reprocess_album("Gallery/New Pics", "New Pics", True) is True
assert (
utils.should_reprocess_album("Gallery/New Pics", "New Pics", ["Gallery/*"])
is True
)
assert (
utils.should_reprocess_album("Gallery/New Pics", "New Pics", ["Gallery/*Pics"])
is True
)
assert (
utils.should_reprocess_album("Gallery/New Pics", "New Pics", ["Pictures/*"])
is False
)
assert (
utils.should_reprocess_album("Gallery/New Pics", "New Pics", ["New Pics"])
is True
)
assert (
utils.should_reprocess_album("Gallery/New Pics", "New Pics", ["Pictures"])
is False
)
assert (
utils.should_reprocess_album(
"Gallery/New Pics", "New Pics", ["Pictures", "Something"]
)
is False
)
assert (
utils.should_reprocess_album(
"Gallery/New Pics", "New Pics", ["Pictures", "Gallery", "*Pics"]
)
is True
)
def test_check_or_create_dir(tmpdir):
2023-02-12 17:31:08 +01:00
path = str(tmpdir.join("new_directory"))
utils.check_or_create_dir(path)
assert os.path.isdir(path)
2014-03-25 23:27:53 +01:00
def test_url_from_path():
2023-02-12 17:31:08 +01:00
assert utils.url_from_path(os.sep.join(["foo", "bar"])) == "foo/bar"
2014-07-20 23:35:28 +02:00
def test_url_from_windows_path(monkeypatch):
2023-02-12 17:31:08 +01:00
monkeypatch.setattr("os.sep", "\\")
path = os.sep.join(["foo", "bar"])
assert path == r"foo\bar"
assert utils.url_from_path(path) == "foo/bar"
2014-07-20 23:35:28 +02:00
def test_read_markdown():
2023-02-12 17:31:08 +01:00
src = os.path.join(SAMPLE_DIR, "pictures", "dir1", "test1", "11.md")
m = utils.read_markdown(src)
2023-02-12 17:31:08 +01:00
assert m["title"] == "Foo Bar"
assert m["meta"]["location"][0] == "Bavaria"
assert m["description"] == "<p>This is a funny description of this image</p>"
2014-07-21 00:00:13 +02:00
def test_read_markdown_empty_file(tmpdir):
src = tmpdir.join("file.txt")
src.write("content")
m = utils.read_markdown(str(src))
2023-02-12 17:31:08 +01:00
assert "title" not in m
assert m["meta"] == {}
assert m["description"] == "<p>content</p>"
src = tmpdir.join("empty.txt")
src.write("")
m = utils.read_markdown(str(src))
2023-02-12 17:31:08 +01:00
assert "title" not in m
# See https://github.com/Python-Markdown/markdown/pull/672
# Meta attributes should always be there
2023-02-12 17:31:08 +01:00
assert m["meta"] == {}
assert m["description"] == ""
def test_is_valid_html5_video():
2023-02-12 17:31:08 +01:00
assert utils.is_valid_html5_video(".webm") is True
assert utils.is_valid_html5_video(".mpeg") is False