From 0ee5f043dee087c4feee1451db7e00c0327df459 Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Sun, 10 Dec 2023 23:40:44 +0200 Subject: [PATCH] fix: don't strip trailing newlines when rendering fragment templates If a fragment template ends with a newline, then created fragments should end with one too. The `keep_trailing_newline` parameter was added in Jinja 2.7, so update the dependency. --- changelog.d/20231210_233839_dpb_keep_trailing_newline.rst | 7 +++++++ setup.cfg | 2 +- src/scriv/scriv.py | 3 ++- tests/test_create.py | 8 ++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 changelog.d/20231210_233839_dpb_keep_trailing_newline.rst diff --git a/changelog.d/20231210_233839_dpb_keep_trailing_newline.rst b/changelog.d/20231210_233839_dpb_keep_trailing_newline.rst new file mode 100644 index 0000000..99c70c5 --- /dev/null +++ b/changelog.d/20231210_233839_dpb_keep_trailing_newline.rst @@ -0,0 +1,7 @@ +Fixed +..... + +- A final newline is no longer stripped when rendering the new fragment + template, fixing `issue 108`_. + +.. _issue 108: https://github.com/nedbat/scriv/issues/108 diff --git a/setup.cfg b/setup.cfg index 27a6be5..9ebfca2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -39,7 +39,7 @@ install_requires = attrs click click-log - jinja2 + jinja2>=2.7 markdown-it-py requests # end_install_requires diff --git a/src/scriv/scriv.py b/src/scriv/scriv.py index 5665cf4..0826a89 100644 --- a/src/scriv/scriv.py +++ b/src/scriv/scriv.py @@ -90,7 +90,8 @@ def _new_fragment_path(config: Config) -> Path: def _new_fragment_content(config: Config) -> str: """Produce the initial content of a scriv fragment.""" return jinja2.Template( - textwrap.dedent(config.new_fragment_template) + textwrap.dedent(config.new_fragment_template), + keep_trailing_newline=True, ).render(config=config) diff --git a/tests/test_create.py b/tests/test_create.py index 43e836a..5358fa4 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -85,6 +85,14 @@ def test_new_fragment_contents_md(self): assert "### Added\n" in content assert all(cat in content for cat in scriv.config.categories) + def test_final_newline_in_template_preserved(self, changelog_d): + (changelog_d / "new_fragment.j2").write_text("Custom template.\n") + scriv = Scriv( + config=Config(new_fragment_template="file: new_fragment.j2") + ) + content = scriv.new_fragment().content + assert content == "Custom template.\n" + class TestCreate: """