Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error related to display_format in config file for some values #1495

Merged
merged 6 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions jrnl/jrnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,20 @@ def _change_time_search_results(args, journal, old_entries, no_prompt=False, **k


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))

elif args.export == "pretty":
print(journal.pprint())

elif args.tags:
print(plugins.get_exporter("tags").export(journal))

elif args.export:
exporter = plugins.get_exporter(args.export)
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:
print(journal.pprint())
16 changes: 16 additions & 0 deletions tests/bdd/features/format.feature
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,19 @@ Feature: Custom formats
| basic_encrypted.yaml |
| basic_folder.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 |
25 changes: 0 additions & 25 deletions tests/unit/test_display.py

This file was deleted.

48 changes: 48 additions & 0 deletions tests/unit/test_jrnl.py
Original file line number Diff line number Diff line change
@@ -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)