Skip to content

Commit

Permalink
Fix for var-naming rule to not break on include_tasks and variables u…
Browse files Browse the repository at this point in the history
…sed with it
  • Loading branch information
audgirka committed May 18, 2023
1 parent 02804cd commit 3783847
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
71 changes: 35 additions & 36 deletions src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ansible.parsing.yaml.objects import AnsibleUnicode

from ansiblelint.config import options
from ansiblelint.constants import LINE_NUMBER_KEY, RC
from ansiblelint.constants import ANNOTATION_KEYS, LINE_NUMBER_KEY, RC
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule, RulesCollection
Expand Down Expand Up @@ -46,44 +46,44 @@ def get_var_naming_matcherror(
message="Variables names must be strings.",
rule=self,
)
if ident not in ANNOTATION_KEYS:
try:
ident.encode("ascii")
except UnicodeEncodeError:
return MatchError(
tag="var-naming[non-ascii]",
message="Variables names must be ASCII.",
rule=self,
)

try:
ident.encode("ascii")
except UnicodeEncodeError:
return MatchError(
tag="var-naming[non-ascii]",
message="Variables names must be ASCII.",
rule=self,
)

if keyword.iskeyword(ident):
return MatchError(
tag="var-naming[no-keyword]",
message="Variables names must not be Python keywords.",
rule=self,
)
if keyword.iskeyword(ident):
return MatchError(
tag="var-naming[no-keyword]",
message="Variables names must not be Python keywords.",
rule=self,
)

# We want to allow use of jinja2 templating for variable names
if "{{" in ident:
return MatchError(
tag="var-naming[no-jinja]",
message="Variables names must not contain jinja2 templating.",
rule=self,
)
# We want to allow use of jinja2 templating for variable names
if "{{" in ident:
return MatchError(
tag="var-naming[no-jinja]",
message="Variables names must not contain jinja2 templating.",
rule=self,
)

if not bool(self.re_pattern.match(ident)):
return MatchError(
tag="var-naming[pattern]",
message=f"Variables names should match {self.re_pattern_str} regex.",
rule=self,
)
if not bool(self.re_pattern.match(ident)):
return MatchError(
tag="var-naming[pattern]",
message=f"Variables names should match {self.re_pattern_str} regex.",
rule=self,
)

if prefix and not ident.startswith(f"{prefix}_"):
return MatchError(
tag="var-naming[no-role-prefix]",
message="Variables names from within roles should use role_name_ as a prefix.",
rule=self,
)
if prefix and not ident.startswith(f"{prefix}_"):
return MatchError(
tag="var-naming[no-role-prefix]",
message="Variables names from within roles should use role_name_ as a prefix.",
rule=self,
)
return None

def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
Expand Down Expand Up @@ -140,7 +140,6 @@ def matchtask(
results.append(match_error)

# If the task uses the 'set_fact' module
# breakpoint()
ansible_module = task["action"]["__ansible_module__"]
if ansible_module == "set_fact":
for key in filter(
Expand Down
3 changes: 1 addition & 2 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
ANNOTATION_KEYS,
NESTED_TASK_KEYS,
PLAYBOOK_TASK_KEYWORDS,
SKIPPED_RULES_KEY,
)
from ansiblelint.utils import Task

Expand Down Expand Up @@ -212,7 +211,7 @@ def _nested_items_path(
msg = f"Expected a dict or a list but got {data_collection!r} of type '{type(data_collection)}'"
raise TypeError(msg)
for key, value in convert_data_collection_to_tuples():
if key in (SKIPPED_RULES_KEY, "__file__", "__line__", *ignored_keys):
if key in (*ANNOTATION_KEYS, *ignored_keys):
continue
yield key, value, parent_path
if isinstance(value, (dict, list)):
Expand Down

0 comments on commit 3783847

Please sign in to comment.