Skip to content

Commit

Permalink
add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
dorschw committed Aug 5, 2024
1 parent 00793d6 commit df72621
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ def find_fragments(
orphan_fragment_counter[category] += 1
if config.fragment_filename_stem_pattern and (
not re.fullmatch(
config.fragment_filename_stem_pattern, stem := Path(basename).stem
config.fragment_filename_stem_pattern,
stem := Path(basename).stem.removesuffix(f".{category}"),
)
):
raise ClickException(
f"File name '{stem}' does not match the "
f"File name stem '{stem}' does not match the "
f"given pattern, '{config.fragment_filename_stem_pattern}'"
)
full_filename = os.path.join(section_dir, basename)
Expand Down
51 changes: 32 additions & 19 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,22 +515,35 @@ def test_invalid_fragment_name(self, runner):
self.assertEqual(1, result.exit_code, result.output)
self.assertIn("Invalid news fragment name: feature.125", result.output)

# @with_isolated_runner
# def test_invalid_fragment_name_pattern(self, runner):
# """
# Fails if a news fragment has an invalid name, even if `ignore` is not set in
# the config.
# """
# create_project(
# "pyproject.toml",
# extra_config=r'fragment_filename_stem_pattern = "[A-Z]+-\d+"',
# )
# write(
# "foo/newsfragments/124.feature",
# "This fragment has valid name (control case)",
# )
# commit("add stuff")

# result = runner.invoke(towncrier_check, ["--compare-with", "main"])
# self.assertEqual(1, result.exit_code, result.output)
# self.assertIn("Invalid news fragment name: feature.125", result.output)
@with_isolated_runner
def test_fragment_name_stem_pattern(self, runner):
"""
Fails if a news fragment has an invalid name, even if `ignore` is not set in
the config.
"""
create_project(
"pyproject.toml",
extra_config='fragment_filename_stem_pattern = "\\\\d+"',
)
write(
"foo/newsfragments/AAA.feature", #
"This fragment has an invalid name (should be digits only)",
)
write(
"foo/newsfragments/123.feature", #
"This fragment has a valid name",
)
commit("add stuff")

result = runner.invoke(towncrier_check, ["--compare-with", "main"])
print(">>>")
print(result)
self.assertEqual(1, result.exit_code, result.output)
self.assertIn(
"Error: File name stem 'AAA' does not match the given pattern, '\\d+'",
result.output,
)
self.assertNotIn(
"Error: File name stem '123' does not match the given pattern, '\\d+'",
result.output,
)

0 comments on commit df72621

Please sign in to comment.