diff --git a/jrnl/editor.py b/jrnl/editor.py index 54864089..cbcf6207 100644 --- a/jrnl/editor.py +++ b/jrnl/editor.py @@ -77,15 +77,15 @@ def get_text_from_stdin() -> str: 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(): +def get_template_path(template_path: str, jrnl_template_dir: str) -> str: + actual_template_path = os.path.join(jrnl_template_dir, template_path) + if not os.path.exists(actual_template_path): 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) + return actual_template_path def read_template_file(template_path: str) -> str: diff --git a/jrnl/path.py b/jrnl/path.py index c15fb453..62ba9678 100644 --- a/jrnl/path.py +++ b/jrnl/path.py @@ -34,13 +34,17 @@ def get_default_journal_path() -> str: return os.path.join(journal_data_path, DEFAULT_JOURNAL_NAME) -def get_templates_path() -> Path: +def get_templates_path() -> str: + """ + Get the path to the XDG templates directory. Creates the directory if it + doesn't exist. + """ # jrnl_xdg_resource_path is created by save_data_path if it does not exist jrnl_xdg_resource_path = Path(xdg.BaseDirectory.save_data_path(XDG_RESOURCE)) jrnl_templates_path = jrnl_xdg_resource_path / "templates" # Create the directory if needed. jrnl_templates_path.mkdir(exist_ok=True) - return jrnl_templates_path + return str(jrnl_templates_path) def get_config_directory() -> str: diff --git a/tests/lib/fixtures.py b/tests/lib/fixtures.py index ab43b1bf..168a8007 100644 --- a/tests/lib/fixtures.py +++ b/tests/lib/fixtures.py @@ -183,7 +183,7 @@ def mock_default_journal_path(temp_dir): @fixture def mock_default_templates_path(temp_dir): - templates_path = Path(temp_dir.name, "templates") + templates_path = os.path.join(temp_dir.name, "templates") return { "get_templates_path": lambda: patch( "jrnl.editor.get_templates_path", return_value=templates_path diff --git a/tests/unit/test_editor.py b/tests/unit/test_editor.py index de5220e7..7b90845d 100644 --- a/tests/unit/test_editor.py +++ b/tests/unit/test_editor.py @@ -1,7 +1,7 @@ # Copyright © 2012-2023 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html -import pathlib +import os from unittest.mock import mock_open from unittest.mock import patch @@ -25,17 +25,15 @@ def test_read_template_file_with_valid_file_returns_text(mock_file): def test_get_template_path_when_exists_returns_correct_path(): - template_path = pathlib.Path("templatepath") + with patch("os.path.exists", return_value = True): + output = get_template_path("template", "templatepath") - with patch.object(pathlib.Path, "exists", lambda _: True): - output = get_template_path("template", template_path) - - assert output == str(template_path / "template") + assert output == os.path.join("templatepath", "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")) + with patch("os.path.exists", return_value = False): + output = get_template_path("template", "templatepath") - assert output == str(mock_absolute_paths.return_value) + assert output == mock_absolute_paths.return_value