Skip to content

Commit 7ec6d6b

Browse files
Avoid caching on role_name regex (#2876)Co-authored-by: Sorin Sbarnea <[email protected]>Co-authored-by: Sorin Sbarnea <[email protected]>
suppressing output affects repeated runs such as when using the `progressive` option Co-authored-by: Sorin Sbarnea <[email protected]>
1 parent 386d63d commit 7ec6d6b

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/ansiblelint/rules/role_name.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from __future__ import annotations
2323

2424
import re
25+
from functools import cache
2526
from pathlib import Path
2627
from typing import TYPE_CHECKING, Any
2728

@@ -34,13 +35,18 @@
3435
from ansiblelint.errors import MatchError
3536

3637

37-
ROLE_NAME_REGEX = r"^[a-z][a-z0-9_]*$"
38+
ROLE_NAME_REGEX = re.compile(r"^[a-z][a-z0-9_]*$")
3839

3940

4041
def _remove_prefix(text: str, prefix: str) -> str:
4142
return re.sub(rf"^{re.escape(prefix)}", "", text)
4243

4344

45+
@cache
46+
def _match_role_name_regex(role_name: str) -> bool:
47+
return ROLE_NAME_REGEX.match(role_name) is not None
48+
49+
4450
class RoleNames(AnsibleLintRule):
4551
# Unable to use f-strings due to flake8 bug with AST parsing
4652
"""Role name {0} does not match ``^[a-z][a-z0-9_]*$`` pattern."""
@@ -52,14 +58,9 @@ class RoleNames(AnsibleLintRule):
5258
)
5359
link = "https://docs.ansible.com/ansible/devel/dev_guide/developing_collections_structure.html#roles-directory"
5460
severity = "HIGH"
55-
done: list[str] = [] # already noticed roles list
5661
tags = ["deprecations", "metadata"]
5762
version_added = "v6.8.5"
5863

59-
def __init__(self) -> None:
60-
"""Save precompiled regex."""
61-
self._re = re.compile(ROLE_NAME_REGEX)
62-
6364
def matchtask(
6465
self, task: dict[str, Any], file: Lintable | None = None
6566
) -> list[MatchError]:
@@ -95,15 +96,13 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
9596
)
9697

9798
role_name = _remove_prefix(role_name, "ansible-role-")
98-
if role_name not in self.done:
99-
self.done.append(role_name)
100-
if role_name and not self._re.match(role_name):
101-
result.append(
102-
self.create_matcherror(
103-
filename=file,
104-
message=self.shortdesc.format(role_name),
105-
)
99+
if role_name and not _match_role_name_regex(role_name):
100+
result.append(
101+
self.create_matcherror(
102+
filename=file,
103+
message=self.shortdesc.format(role_name),
106104
)
105+
)
107106
return result
108107

109108
@staticmethod

0 commit comments

Comments
 (0)