Use path strings instead of Path objects in return values to prevent side effects that caused unit tests to fail on some platforms

This commit is contained in:
Micah Jerome Ellison 2023-03-28 09:02:23 -07:00
parent 1a67c8c0ed
commit 0da0b7428f
4 changed files with 18 additions and 16 deletions

View file

@ -77,15 +77,15 @@ def get_text_from_stdin() -> str:
return raw return raw
def get_template_path(template_path: str, jrnl_template_dir: Path) -> Path: def get_template_path(template_path: str, jrnl_template_dir: str) -> str:
actual_template_path = jrnl_template_dir / template_path actual_template_path = os.path.join(jrnl_template_dir, template_path)
if not actual_template_path.exists(): if not os.path.exists(actual_template_path):
logging.debug( logging.debug(
f"Couldn't open {actual_template_path}. Treating template path like a local / abs path." f"Couldn't open {actual_template_path}. Treating template path like a local / abs path."
) )
actual_template_path = absolute_path(template_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: def read_template_file(template_path: str) -> str:

View file

@ -34,13 +34,17 @@ def get_default_journal_path() -> str:
return os.path.join(journal_data_path, DEFAULT_JOURNAL_NAME) 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 is created by save_data_path if it does not exist
jrnl_xdg_resource_path = Path(xdg.BaseDirectory.save_data_path(XDG_RESOURCE)) jrnl_xdg_resource_path = Path(xdg.BaseDirectory.save_data_path(XDG_RESOURCE))
jrnl_templates_path = jrnl_xdg_resource_path / "templates" jrnl_templates_path = jrnl_xdg_resource_path / "templates"
# Create the directory if needed. # Create the directory if needed.
jrnl_templates_path.mkdir(exist_ok=True) jrnl_templates_path.mkdir(exist_ok=True)
return jrnl_templates_path return str(jrnl_templates_path)
def get_config_directory() -> str: def get_config_directory() -> str:

View file

@ -183,7 +183,7 @@ def mock_default_journal_path(temp_dir):
@fixture @fixture
def mock_default_templates_path(temp_dir): def mock_default_templates_path(temp_dir):
templates_path = Path(temp_dir.name, "templates") templates_path = os.path.join(temp_dir.name, "templates")
return { return {
"get_templates_path": lambda: patch( "get_templates_path": lambda: patch(
"jrnl.editor.get_templates_path", return_value=templates_path "jrnl.editor.get_templates_path", return_value=templates_path

View file

@ -1,7 +1,7 @@
# Copyright © 2012-2023 jrnl contributors # Copyright © 2012-2023 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html # License: https://www.gnu.org/licenses/gpl-3.0.html
import pathlib import os
from unittest.mock import mock_open from unittest.mock import mock_open
from unittest.mock import patch 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(): 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): assert output == os.path.join("templatepath", "template")
output = get_template_path("template", template_path)
assert output == str(template_path / "template")
@patch("jrnl.editor.absolute_path") @patch("jrnl.editor.absolute_path")
def test_get_template_path_when_doesnt_exist_returns_correct_path(mock_absolute_paths): def test_get_template_path_when_doesnt_exist_returns_correct_path(mock_absolute_paths):
with patch.object(pathlib.Path, "exists", lambda _: False): with patch("os.path.exists", return_value = False):
output = get_template_path("template", pathlib.Path("templatepath")) output = get_template_path("template", "templatepath")
assert output == str(mock_absolute_paths.return_value) assert output == mock_absolute_paths.return_value