Skip to content

Commit

Permalink
Fix for var-naming rule to not break on include_tasks and vars (#3458)
Browse files Browse the repository at this point in the history
  • Loading branch information
audgirka committed May 18, 2023
1 parent 50bdeca commit 0786920
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Expand Up @@ -59,7 +59,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 797
PYTEST_REQPASS: 798
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
4 changes: 4 additions & 0 deletions examples/playbooks/tasks/included-task-with-vars.yml
@@ -0,0 +1,4 @@
---
- name: included-task-with-vars | Test
ansible.builtin.debug:
msg: "{{ foo }}"
@@ -0,0 +1,5 @@
---
- name: include_task_with_vars | Foo
ansible.builtin.include_tasks: ../tasks/included-task-with-vars.yml
vars:
var_naming_pattern_foo: bar
12 changes: 11 additions & 1 deletion src/ansiblelint/rules/var_naming.py
Expand Up @@ -10,7 +10,7 @@
from ansible.vars.reserved import get_reserved_names

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 @@ -49,6 +49,9 @@ def get_var_naming_matcherror(
rule=self,
)

if ident in ANNOTATION_KEYS:
return None

try:
ident.encode("ascii")
except UnicodeEncodeError:
Expand Down Expand Up @@ -264,3 +267,10 @@ def test_var_naming_with_pattern() -> None:
)
assert result.returncode == RC.SUCCESS
assert "var-naming" not in result.stdout

def test_var_naming_with_include_tasks_and_vars() -> None:
"""Test with include tasks and vars."""
role_path = "examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml"
result = run_ansible_lint(role_path)
assert result.returncode == RC.SUCCESS
assert "var-naming" not in result.stdout
3 changes: 1 addition & 2 deletions src/ansiblelint/yaml_utils.py
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 0786920

Please sign in to comment.