Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit-extension #594

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/594.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The temporary file ``towncrier create`` creates now uses the correct ``.rst`` or ``.md`` extension, which may help your editor with with syntax highlighting.
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
Loading