2014-01-15 23:25:24 +01:00
|
|
|
import os
|
2019-10-04 00:01:41 -03:00
|
|
|
from pathlib import Path
|
2020-03-15 22:58:03 -03:00
|
|
|
|
2014-07-17 00:32:42 +02:00
|
|
|
from sigal import utils
|
2014-01-15 23:25:24 +01:00
|
|
|
|
|
|
|
|
CURRENT_DIR = os.path.dirname(__file__)
|
2023-02-12 17:31:08 +01:00
|
|
|
SAMPLE_DIR = os.path.join(CURRENT_DIR, "sample")
|
2014-01-15 23:25:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_copy(tmpdir):
|
2023-02-12 17:31:08 +01:00
|
|
|
filename = "KeckObservatory20071020.jpg"
|
|
|
|
|
src = os.path.join(SAMPLE_DIR, "pictures", "dir2", filename)
|
2014-01-15 23:25:24 +01:00
|
|
|
dst = str(tmpdir.join(filename))
|
2014-07-17 00:32:42 +02:00
|
|
|
utils.copy(src, dst)
|
2014-01-15 23:25:24 +01:00
|
|
|
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)
|
2014-01-15 23:25:24 +01:00
|
|
|
dst = str(tmpdir.join(filename))
|
2014-07-17 00:32:42 +02:00
|
|
|
utils.copy(src, dst, symlink=True)
|
2014-01-15 23:25:24 +01:00
|
|
|
assert os.path.islink(dst)
|
2014-01-16 00:16:01 +01:00
|
|
|
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)
|
2014-07-17 00:32:42 +02:00
|
|
|
utils.copy(src, dst, symlink=True)
|
2014-01-16 00:16:01 +01:00
|
|
|
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
|
2014-01-16 00:16:01 +01:00
|
|
|
|
2023-02-12 17:31:08 +01:00
|
|
|
src = str(tmpdir.join("foo.txt"))
|
|
|
|
|
dst = str(tmpdir.join("bar.txt"))
|
2019-10-04 00:01:41 -03:00
|
|
|
p = Path(src)
|
|
|
|
|
p.touch()
|
|
|
|
|
p.chmod(0o444)
|
|
|
|
|
utils.copy(src, dst)
|
|
|
|
|
utils.copy(src, dst)
|
|
|
|
|
|
|
|
|
|
|
2014-01-16 00:16:01 +01:00
|
|
|
def test_check_or_create_dir(tmpdir):
|
2023-02-12 17:31:08 +01:00
|
|
|
path = str(tmpdir.join("new_directory"))
|
2014-07-17 00:32:42 +02:00
|
|
|
utils.check_or_create_dir(path)
|
2014-01-16 00:16:01 +01:00
|
|
|
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-17 00:32:42 +02:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-07-17 00:32:42 +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")
|
2014-07-17 00:32:42 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-10-15 23:43:53 +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>"
|
2014-10-15 23:43:53 +02:00
|
|
|
|
|
|
|
|
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
|
2018-06-19 22:43:43 +02:00
|
|
|
# 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"] == ""
|
2014-10-15 23:43:53 +02:00
|
|
|
|
|
|
|
|
|
2015-08-31 00:26:20 +02:00
|
|
|
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
|