summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/no_handler.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/ansiblelint/rules/no_handler.py')
-rw-r--r--src/ansiblelint/rules/no_handler.py27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/ansiblelint/rules/no_handler.py b/src/ansiblelint/rules/no_handler.py
index 380fd61..ae8f820 100644
--- a/src/ansiblelint/rules/no_handler.py
+++ b/src/ansiblelint/rules/no_handler.py
@@ -69,25 +69,27 @@ class UseHandlerRatherThanWhenChangedRule(AnsibleLintRule):
task: Task,
file: Lintable | None = None,
) -> bool | str:
- if task["__ansible_action_type__"] != "task":
+ if task["__ansible_action_type__"] != "task" or task.is_handler():
return False
when = task.get("when")
+ result = False
if isinstance(when, list):
- if len(when) > 1:
- return False
- return _changed_in_when(when[0])
- if isinstance(when, str):
- return _changed_in_when(when)
- return False
+ if len(when) <= 1:
+ result = _changed_in_when(when[0])
+ elif isinstance(when, str):
+ result = _changed_in_when(when)
+ return result
if "pytest" in sys.modules:
import pytest
- from ansiblelint.rules import RulesCollection # pylint: disable=ungrouped-imports
- from ansiblelint.runner import Runner # pylint: disable=ungrouped-imports
+ # 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"),
@@ -106,3 +108,10 @@ if "pytest" in sys.modules:
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