summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorouta <outa@users.noreply.github.com>2022-10-08 22:48:29 +0200
committerGitHub <noreply@github.com>2022-10-08 13:48:29 -0700
commitd7242d81a463b790f6650a18fce2b8047d776fbc (patch)
treea3a7ef7d78e0c44bafceb49b14f4460af4ae2c38
parenta925c81ba8acad9bdc03a86d24af521f66b48e74 (diff)
Don't create empty file when attempting a YAML export to a non-existing folder (#1600)
* Call `export_journal` before opening file handle * Use correct exporter class * Fix unit test
-rw-r--r--jrnl/plugins/text_exporter.py21
-rw-r--r--tests/unit/test_export.py18
2 files changed, 29 insertions, 10 deletions
diff --git a/jrnl/plugins/text_exporter.py b/jrnl/plugins/text_exporter.py
index 931305ef..4451cfef 100644
--- a/jrnl/plugins/text_exporter.py
+++ b/jrnl/plugins/text_exporter.py
@@ -31,18 +31,19 @@ class TextExporter:
@classmethod
def write_file(cls, journal, path):
"""Exports a journal into a single file."""
+ export_str = cls.export_journal(journal)
with open(path, "w", encoding="utf-8") as f:
- f.write(cls.export_journal(journal))
- print_msg(
- Message(
- MsgText.JournalExportedTo,
- MsgStyle.NORMAL,
- {
- "path": path,
- },
- )
+ f.write(export_str)
+ print_msg(
+ Message(
+ MsgText.JournalExportedTo,
+ MsgStyle.NORMAL,
+ {
+ "path": path,
+ },
)
- return ""
+ )
+ return ""
@classmethod
def make_filename(cls, entry):
diff --git a/tests/unit/test_export.py b/tests/unit/test_export.py
index 27e60d80..1fd8663e 100644
--- a/tests/unit/test_export.py
+++ b/tests/unit/test_export.py
@@ -1,10 +1,16 @@
# Copyright © 2012-2022 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
+from unittest import mock
+
import pytest
from jrnl.exception import JrnlException
+from jrnl.messages import Message
+from jrnl.messages import MsgStyle
+from jrnl.messages import MsgText
from jrnl.plugins.fancy_exporter import check_provided_linewrap_viability
+from jrnl.plugins.yaml_exporter import YAMLExporter
@pytest.fixture()
@@ -28,3 +34,15 @@ class TestFancy:
with pytest.raises(JrnlException):
check_provided_linewrap_viability(total_linewrap, [content], journal)
+
+
+class TestYaml:
+ @mock.patch("jrnl.plugins.yaml_exporter.YAMLExporter.export_journal")
+ @mock.patch("builtins.open")
+ def test_export_to_nonexisting_folder(self, mock_open, mock_export_journal):
+ mock_export_journal.side_effect = JrnlException(
+ Message(MsgText.YamlMustBeDirectory, MsgStyle.ERROR)
+ )
+ with pytest.raises(JrnlException):
+ YAMLExporter.write_file("journal", "non-existing-path")
+ mock_open.assert_not_called()