Skip to content

Commit

Permalink
Avoid false positives for handler in roles
Browse files Browse the repository at this point in the history
  • Loading branch information
audgirka committed Oct 12, 2023
1 parent b00988c commit c64bc76
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
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: 832
PYTEST_REQPASS: 833
steps:
- uses: actions/checkout@v4
with:
Expand Down
8 changes: 8 additions & 0 deletions examples/roles/role_with_handler/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
14 changes: 13 additions & 1 deletion src/ansiblelint/rules/no_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ def matchtask(
task: Task,
file: Lintable | None = None,
) -> bool | str:
if task["__ansible_action_type__"] != "task" or task.is_handler():
if (
task["__ansible_action_type__"] != "task"
or task.is_handler()
or (file is not None and "handlers" in file.name)
):
return False

when = task.get("when")
Expand All @@ -89,6 +93,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 +112,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

0 comments on commit c64bc76

Please sign in to comment.