Skip to content

Commit

Permalink
automatically ignore template files (#633)
Browse files Browse the repository at this point in the history
* automatically ignore template files

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix cr

* changelog

* revert

* CR fix

* Update src/towncrier/_builder.py

Co-authored-by: Adi Roiban <[email protected]>

* Update src/towncrier/newsfragments/632.bugfix.rst

Co-authored-by: Adi Roiban <[email protected]>

* CR

* Update src/towncrier/_builder.py

* Update src/towncrier/_builder.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Adi Roiban <[email protected]>
  • Loading branch information
3 people authored Jul 31, 2024
1 parent 68a7a3b commit 67c9fd2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Top level keys

``towncrier check`` will fail if there are any news fragment files that have invalid filenames, except for those in the list. ``towncrier build`` will likewise fail, but only if this list has been configured (set to an empty list if there are no files to ignore).

Some filenames such as ``.gitignore`` and ``README`` are automatically ignored. However, if a custom template is stored in the news fragment directory, you should add it to this list.
Some filenames such as .gitignore, README.rst. README.md, and the template file, are automatically ignored.

``None`` by default.

Expand Down
5 changes: 5 additions & 0 deletions src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ def find_fragments(
If strict, raise ClickException if any fragments have an invalid name.
"""
ignored_files = {".gitignore", ".keep", "readme", "readme.md", "readme.rst"}
if isinstance(config.template, str):
# Template can be a tuple of (package_name, resource_name).
#
# See https://github.com/twisted/towncrier/issues/634
ignored_files.add(config.template)
if config.ignore:
ignored_files.update(filename.lower() for filename in config.ignore)

Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/632.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When the template file is stored in the same directory with the news fragments, it is automatically ignored when checking for valid fragment file names.
23 changes: 23 additions & 0 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,29 @@ def test_invalid_fragment_name(self, runner):
self.assertEqual(1, result.exit_code, result.output)
self.assertIn("Invalid news fragment name: feature.124", result.output)

@with_project(
config="""
[tool.towncrier]
package = "foo"
template = "foo/newsfragments/template.jinja"
"""
)
def test_ignored_template_string(self, runner):
"""
Files used in `template` are automatically ignored.
"""
with open("foo/newsfragments/123.feature", "w") as f:
f.write("This has valid filename (control case)")
with open("foo/newsfragments/template.jinja", "w") as f:
f.write("Template file should be automatically ignored")
with open("foo/newsfragments/.gitignore", "w") as f:
f.write("gitignore is automatically ignored")

result = runner.invoke(
_main, ["--draft", "--date", "01-01-2001", "--version", "1.0.0"]
)
self.assertEqual(0, result.exit_code, result.output)

@with_project()
def test_no_ignore_configured(self, runner):
"""
Expand Down

0 comments on commit 67c9fd2

Please sign in to comment.