Skip to content

Commit

Permalink
Add name[casing] to identify wrongly capitalized task names
Browse files Browse the repository at this point in the history
Fixes: #2169
  • Loading branch information
ssbarnea committed Aug 9, 2022
1 parent 8fc33cd commit 86dc4e9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:TOX_PARALLEL_NO_SPINNER
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 658
PYTEST_REQPASS: 659

steps:
- name: Activate WSL1
Expand Down
6 changes: 6 additions & 0 deletions examples/playbooks/task-name-lowercase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- hosts: all
tasks:
- name: this task has a name is not correctly capitalized
ansible.builtin.command: echo "Hello World"
changed_when: false
24 changes: 24 additions & 0 deletions src/ansiblelint/rules/name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## name

This rule identifies several problems related to task naming. This is important
because these names are the primary way to identify executed tasks on console,
logs or web interface. Their role is also to document what a task is supposed
to do.

- `name[missing]` - All tasks are required to have a name
- `name[casing]` - All task names should start with a capital letter

### Problematic code

```yaml
---
- ansible.builtin.command: touch /tmp/.placeholder
```

### Correct code

```yaml
---
- name: Create placeholder file
ansible.builtin.command: touch /tmp/.placeholder
```
28 changes: 25 additions & 3 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class NameRule(AnsibleLintRule):
"""All tasks should be named."""
"""Rule for checking task names and their content."""

id = "name"
description = (
Expand All @@ -26,9 +26,20 @@ class NameRule(AnsibleLintRule):
def matchtask(
self, task: Dict[str, Any], file: "Optional[Lintable]" = None
) -> Union[bool, str, MatchError]:
if not task.get("name"):
name = task.get("name")
if not name:
return self.create_matcherror(
linenumber=task["__line__"], tag="name[missing]", filename=file
message="All tasks should be named.",
linenumber=task["__line__"],
tag="name[missing]",
filename=file,
)
if not name[0].isupper():
return self.create_matcherror(
message="All names should start with an uppercase letter.",
linenumber=task["__line__"],
tag="name[casing]",
filename=file,
)
return False

Expand All @@ -54,3 +65,14 @@ def test_file_negative() -> None:
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
assert len(errs) == 4

def test_rule_name_lowercase() -> None:
"""Negative test for a task that starts with lowercase."""
collection = RulesCollection()
collection.register(NameRule())
failure = "examples/playbooks/task-name-lowercase.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"

0 comments on commit 86dc4e9

Please sign in to comment.