Skip to content

Commit

Permalink
Add support for wildcards in ignore (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban authored Aug 5, 2024
2 parents eed6d16 + 4fd3b42 commit 0f1d802
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Top level keys

``ignore``
A case-insensitive list of filenames in the news fragments directory to ignore.
Wildcard matching is supported via the `fnmatch <https://docs.python.org/3/library/fnmatch.html#fnmatch.fnmatch>`_ function.

``None`` by default.

Expand Down
8 changes: 7 additions & 1 deletion src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import textwrap

from collections import defaultdict
from fnmatch import fnmatch
from pathlib import Path
from typing import Any, DefaultDict, Iterable, Iterator, Mapping, NamedTuple, Sequence

Expand Down Expand Up @@ -149,7 +150,12 @@ def find_fragments(
file_content = {}

for basename in files:
if basename.lower() in ignored_files:
if any(
[
fnmatch(basename.lower(), ignore_pattern)
for ignore_pattern in ignored_files
]
):
continue

issue, category, counter = parse_newfragment_basename(
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/644.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Config `ignore` option now supports wildcard matching via `fnmatch <https://docs.python.org/3/library/fnmatch.html#fnmatch.fnmatch>`_.
5 changes: 4 additions & 1 deletion src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1677,12 +1677,13 @@ def test_uncommitted_files(self, runner, commit):
config="""
[tool.towncrier]
package = "foo"
ignore = ["template.jinja", "CAPYBARAS.md"]
ignore = ["template.jinja", "CAPYBARAS.md", "seq_wildcard_[ab]"]
"""
)
def test_ignored_files(self, runner):
"""
When `ignore` is set in config, files with those names are ignored.
Configuration supports wildcard matching with `fnmatch`.
"""
with open("foo/newsfragments/123.feature", "w") as f:
f.write("This has valid filename (control case)")
Expand All @@ -1692,6 +1693,8 @@ def test_ignored_files(self, runner):
f.write("This markdown file has been manually ignored")
with open("foo/newsfragments/.gitignore", "w") as f:
f.write("gitignore is automatically ignored")
with open("foo/newsfragments/seq_wildcard_a", "w") as f:
f.write("Manually ignored with [] wildcard")

result = runner.invoke(_main, ["--draft"])
self.assertEqual(0, result.exit_code, result.output)
Expand Down
7 changes: 6 additions & 1 deletion src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,20 @@ def test_in_different_dir_with_nondefault_newsfragments_directory(self, runner):
def test_ignored_files(self, runner):
"""
When `ignore` is set in config, files with those names are ignored.
Configuration supports wildcard matching with `fnmatch`.
"""
create_project("pyproject.toml", extra_config='ignore = ["template.jinja"]')
create_project(
"pyproject.toml",
extra_config='ignore = ["template.jinja", "star_wildcard*"]',
)

write(
"foo/newsfragments/124.feature",
"This fragment has valid name (control case)",
)
write("foo/newsfragments/template.jinja", "This is manually ignored")
write("foo/newsfragments/.gitignore", "gitignore is automatically ignored")
write("foo/newsfragments/star_wildcard_foo", "Manually ignored with * wildcard")
commit("add stuff")

result = runner.invoke(towncrier_check, ["--compare-with", "main"])
Expand Down

0 comments on commit 0f1d802

Please sign in to comment.