Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for var-naming rule to not break on include_tasks and vars #3458

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- name: included-task-with-vars | Test
ansible.builtin.debug:
msg: "{{ foo }}"
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
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