Skip to content

Commit

Permalink
Fix role deps check for detecting path names (#3923)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavcrosby authored Dec 4, 2023
1 parent 79826a2 commit ca61bf8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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

0 comments on commit ca61bf8

Please sign in to comment.