diff --git a/src/towncrier/_builder.py b/src/towncrier/_builder.py index 074bd801..eb9938ab 100644 --- a/src/towncrier/_builder.py +++ b/src/towncrier/_builder.py @@ -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) diff --git a/src/towncrier/test/test_check.py b/src/towncrier/test/test_check.py index 4ba894fb..206d90b7 100644 --- a/src/towncrier/test/test_check.py +++ b/src/towncrier/test/test_check.py @@ -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, + )