Call export_journal before opening file handle

This commit is contained in:
outa 2022-09-20 21:00:52 +02:00
parent 057f31407a
commit 53e7be72de
2 changed files with 34 additions and 10 deletions

View file

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

View file

@ -1,10 +1,16 @@
# Copyright © 2012-2022 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from ruamel.yaml import YAML
from jrnl.exception import JrnlException
from jrnl.Journal import PlainJournal
from jrnl.plugins.fancy_exporter import check_provided_linewrap_viability
from jrnl.plugins.text_exporter import TextExporter
@pytest.fixture()
@ -12,6 +18,14 @@ def datestr():
yield "2020-10-20 16:59"
@pytest.fixture()
def simple_journal():
with open("tests/data/configs/simple.yaml") as f:
yaml = YAML(typ="safe")
config = yaml.load(f)
return PlainJournal("simple_journal", **config).open()
def build_card_header(datestr):
top_left_corner = "┎─╮"
content = top_left_corner + datestr
@ -28,3 +42,12 @@ class TestFancy:
with pytest.raises(JrnlException):
check_provided_linewrap_viability(total_linewrap, [content], journal)
class TestYaml:
def test_export_to_nonexisting_folder(self, simple_journal):
with TemporaryDirectory() as tmpdir:
p = Path(tmpdir / "non_existing_folder")
with pytest.raises(JrnlException):
TextExporter.write_file(simple_journal, p)
assert not p.exists()