Skip to content

Commit

Permalink
Reimplement unamed-task rule as name[missing]
Browse files Browse the repository at this point in the history
This reimplementation will allow us to implement closely related
extra checks.

Related: #2171 #2170 #2169 #2036
  • Loading branch information
ssbarnea committed Aug 9, 2022
1 parent a5f0d52 commit ee1a8eb
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 82 deletions.
10 changes: 6 additions & 4 deletions examples/playbooks/task-has-name-failure.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
- hosts: all
tasks:
- ansible.builtin.command: echo "no name"
- name:
- ansible.builtin.command: echo "no name" # <-- 1
changed_when: false
- name: "" # <-- 2
ansible.builtin.command: echo "empty name"
- ansible.builtin.debug:
changed_when: false
- ansible.builtin.debug: # <-- 3
msg: Debug without a name
- ansible.builtin.meta: flush_handlers
- ansible.builtin.meta: flush_handlers # <-- 4
1 change: 1 addition & 0 deletions src/ansiblelint/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def main():
"704": "meta-video-links",
"911": "syntax-check",
"var-spacing": "jinja[spacing]",
"unnamed-task": "name[missing]",
}

PLAYBOOK_TASK_KEYWORDS = [
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/data/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ basic:
partial-become:
playbook-extension:
role-name:
unnamed-task:
name:
var-naming:
yaml:
moderate:
Expand Down
58 changes: 58 additions & 0 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Implementation of NameRule."""
import sys
from typing import TYPE_CHECKING, Any, Dict, Union

from ansiblelint.errors import MatchError
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from typing import Optional

from ansiblelint.file_utils import Lintable


class NameRule(AnsibleLintRule):
"""All tasks should be named."""

id = "name"
description = (
"All tasks should have a distinct name for readability "
"and for ``--start-at-task`` to work"
)
severity = "MEDIUM"
tags = ["idiom"]
version_added = "historic"

def matchtask(
self, task: Dict[str, Any], file: "Optional[Lintable]" = None
) -> Union[bool, str, MatchError]:
if not task.get("name"):
return self.create_matcherror(
linenumber=task["__line__"], tag="name[missing]", filename=file
)
return False


if "pytest" in sys.modules: # noqa: C901

from ansiblelint.rules import RulesCollection

# from ansiblelint.rules.name import NameRule
from ansiblelint.runner import Runner

def test_file_positive() -> None:
"""Positive test for unnamed-task."""
collection = RulesCollection()
collection.register(NameRule())
success = "examples/playbooks/task-has-name-success.yml"
good_runner = Runner(success, rules=collection)
assert [] == good_runner.run()

def test_file_negative() -> None:
"""Negative test for unnamed-task."""
collection = RulesCollection()
collection.register(NameRule())
failure = "examples/playbooks/task-has-name-failure.yml"
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
assert len(errs) == 4
48 changes: 0 additions & 48 deletions src/ansiblelint/rules/unnamed_task.py

This file was deleted.

23 changes: 0 additions & 23 deletions test/rules/test_unnamed_task.py

This file was deleted.

2 changes: 1 addition & 1 deletion test/test_import_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ def test_task_hook_import_playbook(default_rules_collection: RulesCollection) ->
assert len(results) == 2
# Assures we detected the issues from imported playbook
assert "Commands should not change things" in results_text
assert "unnamed-task" in results_text
assert "[name]" in results_text
assert "All tasks should be named" in results_text
3 changes: 2 additions & 1 deletion test/test_skip_inside_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def test_role_tasks(default_text_runner: RunFromText) -> None:
results = default_text_runner.run_role_tasks_main(ROLE_TASKS)
assert len(results) == 1, results
assert results[0].linenumber == 2
assert results[0].rule.id == "unnamed-task"
assert results[0].tag == "name[missing]"
assert results[0].rule.id == "name"


def test_role_tasks_with_block(default_text_runner: RunFromText) -> None:
Expand Down
6 changes: 3 additions & 3 deletions test/test_skiputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,19 @@ def test_playbook_noqa(default_text_runner: RunFromText) -> None:
"__file__": Path(
"examples/playbooks/noqa-nested.yml"
),
"skipped_rules": ["unnamed-task"],
"skipped_rules": ["name[missing]"],
}
],
"__line__": 6,
"__file__": Path(
"examples/playbooks/noqa-nested.yml"
),
"skipped_rules": ["unnamed-task"],
"skipped_rules": ["name[missing]"],
}
],
"__line__": 4,
"__file__": Path("examples/playbooks/noqa-nested.yml"),
"skipped_rules": ["unnamed-task"],
"skipped_rules": ["name[missing]"],
}
],
"__line__": 2,
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ deps =
setenv =
PIP_CONSTRAINT = {toxinidir}/requirements.txt
commands =
sh -c "rm -f docs/pkg/*.rst"
sh -c "rm -f {toxinidir}/docs/pkg/*.rst"
{envpython} -m sphinx \
-j auto \
-b linkcheck \
Expand Down

0 comments on commit ee1a8eb

Please sign in to comment.