Refactor --template code (#1711)

* Move path concerns to path.py and template concerns to editor.py -- BDD tests are failing
* Move path-related constants from config.py to path.py
* Mock get_templates_path in its new calling file
* Mediate template arg vs. config in controller then read template text in editor and unify those two use cases.
Some tests still failing
* Fix test whose message had changed
* poe format
* Refactor for easier unit testing and add unit tests
* Use path strings instead of Path objects in return values to prevent side effects that caused unit tests to fail on some platforms
* poe format
* Attempt to bypass getcwd errors in CI with patch
* Consistently use strings for paths instead of some strings and some pathlib.Path
* Keep pathlib within a function for readability
* fix for ruamel.yaml versions >=0.17.22
* Run poe format

---------

Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
This commit is contained in:
Micah Jerome Ellison 2023-05-20 16:13:24 -07:00 committed by GitHub
parent 78d11d74bd
commit 4a7057c038
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 190 additions and 147 deletions

View file

@ -38,7 +38,7 @@ Feature: Using templates
And we use the password "test" if prompted
When we run "jrnl --template this_template_does_not_exist.template"
Then we should get an error
Then the error output should contain "Unable to find a template file based on the passed arg"
Then the error output should contain "Unable to find a template file"
Examples: configs
| config_file |

View file

@ -183,10 +183,10 @@ 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.controller.get_templates_path", return_value=templates_path
"jrnl.editor.get_templates_path", return_value=templates_path
),
}

45
tests/unit/test_editor.py Normal file
View file

@ -0,0 +1,45 @@
# Copyright © 2012-2023 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
import os
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(
"os.getcwd", side_effect="/"
) # prevent failures in CI if current directory has been deleted
@patch("builtins.open", side_effect=FileNotFoundError())
def test_read_template_file_with_no_file_raises_exception(mock_open, mock_getcwd):
with pytest.raises(JrnlException) as ex:
read_template_file("invalid_file.txt")
assert isinstance(ex.value, JrnlException)
@patch(
"os.getcwd", side_effect="/"
) # prevent failures in CI if current directory has been deleted
@patch("builtins.open", new_callable=mock_open, read_data="template text")
def test_read_template_file_with_valid_file_returns_text(mock_file, mock_getcwd):
assert read_template_file("valid_file.txt") == "template text"
def test_get_template_path_when_exists_returns_correct_path():
with patch("os.path.exists", return_value=True):
output = get_template_path("template", "templatepath")
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("os.path.exists", return_value=False):
output = get_template_path("template", "templatepath")
assert output == mock_absolute_paths.return_value