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

Fix role deps check for detecting path names #3923

Merged
merged 1 commit into from
Dec 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 848
PYTEST_REQPASS: 849
steps:
- uses: actions/checkout@v4
with:
Expand Down
9 changes: 9 additions & 0 deletions examples/roles/role_with_deps_paths/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
dependencies:
- role: subfolder/1st_role
vars:
param: baz
- role: subfolder
vars:
param: baz
- role: subfolder/2nd_role
37 changes: 37 additions & 0 deletions src/ansiblelint/rules/role_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
if file.kind not in ("meta", "role", "playbook"):
return result

if file.kind == "meta":
for role in file.data["dependencies"]:
role_name = role["role"]
if "/" in role_name:
result.append(
self.create_matcherror(
f"Avoid using paths when importing roles. ({role_name})",
filename=file,
lineno=role["__line__"],
tag=f"{self.id}[path]",
),
)
return result

if file.kind == "playbook":
for play in file.data:
if "roles" in play:
Expand Down Expand Up @@ -169,3 +183,26 @@ def test_role_name_path(
for result in results:
assert result.tag == "role-name[path]"
assert len(results) == failure

@pytest.mark.parametrize(
("test_file", "failure"),
(pytest.param("examples/roles/role_with_deps_paths", 2, id="fail"),),
)
def test_role_deps_path_names(
default_rules_collection: RulesCollection,
test_file: str,
failure: int,
) -> None:
"""Test rule matches."""
results = Runner(
test_file,
rules=default_rules_collection,
).run()
expected_errors = (
("role-name[path]", 3),
("role-name[path]", 9),
)
for idx, result in enumerate(results):
assert result.tag == expected_errors[idx][0]
assert result.lineno == expected_errors[idx][1]
assert len(results) == failure