diff --git a/src/towncrier/create.py b/src/towncrier/create.py index 28903f58..77433fca 100644 --- a/src/towncrier/create.py +++ b/src/towncrier/create.py @@ -167,7 +167,7 @@ def __main( if edit: if content == DEFAULT_CONTENT: content = "" - content = _get_news_content_from_user(content) + content = _get_news_content_from_user(content, extension=filename_ext) if not content: click.echo("Aborted creating news fragment due to empty message.") ctx.exit(1) @@ -180,14 +180,14 @@ def __main( click.echo(f"Created news fragment at {segment_file}") -def _get_news_content_from_user(message: str) -> str: +def _get_news_content_from_user(message: str, extension: str = "") -> str: initial_content = """ # Please write your news content. Lines starting with '#' will be ignored, and # an empty message aborts. """ if message: initial_content = f"{message}\n{initial_content}" - content = click.edit(initial_content) + content = click.edit(initial_content, extension=extension or ".txt") if content is None: return message all_lines = content.split("\n") diff --git a/src/towncrier/test/test_create.py b/src/towncrier/test/test_create.py index e946f201..2ba74af9 100644 --- a/src/towncrier/test/test_create.py +++ b/src/towncrier/test/test_create.py @@ -63,7 +63,8 @@ def test_edit_without_comments(self): self._test_success(content=content, additional_args=["--edit"]) mock_edit.assert_called_once_with( "\n# Please write your news content. Lines starting " - "with '#' will be ignored, and\n# an empty message aborts.\n" + "with '#' will be ignored, and\n# an empty message aborts.\n", + extension=".rst", ) def test_edit_with_comment(self): @@ -87,6 +88,56 @@ def test_edit_abort(self): self.assertEqual([], os.listdir("foo/newsfragments")) self.assertEqual(1, result.exit_code) + def test_edit_markdown_extension(self): + """ + The temporary file extension used when editing is ``.md`` if the main filename + also uses that extension. + """ + + with mock.patch("click.edit") as mock_edit: + mock_edit.return_value = "This is line 1" + self._test_success( + content=["This is line 1"], + config=dedent( + """\ + [tool.towncrier] + package = "foo" + filename = "README.md" + """ + ), + additional_args=["--edit"], + ) + mock_edit.assert_called_once_with( + "\n# Please write your news content. Lines starting " + "with '#' will be ignored, and\n# an empty message aborts.\n", + extension=".md", + ) + + def test_edit_unknown_extension(self): + """ + The temporary file extension used when editing is ``.txt`` if it the main + filename isn't ``.rst`` or ``.md``. + """ + + with mock.patch("click.edit") as mock_edit: + mock_edit.return_value = "This is line 1" + self._test_success( + content=["This is line 1"], + config=dedent( + """\ + [tool.towncrier] + package = "foo" + filename = "README.FIRST" + """ + ), + additional_args=["--edit"], + ) + mock_edit.assert_called_once_with( + "\n# Please write your news content. Lines starting " + "with '#' will be ignored, and\n# an empty message aborts.\n", + extension=".txt", + ) + def test_content(self): """ When creating a new fragment the content can be passed as a @@ -132,7 +183,8 @@ def test_message_and_edit(self): ) mock_edit.assert_called_once_with( f"{content_line}\n\n# Please write your news content. Lines starting " - "with '#' will be ignored, and\n# an empty message aborts.\n" + "with '#' will be ignored, and\n# an empty message aborts.\n", + extension=".rst", ) def test_different_directory(self):