feat: improve template processing (#112)

* Improve template processing

* docs update, config validation rules update
This commit is contained in:
2023-09-08 23:38:07 +03:00
committed by GitHub
parent a56fe28003
commit 018d9589e1
28 changed files with 413 additions and 59 deletions

View File

@ -229,6 +229,19 @@ def test_getpath_without_fallback(configuration: Configuration) -> None:
assert configuration.getpath("build", "option")
def test_getpathlist(configuration: Configuration) -> None:
"""
must extract path list
"""
path = Path("/a/b/c")
configuration.set_option("build", "path", f"""{path} {path.relative_to("/")}""")
result = configuration.getpathlist("build", "path")
assert all(element.is_absolute() for element in result)
assert path in result
assert all(element.is_relative_to("/") for element in result)
def test_gettype(configuration: Configuration) -> None:
"""
must extract type from variable

View File

@ -118,3 +118,27 @@ def test_validate_path_exists(validator: Validator, mocker: MockerFixture) -> No
MockCall("field", "Path 2 must not exist"),
MockCall("field", "Path 3 must exist"),
])
def test_validate_path_type(validator: Validator, mocker: MockerFixture) -> None:
"""
must correctly validate path type
"""
error_mock = mocker.patch("ahriman.core.configuration.validator.Validator._error")
mocker.patch("pathlib.Path.is_file", return_value=True)
validator._validate_path_type("file", "field", Path("1"))
mocker.patch("pathlib.Path.is_file", return_value=False)
validator._validate_path_type("file", "field", Path("2"))
mocker.patch("pathlib.Path.is_dir", return_value=True)
validator._validate_path_type("dir", "field", Path("3"))
mocker.patch("pathlib.Path.is_dir", return_value=False)
validator._validate_path_type("dir", "field", Path("4"))
error_mock.assert_has_calls([
MockCall("field", "Path 2 must be type of file"),
MockCall("field", "Path 4 must be type of dir"),
])

View File

@ -8,6 +8,35 @@ from ahriman.models.package import Package
from ahriman.models.result import Result
def test_template(configuration: Configuration) -> None:
"""
must correctly parse template name and path
"""
template = configuration.get("email", "template")
root, repository_id = configuration.check_loaded()
assert Email(repository_id, configuration, "email").template == template
configuration.remove_option("email", "template")
configuration.set_option("email", "template_path", template)
assert Email(repository_id, configuration, "email").template == root.parent / template
def test_template_full(configuration: Configuration) -> None:
"""
must correctly parse template name and path
"""
template = "template"
root, repository_id = configuration.check_loaded()
configuration.set_option("email", "template_full", template)
assert Email(repository_id, configuration, "email").template_full == template
configuration.remove_option("email", "template_full")
configuration.set_option("email", "full_template_path", template)
assert Email(repository_id, configuration, "email").template_full == root.parent / template
def test_send(email: Email, mocker: MockerFixture) -> None:
"""
must send an email with attachment
@ -115,7 +144,7 @@ def test_generate_with_built_and_full_path(email: Email, package_ahriman: Packag
"""
send_mock = mocker.patch("ahriman.core.report.email.Email._send")
email.full_template_path = email.template_path
email.template_full = email.template
email.generate([package_ahriman], result)
send_mock.assert_called_once_with(pytest.helpers.anyvar(int), pytest.helpers.anyvar(int))

View File

@ -8,6 +8,20 @@ from ahriman.models.package import Package
from ahriman.models.result import Result
def test_template(configuration: Configuration) -> None:
"""
must correctly parse template name and path
"""
template = configuration.get("html", "template")
root, repository_id = configuration.check_loaded()
assert HTML(repository_id, configuration, "html").template == template
configuration.remove_option("html", "template")
configuration.set_option("html", "template_path", template)
assert HTML(repository_id, configuration, "html").template == root.parent / template
def test_generate(configuration: Configuration, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must generate report

View File

@ -8,7 +8,17 @@ def test_generate(configuration: Configuration, package_ahriman: Package) -> Non
"""
must generate html report
"""
path = configuration.getpath("html", "template_path")
name = configuration.getpath("html", "template")
_, repository_id = configuration.check_loaded()
report = JinjaTemplate(repository_id, configuration, "html")
assert report.make_html(Result(success=[package_ahriman]), name)
def test_generate_from_path(configuration: Configuration, package_ahriman: Package) -> None:
"""
must generate html report from path
"""
path = configuration.getpath("html", "templates") / configuration.get("html", "template")
_, repository_id = configuration.check_loaded()
report = JinjaTemplate(repository_id, configuration, "html")
assert report.make_html(Result(success=[package_ahriman]), path)

View File

@ -10,6 +10,20 @@ from ahriman.models.package import Package
from ahriman.models.result import Result
def test_template(configuration: Configuration) -> None:
"""
must correctly parse template name and path
"""
template = configuration.get("telegram", "template")
root, repository_id = configuration.check_loaded()
assert Telegram(repository_id, configuration, "telegram").template == template
configuration.remove_option("telegram", "template")
configuration.set_option("telegram", "template_path", template)
assert Telegram(repository_id, configuration, "telegram").template == root.parent / template
def test_send(telegram: Telegram, mocker: MockerFixture) -> None:
"""
must send a message

View File

@ -63,7 +63,8 @@ no_empty_report = no
port = 587
receivers = mail@example.com
sender = mail@example.com
template_path = ../web/templates/repo-index.jinja2
template = repo-index.jinja2
templates = ../web/templates
[console]
use_utf = yes
@ -72,7 +73,8 @@ use_utf = yes
path =
homepage =
link_path =
template_path = ../web/templates/repo-index.jinja2
template = repo-index.jinja2
templates = ../web/templates
[remote-call]
manual = yes
@ -82,7 +84,8 @@ api_key = apikey
chat_id = @ahrimantestchat
homepage =
link_path =
template_path = ../web/templates/telegram-index.jinja2
template = telegram-index.jinja2
templates = ../web/templates
[upload]
target =