11
11
if TYPE_CHECKING :
12
12
from typing import Optional
13
13
14
+ from ansiblelint .constants import odict
14
15
from ansiblelint .file_utils import Lintable
15
16
16
17
17
18
class NameRule (AnsibleLintRule ):
18
- """Rule for checking task names and their content ."""
19
+ """Rule for checking task and play names ."""
19
20
20
21
id = "name"
21
22
description = (
22
- "All tasks should have a distinct name for readability "
23
+ "All tasks and plays should have a distinct name for readability "
23
24
"and for ``--start-at-task`` to work"
24
25
)
25
26
severity = "MEDIUM"
26
27
tags = ["idiom" ]
27
- version_added = "historic"
28
+ version_added = "v6.5.0 (last update)"
29
+
30
+ def matchplay (self , file : Lintable , data : odict [str , Any ]) -> list [MatchError ]:
31
+ """Return matches found for a specific play (entry in playbook)."""
32
+ if file .kind != "playbook" :
33
+ return []
34
+ if "name" not in data and not any (
35
+ key in data
36
+ for key in ["import_playbook" , "ansible.builtin.import_playbook" ]
37
+ ):
38
+ return [
39
+ self .create_matcherror (
40
+ message = "All plays should be named." ,
41
+ linenumber = data [LINE_NUMBER_KEY ],
42
+ tag = "name[play]" ,
43
+ filename = file ,
44
+ )
45
+ ]
46
+ match = self ._check_name (
47
+ data ["name" ], lintable = file , linenumber = data [LINE_NUMBER_KEY ]
48
+ )
49
+ if match :
50
+ return [match ]
51
+ return []
28
52
29
53
def matchtask (
30
54
self , task : dict [str , Any ], file : Lintable | None = None
@@ -37,14 +61,22 @@ def matchtask(
37
61
tag = "name[missing]" ,
38
62
filename = file ,
39
63
)
64
+ return (
65
+ self ._check_name (name , lintable = file , linenumber = task [LINE_NUMBER_KEY ])
66
+ or False
67
+ )
68
+
69
+ def _check_name (
70
+ self , name : str , lintable : Lintable | None , linenumber : int
71
+ ) -> MatchError | None :
40
72
if not name [0 ].isupper ():
41
73
return self .create_matcherror (
42
74
message = "All names should start with an uppercase letter." ,
43
- linenumber = task [ LINE_NUMBER_KEY ] ,
75
+ linenumber = linenumber ,
44
76
tag = "name[casing]" ,
45
- filename = file ,
77
+ filename = lintable ,
46
78
)
47
- return False
79
+ return None
48
80
49
81
50
82
if "pytest" in sys .modules : # noqa: C901
@@ -67,7 +99,7 @@ def test_file_negative() -> None:
67
99
failure = "examples/playbooks/task-has-name-failure.yml"
68
100
bad_runner = Runner (failure , rules = collection )
69
101
errs = bad_runner .run ()
70
- assert len (errs ) == 4
102
+ assert len (errs ) == 5
71
103
72
104
def test_rule_name_lowercase () -> None :
73
105
"""Negative test for a task that starts with lowercase."""
@@ -79,3 +111,13 @@ def test_rule_name_lowercase() -> None:
79
111
assert len (errs ) == 1
80
112
assert errs [0 ].tag == "name[casing]"
81
113
assert errs [0 ].rule .id == "name"
114
+
115
+ def test_name_play () -> None :
116
+ """Positive test for name[play]."""
117
+ collection = RulesCollection ()
118
+ collection .register (NameRule ())
119
+ success = "examples/playbooks/play-name-missing.yml"
120
+ errs = Runner (success , rules = collection ).run ()
121
+ assert len (errs ) == 1
122
+ assert errs [0 ].tag == "name[play]"
123
+ assert errs [0 ].rule .id == "name"
0 commit comments