mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Refactor for easier unit testing and add unit tests
This commit is contained in:
parent
05ee33a61a
commit
1a67c8c0ed
2 changed files with 53 additions and 7 deletions
|
@ -77,6 +77,17 @@ def get_text_from_stdin() -> str:
|
||||||
return raw
|
return raw
|
||||||
|
|
||||||
|
|
||||||
|
def get_template_path(template_path: str, jrnl_template_dir: Path) -> Path:
|
||||||
|
actual_template_path = jrnl_template_dir / template_path
|
||||||
|
if not actual_template_path.exists():
|
||||||
|
logging.debug(
|
||||||
|
f"Couldn't open {actual_template_path}. Treating template path like a local / abs path."
|
||||||
|
)
|
||||||
|
actual_template_path = absolute_path(template_path)
|
||||||
|
|
||||||
|
return str(actual_template_path)
|
||||||
|
|
||||||
|
|
||||||
def read_template_file(template_path: str) -> str:
|
def read_template_file(template_path: str) -> str:
|
||||||
"""
|
"""
|
||||||
Reads the template file given a template path in this order:
|
Reads the template file given a template path in this order:
|
||||||
|
@ -89,13 +100,7 @@ def read_template_file(template_path: str) -> str:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
jrnl_template_dir = get_templates_path()
|
jrnl_template_dir = get_templates_path()
|
||||||
|
actual_template_path = get_template_path(template_path, jrnl_template_dir)
|
||||||
actual_template_path = jrnl_template_dir / template_path
|
|
||||||
if not (actual_template_path).exists():
|
|
||||||
logging.debug(
|
|
||||||
f"Couldn't open {actual_template_path}. Treating template path like a local / abs path."
|
|
||||||
)
|
|
||||||
actual_template_path = absolute_path(template_path)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(actual_template_path, encoding="utf-8") as f:
|
with open(actual_template_path, encoding="utf-8") as f:
|
||||||
|
|
41
tests/unit/test_editor.py
Normal file
41
tests/unit/test_editor.py
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
# Copyright © 2012-2023 jrnl contributors
|
||||||
|
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
||||||
|
|
||||||
|
import pathlib
|
||||||
|
from unittest.mock import mock_open
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from jrnl.editor import get_template_path
|
||||||
|
from jrnl.editor import read_template_file
|
||||||
|
from jrnl.exception import JrnlException
|
||||||
|
|
||||||
|
|
||||||
|
@patch("builtins.open", side_effect=FileNotFoundError())
|
||||||
|
def test_read_template_file_with_no_file_raises_exception(mock_open):
|
||||||
|
with pytest.raises(JrnlException) as ex:
|
||||||
|
read_template_file("invalid_file.txt")
|
||||||
|
assert isinstance(ex.value, JrnlException)
|
||||||
|
|
||||||
|
|
||||||
|
@patch("builtins.open", new_callable=mock_open, read_data="template text")
|
||||||
|
def test_read_template_file_with_valid_file_returns_text(mock_file):
|
||||||
|
assert read_template_file("valid_file.txt") == "template text"
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_template_path_when_exists_returns_correct_path():
|
||||||
|
template_path = pathlib.Path("templatepath")
|
||||||
|
|
||||||
|
with patch.object(pathlib.Path, "exists", lambda _: True):
|
||||||
|
output = get_template_path("template", template_path)
|
||||||
|
|
||||||
|
assert output == str(template_path / "template")
|
||||||
|
|
||||||
|
|
||||||
|
@patch("jrnl.editor.absolute_path")
|
||||||
|
def test_get_template_path_when_doesnt_exist_returns_correct_path(mock_absolute_paths):
|
||||||
|
with patch.object(pathlib.Path, "exists", lambda _: False):
|
||||||
|
output = get_template_path("template", pathlib.Path("templatepath"))
|
||||||
|
|
||||||
|
assert output == str(mock_absolute_paths.return_value)
|
Loading…
Add table
Reference in a new issue