Skip to content

Commit

Permalink
Don't raise name[casing] on handlers with special role syntax (#4196)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos authored May 31, 2024
1 parent fc87a86 commit bd696d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
8 changes: 8 additions & 0 deletions examples/playbooks/name_case_notify_fail.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
- my handler
- my other handler

- name: Only one of these is invalid
ansible.builtin.debug:
msg: foo
changed_when: true
notify:
- "role_name : lowercase handler"
- "role_name : Uppercase handler"

handlers:
- name: My handler
ansible.builtin.debug:
Expand Down
25 changes: 13 additions & 12 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def matchtask(
if notify:
if isinstance(notify, str):
notify = [notify]

results.extend(
[
self.create_matcherror(
Expand All @@ -106,9 +107,7 @@ def matchtask(
filename=file,
)
for handler in notify
if handler[0].isalpha()
and handler[0].islower()
and not handler[0].isupper()
if check_handler_case(handler)
],
)
return results
Expand Down Expand Up @@ -280,6 +279,14 @@ def update_task_name(task_name: str) -> str:
match.fixed = True


def check_handler_case(handler: str) -> bool:
"""Check the casing of a handler."""
# Handlers may be prefixed with "role_name : " to indicate a handler from a specific role
# Strip this before checking
handler = handler.split(" : ", 1)[-1]
return handler[0].isalpha() and handler[0].islower() and not handler[0].isupper()


if "pytest" in sys.modules:
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner
Expand Down Expand Up @@ -379,15 +386,9 @@ def test_rule_notify_lowercase() -> None:
failure = "examples/playbooks/name_case_notify_fail.yml"
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
assert len(errs) == 4
assert errs[0].tag == "name[casing]"
assert errs[1].tag == "name[casing]"
assert errs[2].tag == "name[casing]"
assert errs[3].tag == "name[casing]"
assert errs[0].rule.id == "name"
assert errs[1].rule.id == "name"
assert errs[2].rule.id == "name"
assert errs[3].rule.id == "name"
assert len(errs) == 5
assert all(err.tag == "name[casing]" for err in errs)
assert all(err.rule.id == "name" for err in errs)

def test_name_play() -> None:
"""Positive test for name[play]."""
Expand Down

0 comments on commit bd696d2

Please sign in to comment.