Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for wildcards in ignore #644

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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>`_.
9 changes: 8 additions & 1 deletion src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,8 @@ def test_uncommitted_files(self, runner, commit):
config="""
[tool.towncrier]
package = "foo"
ignore = ["template.jinja", "CAPYBARAS.md"]
ignore = ["template.jinja", "CAPYBARAS.md", \
"star_wildcard*", "question_wildcard_?", "seq_wildcard_[ab]"]
"""
)
def test_ignored_files(self, runner):
Expand All @@ -1603,6 +1604,12 @@ 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/star_wildcard_bar", "w") as f:
f.write("Manually ignored with * wildcard")
with open("foo/newsfragments/question_wildcard_1", "w") as f:
f.write("Manually ignored with ? wildcard")
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
16 changes: 15 additions & 1 deletion src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,28 @@ def test_ignored_files(self, runner):
"""
When `ignore` is set in config, files with those names are ignored.
eigenbrot marked this conversation as resolved.
Show resolved Hide resolved
"""
create_project("pyproject.toml", extra_config='ignore = ["template.jinja"]')
create_project(
"pyproject.toml",
extra_config='ignore = ["template.jinja", "star_wildcard*", "question_wildcard_?", '
'"seq_wildcard_[ab]"]',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to keep it simple

Suggested change
extra_config='ignore = ["template.jinja", "star_wildcard*", "question_wildcard_?", '
'"seq_wildcard_[ab]"]',
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")
write("foo/newsfragments/STAR_WILDCARD_bar", "Manually ignored with * wildcard")
write(
"foo/newsfragments/question_wildcard_1", "Manually ignored with ? wildcard"
)
write(
"foo/newsfragments/QUESTION_WILDCARD_1", "Manually ignored with ? wildcard"
)
write("foo/newsfragments/seq_wildcard_a", "Manually ignored with [] wildcard")
write("foo/newsfragments/SEQ_WILDCARD_b", "Manually ignored with [] wildcard")
eigenbrot marked this conversation as resolved.
Show resolved Hide resolved
commit("add stuff")

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