Skip to content

Commit

Permalink
Raise name[casing] violation for notify task param (#4149)
Browse files Browse the repository at this point in the history
Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
Co-authored-by: Ansible Bot <107943535+ansibuddy@users.noreply.github.com>
  • Loading branch information
3 people committed May 13, 2024
1 parent 0d3f69c commit 23c046b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 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: 868
PYTEST_REQPASS: 869
steps:
- uses: actions/checkout@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion examples/playbooks/become.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
become: true
become_user: nobody
notify:
- restart apache2
- Restart apache2
26 changes: 26 additions & 0 deletions examples/playbooks/name_case_notify_fail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name: Test Playbook
hosts: localhost
tasks:
- name: Task that always changes
ansible.builtin.debug:
msg: foo
changed_when: true
notify: my handler

- name: Task that always changes
ansible.builtin.debug:
msg: foo
changed_when: true
notify:
- my handler
- my other handler

handlers:
- name: My handler
ansible.builtin.debug:
msg: bar

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

notify = task.get("notify")
if notify:
if isinstance(notify, str):
notify = [notify]
results.extend(
[
self.create_matcherror(
message=f"Task notify '{handler}' should start with an uppercase letter.",
lineno=task[LINE_NUMBER_KEY],
tag="name[casing]",
filename=file,
)
for handler in notify
if handler[0].isalpha()
and handler[0].islower()
and not handler[0].isupper()
],
)
return results

def _prefix_check(
Expand Down Expand Up @@ -352,6 +371,23 @@ 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) == 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"

def test_name_play() -> None:
"""Positive test for name[play]."""
collection = RulesCollection()
Expand Down
2 changes: 1 addition & 1 deletion test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def fixture_runner_result(
id="strings",
),
pytest.param("examples/playbooks/vars/empty.yml", 1, False, True, id="empty"),
pytest.param("examples/playbooks/name-case.yml", 2, True, True, id="name_case"),
pytest.param("examples/playbooks/name-case.yml", 6, True, True, id="name_case"),
pytest.param("examples/playbooks/fqcn.yml", 3, True, True, id="fqcn"),
pytest.param(
"examples/playbooks/multi_yaml_doc.yml",
Expand Down

0 comments on commit 23c046b

Please sign in to comment.