Skip to content

Commit

Permalink
rename to issue_pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
dorschw committed Aug 5, 2024
1 parent e5cd753 commit fb5566d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
9 changes: 5 additions & 4 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ Top level keys
- ``README.rst``
- the template file itself

``fragment_filename_stem_pattern``
Ensure the stem (first part, excluding the category and suffix) of fragment files matches a certain regex pattern.
Make sure to use escape characters properly, e.g. "\\d+" for digit-only file names.
``issue_pattern``
Ensure the issue name (file name excluding the category and suffix) matches a certain regex pattern.
Make sure to use escape characters properly (e.g. "\\d+" for digit-only file names).
When emptry (``""``), all issue names will be considered valid.

``None`` by default.
``""`` by default.

Extra top level keys for Python projects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
10 changes: 5 additions & 5 deletions src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ def find_fragments(
# Use and increment the orphan news fragment counter.
counter = orphan_fragment_counter[category]
orphan_fragment_counter[category] += 1
if config.fragment_filename_stem_pattern and (
if config.issue_pattern and (
not re.fullmatch(
config.fragment_filename_stem_pattern,
stem := Path(basename).stem.removesuffix(f".{category}"),
config.issue_pattern,
issue_name := Path(basename).stem.removesuffix(f".{category}"),
)
):
raise ClickException(
f"File name stem '{stem}' does not match the "
f"given pattern, '{config.fragment_filename_stem_pattern}'"
f"File name '{issue_name}' does not match the "
f"given issue pattern, '{config.issue_pattern}'"
)
full_filename = os.path.join(section_dir, basename)
fragment_files.append((full_filename, category))
Expand Down
2 changes: 1 addition & 1 deletion src/towncrier/_settings/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Config:
create_eof_newline: bool = True
create_add_extension: bool = True
ignore: list[str] | None = None
fragment_filename_stem_pattern: str | None = None
issue_pattern: str = ""


class ConfigError(ClickException):
Expand Down
2 changes: 1 addition & 1 deletion src/towncrier/newsfragments/649.feature.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add a config for enforcing the fragment filename stems, using regex.
Add a config for enforcing issue names using regex.
10 changes: 4 additions & 6 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,14 @@ def test_invalid_fragment_name(self, runner):
self.assertIn("Invalid news fragment name: feature.125", result.output)

@with_isolated_runner
def test_fragment_name_stem_pattern(self, runner):
def test_issue_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+"',
extra_config='issue_pattern = "\\\\d+"',
)
write(
"foo/newsfragments/AAA.feature", #
Expand All @@ -536,14 +536,12 @@ def test_fragment_name_stem_pattern(self, runner):
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+'",
"Error: File name 'AAA' does not match the given issue pattern, '\\d+'",
result.output,
)
self.assertNotIn(
"Error: File name stem '123' does not match the given pattern, '\\d+'",
"Error: File name '123' does not match the given issue pattern, '\\d+'",
result.output,
)

0 comments on commit fb5566d

Please sign in to comment.