Skip to content

Commit

Permalink
Fix fragment parsing with stray numbers and dots
Browse files Browse the repository at this point in the history
fixes #562
  • Loading branch information
mdellweg committed Nov 7, 2023
1 parent ae9f321 commit 6dae5de
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
.vscode
Justfile
*egg-info/
towncrier.test.test_*/
_trial_temp*/
apidocs/
dist/
Expand Down
14 changes: 5 additions & 9 deletions src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,20 @@ def parse_newfragment_basename(

if len(parts) == 1:
return invalid
if len(parts) == 2:
ticket, category = parts
ticket = strip_if_integer_string(ticket)
return (ticket, category, 0) if category in frag_type_names else invalid

# There are at least 3 parts. Search for a valid category from the second
# part onwards.
# There are at least 2 parts. Search for a valid category from the second
# part onwards starting at the back.
# The category is used as the reference point in the parts list to later
# infer the issue number and counter value.
for i in range(1, len(parts)):
for i in reversed(range(1, len(parts))):
if parts[i] in frag_type_names:
# Current part is a valid category according to given definitions.
category = parts[i]
# Use the previous part as the ticket number.
# Use all previous parts as the ticket number.
# NOTE: This allows news fragment names like fix-1.2.3.feature or
# something-cool.feature.ext for projects that don't use ticket
# numbers in news fragment names.
ticket = strip_if_integer_string(parts[i - 1])
ticket = strip_if_integer_string(".".join(parts[0:i]))
counter = 0
# Use the following part as the counter if it exists and is a valid
# digit.
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/562.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parsing news fragments is now more robust in the face of non numeric fragment names containing numbers and dots.
9 changes: 7 additions & 2 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _test_command(self, command):

result = runner.invoke(command, ["--draft", "--date", "01-01-2001"])

self.assertEqual(0, result.exit_code)
self.assertEqual(0, result.exit_code, result.output)
self.assertEqual(
result.output,
dedent(
Expand All @@ -78,7 +78,7 @@ def _test_command(self, command):
--------
- Baz levitation (baz)
- Baz fix levitation (#2)
- Baz fix levitation (fix-1.2)
- Adds levitation (#123)
- Extends levitation (#124)
- An orphaned feature ending with a dotted number
Expand Down Expand Up @@ -417,6 +417,7 @@ def test_draft_no_date(self):
call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "config", "commit.gpgSign", "false"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

Expand All @@ -441,6 +442,7 @@ def test_no_confirmation(self):
call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "config", "commit.gpgSign", "false"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

Expand Down Expand Up @@ -470,6 +472,7 @@ def test_keep_fragments(self, runner):
call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "config", "commit.gpgSign", "false"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

Expand Down Expand Up @@ -503,6 +506,7 @@ def test_yes_keep_error(self, runner):
call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "config", "commit.gpgSign", "false"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

Expand Down Expand Up @@ -531,6 +535,7 @@ def test_confirmation_says_no(self):
call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "config", "commit.gpgSign", "false"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

Expand Down
30 changes: 28 additions & 2 deletions src/towncrier/test/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_non_numeric_ticket_with_extension(self):
def test_dots_in_ticket_name(self):
self.assertEqual(
parse_newfragment_basename("baz.1.2.feature", ["feature"]),
("2", "feature", 0),
("baz.1.2", "feature", 0),
)

def test_dots_in_ticket_name_invalid_category(self):
Expand All @@ -64,7 +64,7 @@ def test_dots_in_ticket_name_invalid_category(self):
def test_dots_in_ticket_name_and_counter(self):
self.assertEqual(
parse_newfragment_basename("baz.1.2.feature.3", ["feature"]),
("2", "feature", 3),
("baz.1.2", "feature", 3),
)

def test_strip(self):
Expand All @@ -81,3 +81,29 @@ def test_strip_with_counter(self):
parse_newfragment_basename(" 007.feature.3", ["feature"]),
("7", "feature", 3),
)

def test_orphan(self):
self.assertEqual(
parse_newfragment_basename("+orphan.feature", ["feature"]),
("+orphan", "feature", 0),
)

def test_orphan_with_number(self):
self.assertEqual(
parse_newfragment_basename("+123_orphan.feature", ["feature"]),
("+123_orphan", "feature", 0),
)
self.assertEqual(
parse_newfragment_basename("+orphan_123.feature", ["feature"]),
("+orphan_123", "feature", 0),
)

def test_orphan_with_dotted_number(self):
self.assertEqual(
parse_newfragment_basename("+12.3_orphan.feature", ["feature"]),
("+12.3_orphan", "feature", 0),
)
self.assertEqual(
parse_newfragment_basename("+orphan_12.3.feature", ["feature"]),
("+orphan_12.3", "feature", 0),
)

0 comments on commit 6dae5de

Please sign in to comment.