Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid false positive with name[casing] #2800

Merged
merged 2 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ test/eco
docs/profiles.md
test/schemas/node_modules
.envrc
collections
2 changes: 1 addition & 1 deletion examples/playbooks/tasks/rule-name-prefix-fail.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
- name: name | This prefix is incomplete
ansible.builtin.assert:
that: true
- name: rule-name-prefix-fail | This is correctly named
- name: rule-name-prefix-fail | This is correctly | named too
ansible.builtin.assert:
that: true
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ junit_suite_name = ansible_lint_test_suite
minversion = 4.6.6
norecursedirs =
build
collections
dist
docs
src/ansible_lint.egg-info
Expand Down
65 changes: 38 additions & 27 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,23 @@ def matchtask(
name, lintable=file, linenumber=task[LINE_NUMBER_KEY]
)
)
task_name = name.split("|")
if len(task_name) > 1:
name = task_name[1].strip()
results.extend(
self._check_name(name, lintable=file, linenumber=task[LINE_NUMBER_KEY])
)
return results

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

results: list[MatchError] = []
effective_name = name
if lintable is None:
return []

if self._collection:
prefix = self._collection.options.task_name_prefix.format(
stem=lintable.path.stem
if not results:
results.extend(
self._check_name(
effective_name, lintable=lintable, linenumber=linenumber
)
)
if (
lintable.kind == "tasks"
and lintable.path.stem != "main"
and not name.startswith(prefix)
):
# For the moment in order to raise errors this rule needs to be
# enabled manually. Still, we do allow use of prefixes even without
# having to enable the rule.
if "name[prefix]" in self._collection.options.enable_list:
results.append(
self.create_matcherror(
message=f"Task name should start with '{prefix}'.",
linenumber=linenumber,
tag="name[prefix]",
filename=lintable,
)
)
return results

def _check_name(
Expand All @@ -114,7 +94,37 @@ def _check_name(
# lowercase letter, so we ignore anything else. On Unicode isupper()
# is not necessarily the opposite of islower()
results = []
if name[0].isalpha() and name[0].islower() and not name[0].isupper():
# stage one check prefix
effective_name = name
if self._collection and lintable:
prefix = self._collection.options.task_name_prefix.format(
stem=lintable.path.stem
)
if lintable.kind == "tasks" and lintable.path.stem != "main":
if not name.startswith(prefix):
# For the moment in order to raise errors this rule needs to be
# enabled manually. Still, we do allow use of prefixes even without
# having to enable the rule.
if "name[prefix]" in self._collection.options.enable_list:
results.append(
self.create_matcherror(
message=f"Task name should start with '{prefix}'.",
linenumber=linenumber,
tag="name[prefix]",
filename=lintable,
)
)
return results
else:
effective_name = name[len(prefix) :]
# breakpoint()

if (
effective_name[0].isalpha()
and effective_name[0].islower()
and not effective_name[0].isupper()
):
# breakpoint()
results.append(
self.create_matcherror(
message="All names should start with an uppercase letter.",
Expand Down Expand Up @@ -171,6 +181,7 @@ def test_name_prefix_negative() -> None:
bad_runner = Runner(failure, rules=collection)
results = bad_runner.run()
assert len(results) == 3
# , "\n".join(results)
assert results[0].tag == "name[casing]"
assert results[1].tag == "name[prefix]"
assert results[2].tag == "name[prefix]"
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/schemas/__store__.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ansible-lint-config": {
"etag": "1e2610f0a8c2c7f88cce3a24964e37b15c69075af665d8ca7ce8de4f5acef526",
"etag": "9ee4e29fc03cdf2edf8c7a93667cab776d7cc5d47ce1c9950ac315e0af0249e2",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible-lint-config.json"
},
"ansible-navigator-config": {
Expand Down