-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement unamed-task rule as name[missing]
This reimplementation will allow us to implement closely related extra checks. Related: #2171 #2170 #2169 #2036
- Loading branch information
Showing
10 changed files
with
73 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters