mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 08:38:32 +02:00
Fixed error related to display_format in config file for some values (#1495)
* Fixed error related to display_format in config file Now _display_search_results tries to source the export arg from the config file before dispaying search results. * Add BDD test for original bug * update unit test Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com> Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
This commit is contained in:
parent
e758986985
commit
c043f5058f
4 changed files with 71 additions and 32 deletions
14
jrnl/jrnl.py
14
jrnl/jrnl.py
|
@ -374,20 +374,20 @@ def _change_time_search_results(args, journal, old_entries, no_prompt=False, **k
|
||||||
|
|
||||||
|
|
||||||
def _display_search_results(args, journal, **kwargs):
|
def _display_search_results(args, journal, **kwargs):
|
||||||
if args.short or args.export == "short":
|
# Get export format from config file if not provided at the command line
|
||||||
|
args.export = args.export or kwargs["config"].get("display_format")
|
||||||
|
|
||||||
|
if args.tags:
|
||||||
|
print(plugins.get_exporter("tags").export(journal))
|
||||||
|
|
||||||
|
elif args.short or args.export == "short":
|
||||||
print(journal.pprint(short=True))
|
print(journal.pprint(short=True))
|
||||||
|
|
||||||
elif args.export == "pretty":
|
elif args.export == "pretty":
|
||||||
print(journal.pprint())
|
print(journal.pprint())
|
||||||
|
|
||||||
elif args.tags:
|
|
||||||
print(plugins.get_exporter("tags").export(journal))
|
|
||||||
|
|
||||||
elif args.export:
|
elif args.export:
|
||||||
exporter = plugins.get_exporter(args.export)
|
exporter = plugins.get_exporter(args.export)
|
||||||
print(exporter.export(journal, args.filename))
|
print(exporter.export(journal, args.filename))
|
||||||
elif kwargs["config"].get("display_format"):
|
|
||||||
exporter = plugins.get_exporter(kwargs["config"]["display_format"])
|
|
||||||
print(exporter.export(journal, args.filename))
|
|
||||||
else:
|
else:
|
||||||
print(journal.pprint())
|
print(journal.pprint())
|
||||||
|
|
|
@ -560,3 +560,19 @@ Feature: Custom formats
|
||||||
| basic_encrypted.yaml |
|
| basic_encrypted.yaml |
|
||||||
| basic_folder.yaml |
|
| basic_folder.yaml |
|
||||||
| basic_dayone.yaml |
|
| basic_dayone.yaml |
|
||||||
|
|
||||||
|
|
||||||
|
Scenario Outline: display_format short and pretty do not crash if specified as config values
|
||||||
|
Given we use the config "<config_file>"
|
||||||
|
And we use the password "test" if prompted
|
||||||
|
When we run "jrnl --config-override display_format short -1"
|
||||||
|
Then we should get no error
|
||||||
|
When we run "jrnl --config-override display_format pretty -1"
|
||||||
|
Then we should get no error
|
||||||
|
|
||||||
|
Examples: configs
|
||||||
|
| config_file |
|
||||||
|
| basic_onefile.yaml |
|
||||||
|
| basic_encrypted.yaml |
|
||||||
|
| basic_folder.yaml |
|
||||||
|
| basic_dayone.yaml |
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
import argparse
|
|
||||||
from unittest import mock
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import jrnl
|
|
||||||
from jrnl.jrnl import _display_search_results
|
|
||||||
|
|
||||||
|
|
||||||
# fmt: off
|
|
||||||
# see: https://github.com/psf/black/issues/664
|
|
||||||
@pytest.mark.parametrize("export_format", [ "pretty", "short","markdown"])
|
|
||||||
#fmt: on
|
|
||||||
@mock.patch.object(argparse, "Namespace", return_value={"export": "markdown", "filename": "irrele.vant"})
|
|
||||||
def test_export_format(mock_args, export_format):
|
|
||||||
|
|
||||||
test_journal = jrnl.Journal.Journal
|
|
||||||
mock_args.export = export_format
|
|
||||||
#fmt: off
|
|
||||||
# see: https://github.com/psf/black/issues/664
|
|
||||||
with mock.patch("builtins.print") as mock_spy_print, \
|
|
||||||
mock.patch('jrnl.Journal.Journal.pprint') as mock_pprint:
|
|
||||||
_display_search_results(mock_args, test_journal)
|
|
||||||
mock_spy_print.assert_called_once_with(mock_pprint())
|
|
||||||
#fmt: on
|
|
48
tests/unit/test_jrnl.py
Normal file
48
tests/unit/test_jrnl.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
import jrnl
|
||||||
|
from jrnl.jrnl import _display_search_results
|
||||||
|
from jrnl.args import parse_args
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def random_string():
|
||||||
|
return "".join(random.choices(string.ascii_uppercase + string.digits, k=25))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("export_format", ["pretty", "short"])
|
||||||
|
@mock.patch("builtins.print")
|
||||||
|
@mock.patch("jrnl.Journal.Journal.pprint")
|
||||||
|
def test_display_search_results_pretty_short(mock_pprint, mock_print, export_format):
|
||||||
|
mock_args = parse_args(["--format", export_format])
|
||||||
|
test_journal = mock.Mock(wraps=jrnl.Journal.Journal)
|
||||||
|
|
||||||
|
_display_search_results(mock_args, test_journal)
|
||||||
|
|
||||||
|
mock_print.assert_called_once_with(mock_pprint.return_value)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"export_format", ["markdown", "json", "xml", "yaml", "fancy", "dates"]
|
||||||
|
)
|
||||||
|
@mock.patch("jrnl.plugins.get_exporter")
|
||||||
|
@mock.patch("builtins.print")
|
||||||
|
def test_display_search_results_builtin_plugins(
|
||||||
|
mock_print, mock_exporter, export_format, random_string
|
||||||
|
):
|
||||||
|
test_filename = random_string
|
||||||
|
mock_args = parse_args(["--format", export_format, "--file", test_filename])
|
||||||
|
|
||||||
|
test_journal = mock.Mock(wraps=jrnl.Journal.Journal)
|
||||||
|
mock_export = mock.Mock()
|
||||||
|
mock_exporter.return_value.export = mock_export
|
||||||
|
|
||||||
|
_display_search_results(mock_args, test_journal)
|
||||||
|
|
||||||
|
mock_exporter.assert_called_once_with(export_format)
|
||||||
|
mock_export.assert_called_once_with(test_journal, test_filename)
|
||||||
|
mock_print.assert_called_once_with(mock_export.return_value)
|
Loading…
Add table
Reference in a new issue