Skip to content

Commit

Permalink
Avoid false positives for handler in roles handlers directory (#3838)
Browse files Browse the repository at this point in the history
Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
audgirka and ssbarnea committed Oct 17, 2023
1 parent 721d1d5 commit bf43367
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Expand Up @@ -70,7 +70,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 833
PYTEST_REQPASS: 834
steps:
- uses: actions/checkout@v4
with:
Expand Down
8 changes: 8 additions & 0 deletions examples/roles/role_with_handler/handlers/main.yml
@@ -0,0 +1,8 @@
---
- name: Debug
loop: "{{ _something_done.results }}"
loop_control:
label: "{{ item.item.name }}"
when: item.changed
ansible.builtin.debug:
msg: "{{ item.item.name }} changed"
17 changes: 17 additions & 0 deletions examples/roles/role_with_handler/tasks/main.yml
@@ -0,0 +1,17 @@
---
- name: Get info
delegate_to: localhost
register: collected_info
ansible.builtin.debug:
msg: test

- name: Do something
delegate_to: localhost
loop: "{{ collected_info['some_list'] }}"
loop_control:
label: "{{ item.name }}"
notify:
- Debug
register: _something_done
ansible.builtin.debug:
msg: test2
8 changes: 8 additions & 0 deletions src/ansiblelint/rules/no_handler.py
Expand Up @@ -89,6 +89,7 @@ def matchtask(
# pylint: disable=ungrouped-imports
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner
from ansiblelint.testing import run_ansible_lint

@pytest.mark.parametrize(
("test_file", "failures"),
Expand All @@ -107,3 +108,10 @@ def test_no_handler(
assert len(results) == failures
for result in results:
assert result.tag == "no-handler"

def test_role_with_handler() -> None:
"""Test role with handler."""
role_path = "examples/roles/role_with_handler"

results = run_ansible_lint("-v", role_path)
assert "no-handler" not in results.stdout
7 changes: 6 additions & 1 deletion src/ansiblelint/utils.py
Expand Up @@ -755,7 +755,12 @@ def skip_tags(self) -> list[str]:

def is_handler(self) -> bool:
"""Return true for tasks that are handlers."""
return ".handlers[" in self.position
is_handler_file = False
if isinstance(self._normalized_task, dict):
file_name = str(self._normalized_task["action"].get(FILENAME_KEY, None))
if file_name:
is_handler_file = "handlers" in str(file_name)
return is_handler_file if is_handler_file else ".handlers[" in self.position

def __repr__(self) -> str:
"""Return a string representation of the task."""
Expand Down

0 comments on commit bf43367

Please sign in to comment.