Skip to content

Commit

Permalink
Avoid caching on role_name regex (#2876)Co-authored-by: Sorin Sbarnea…
Browse files Browse the repository at this point in the history
… <[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]>
  • Loading branch information
DanInProgress and ssbarnea authored Feb 1, 2023
1 parent 386d63d commit 7ec6d6b
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/ansiblelint/rules/role_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from __future__ import annotations

import re
from functools import cache
from pathlib import Path
from typing import TYPE_CHECKING, Any

Expand All @@ -34,13 +35,18 @@
from ansiblelint.errors import MatchError


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


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


@cache
def _match_role_name_regex(role_name: str) -> bool:
return ROLE_NAME_REGEX.match(role_name) is not None


class RoleNames(AnsibleLintRule):
# Unable to use f-strings due to flake8 bug with AST parsing
"""Role name {0} does not match ``^[a-z][a-z0-9_]*$`` pattern."""
Expand All @@ -52,14 +58,9 @@ class RoleNames(AnsibleLintRule):
)
link = "https://docs.ansible.com/ansible/devel/dev_guide/developing_collections_structure.html#roles-directory"
severity = "HIGH"
done: list[str] = [] # already noticed roles list
tags = ["deprecations", "metadata"]
version_added = "v6.8.5"

def __init__(self) -> None:
"""Save precompiled regex."""
self._re = re.compile(ROLE_NAME_REGEX)

def matchtask(
self, task: dict[str, Any], file: Lintable | None = None
) -> list[MatchError]:
Expand Down Expand Up @@ -95,15 +96,13 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
)

role_name = _remove_prefix(role_name, "ansible-role-")
if role_name not in self.done:
self.done.append(role_name)
if role_name and not self._re.match(role_name):
result.append(
self.create_matcherror(
filename=file,
message=self.shortdesc.format(role_name),
)
if role_name and not _match_role_name_regex(role_name):
result.append(
self.create_matcherror(
filename=file,
message=self.shortdesc.format(role_name),
)
)
return result

@staticmethod
Expand Down

0 comments on commit 7ec6d6b

Please sign in to comment.