From 52570dfa93d81ab66e0ea4497590f268aae445ae Mon Sep 17 00:00:00 2001 From: Conner Crosby Date: Sat, 2 Dec 2023 02:01:09 -0500 Subject: [PATCH] Fix role deps check for detecting path names --- .github/workflows/tox.yml | 2 +- .../roles/role_with_deps_paths/meta/main.yml | 9 +++++ src/ansiblelint/rules/role_name.py | 37 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 examples/roles/role_with_deps_paths/meta/main.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 5458bdb90f..0c15f43e16 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -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: diff --git a/examples/roles/role_with_deps_paths/meta/main.yml b/examples/roles/role_with_deps_paths/meta/main.yml new file mode 100644 index 0000000000..b0ea37f1cd --- /dev/null +++ b/examples/roles/role_with_deps_paths/meta/main.yml @@ -0,0 +1,9 @@ +--- +dependencies: + - role: subfolder/1st_role + vars: + param: baz + - role: subfolder + vars: + param: baz + - role: subfolder/2nd_role diff --git a/src/ansiblelint/rules/role_name.py b/src/ansiblelint/rules/role_name.py index a15f95538e..0212aa5a6d 100644 --- a/src/ansiblelint/rules/role_name.py +++ b/src/ansiblelint/rules/role_name.py @@ -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: @@ -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