Skip to content

Commit

Permalink
Raise name[casing] violation for notify task param (ansible#4035)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavcrosby committed May 11, 2024
1 parent f6d4702 commit 0856251
Show file tree
Hide file tree
Showing 4 changed files with 43 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 @@ -73,7 +73,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 860
PYTEST_REQPASS: 861
steps:
- uses: actions/checkout@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion examples/playbooks/name-case.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ansible.builtin.debug:
msg: I always change!
changed_when: true
notify: my handler
notify: My handler

- name: Task with notify as list
ansible.builtin.debug:
Expand Down
14 changes: 14 additions & 0 deletions examples/playbooks/name_case_notify_fail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: Test Playbook
hosts: localhost
tasks:
- name: Task that always changes
ansible.builtin.debug:
msg: foo
changed_when: true
notify: my handler

handlers:
- name: My handler
ansible.builtin.debug:
msg: bar
27 changes: 27 additions & 0 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ def matchtask(
lineno=task[LINE_NUMBER_KEY],
),
)

notify_name = task.get("notify")
if (
notify_name
and notify_name[0].isalpha()
and notify_name[0].islower()
and not notify_name[0].isupper()
):
results.append(
self.create_matcherror(
message="Task notify should start with an uppercase letter.",
lineno=task[LINE_NUMBER_KEY],
tag="name[casing]",
filename=file,
),
)
return results

def _prefix_check(
Expand Down Expand Up @@ -272,6 +288,17 @@ def test_rule_name_lowercase() -> None:
assert errs[0].tag == "name[casing]"
assert errs[0].rule.id == "name"

def test_rule_notify_lowercase() -> None:
"""Negative test for a task notify that starts with lowercase."""
collection = RulesCollection()
collection.register(NameRule())
failure = "examples/playbooks/name_case_notify_fail.yml"
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
assert len(errs) == 1
assert errs[0].tag == "name[casing]"
assert errs[0].rule.id == "name"

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

0 comments on commit 0856251

Please sign in to comment.