Skip to content

Commit

Permalink
Refactor _get_news_content_from_user to accept optional extension par…
Browse files Browse the repository at this point in the history
…ameter
  • Loading branch information
SmileyChris committed May 6, 2024
1 parent db10a4d commit 923afc6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/towncrier/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
56 changes: 54 additions & 2 deletions src/towncrier/test/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 923afc6

Please sign in to comment.