Skip to content

Commit a6766a0

Browse files
Avoid false positive with name[casing] (#2800)
* Avoid false positive with name[casing] Fixes: #2789 * chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent bed28bc commit a6766a0

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ test/eco
6767
docs/profiles.md
6868
test/schemas/node_modules
6969
.envrc
70+
collections

examples/playbooks/tasks/rule-name-prefix-fail.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
- name: name | This prefix is incomplete
99
ansible.builtin.assert:
1010
that: true
11-
- name: rule-name-prefix-fail | This is correctly named
11+
- name: rule-name-prefix-fail | This is correctly | named too
1212
ansible.builtin.assert:
1313
that: true

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ junit_suite_name = ansible_lint_test_suite
3636
minversion = 4.6.6
3737
norecursedirs =
3838
build
39+
collections
3940
dist
4041
docs
4142
src/ansible_lint.egg-info

src/ansiblelint/rules/name.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,43 +68,23 @@ def matchtask(
6868
name, lintable=file, linenumber=task[LINE_NUMBER_KEY]
6969
)
7070
)
71-
task_name = name.split("|")
72-
if len(task_name) > 1:
73-
name = task_name[1].strip()
74-
results.extend(
75-
self._check_name(name, lintable=file, linenumber=task[LINE_NUMBER_KEY])
76-
)
7771
return results
7872

7973
def _prefix_check(
8074
self, name: str, lintable: Lintable | None, linenumber: int
8175
) -> list[MatchError]:
8276

8377
results: list[MatchError] = []
78+
effective_name = name
8479
if lintable is None:
8580
return []
8681

87-
if self._collection:
88-
prefix = self._collection.options.task_name_prefix.format(
89-
stem=lintable.path.stem
82+
if not results:
83+
results.extend(
84+
self._check_name(
85+
effective_name, lintable=lintable, linenumber=linenumber
86+
)
9087
)
91-
if (
92-
lintable.kind == "tasks"
93-
and lintable.path.stem != "main"
94-
and not name.startswith(prefix)
95-
):
96-
# For the moment in order to raise errors this rule needs to be
97-
# enabled manually. Still, we do allow use of prefixes even without
98-
# having to enable the rule.
99-
if "name[prefix]" in self._collection.options.enable_list:
100-
results.append(
101-
self.create_matcherror(
102-
message=f"Task name should start with '{prefix}'.",
103-
linenumber=linenumber,
104-
tag="name[prefix]",
105-
filename=lintable,
106-
)
107-
)
10888
return results
10989

11090
def _check_name(
@@ -114,7 +94,37 @@ def _check_name(
11494
# lowercase letter, so we ignore anything else. On Unicode isupper()
11595
# is not necessarily the opposite of islower()
11696
results = []
117-
if name[0].isalpha() and name[0].islower() and not name[0].isupper():
97+
# stage one check prefix
98+
effective_name = name
99+
if self._collection and lintable:
100+
prefix = self._collection.options.task_name_prefix.format(
101+
stem=lintable.path.stem
102+
)
103+
if lintable.kind == "tasks" and lintable.path.stem != "main":
104+
if not name.startswith(prefix):
105+
# For the moment in order to raise errors this rule needs to be
106+
# enabled manually. Still, we do allow use of prefixes even without
107+
# having to enable the rule.
108+
if "name[prefix]" in self._collection.options.enable_list:
109+
results.append(
110+
self.create_matcherror(
111+
message=f"Task name should start with '{prefix}'.",
112+
linenumber=linenumber,
113+
tag="name[prefix]",
114+
filename=lintable,
115+
)
116+
)
117+
return results
118+
else:
119+
effective_name = name[len(prefix) :]
120+
# breakpoint()
121+
122+
if (
123+
effective_name[0].isalpha()
124+
and effective_name[0].islower()
125+
and not effective_name[0].isupper()
126+
):
127+
# breakpoint()
118128
results.append(
119129
self.create_matcherror(
120130
message="All names should start with an uppercase letter.",
@@ -171,6 +181,7 @@ def test_name_prefix_negative() -> None:
171181
bad_runner = Runner(failure, rules=collection)
172182
results = bad_runner.run()
173183
assert len(results) == 3
184+
# , "\n".join(results)
174185
assert results[0].tag == "name[casing]"
175186
assert results[1].tag == "name[prefix]"
176187
assert results[2].tag == "name[prefix]"

src/ansiblelint/schemas/__store__.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ansible-lint-config": {
3-
"etag": "1e2610f0a8c2c7f88cce3a24964e37b15c69075af665d8ca7ce8de4f5acef526",
3+
"etag": "9ee4e29fc03cdf2edf8c7a93667cab776d7cc5d47ce1c9950ac315e0af0249e2",
44
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible-lint-config.json"
55
},
66
"ansible-navigator-config": {

0 commit comments

Comments
 (0)