diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 1f7c35c43a..2ee2c8c195 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -71,7 +71,7 @@ jobs: WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY # Number of expected test passes, safety measure for accidental skip of # tests. Update value if you add/remove tests. - PYTEST_REQPASS: 825 + PYTEST_REQPASS: 826 steps: - name: Activate WSL1 if: "contains(matrix.shell, 'wsl')" diff --git a/examples/roles/role_vars_prefix_detection/defaults/main.yml b/examples/roles/role_vars_prefix_detection/defaults/main.yml new file mode 100644 index 0000000000..23809fe068 --- /dev/null +++ b/examples/roles/role_vars_prefix_detection/defaults/main.yml @@ -0,0 +1,2 @@ +--- +foo: bar diff --git a/examples/roles/role_vars_prefix_detection/vars/main.yml b/examples/roles/role_vars_prefix_detection/vars/main.yml new file mode 100644 index 0000000000..143ee723b9 --- /dev/null +++ b/examples/roles/role_vars_prefix_detection/vars/main.yml @@ -0,0 +1,3 @@ +--- +role_vars_prefix_detection_bar: baz +bar: baz diff --git a/src/ansiblelint/rules/var_naming.py b/src/ansiblelint/rules/var_naming.py index 73d7fb44b5..33abb489a4 100644 --- a/src/ansiblelint/rules/var_naming.py +++ b/src/ansiblelint/rules/var_naming.py @@ -272,7 +272,8 @@ def matchyaml(self, file: Lintable) -> list[MatchError]: if str(file.kind) == "vars" and file.data: meta_data = parse_yaml_from_file(str(file.path)) for key in meta_data: - match_error = self.get_var_naming_matcherror(key) + prefix = file.role if file.role else "" + match_error = self.get_var_naming_matcherror(key, prefix=prefix) if match_error: match_error.filename = filename match_error.lineno = key.ansible_pos[1] @@ -347,6 +348,18 @@ def test_invalid_var_name_varsfile( assert result.tag == expected_errors[idx][0] assert result.lineno == expected_errors[idx][1] + def test_var_naming_with_role_prefix( + default_rules_collection: RulesCollection, + ) -> None: + """Test rule matches.""" + results = Runner( + Lintable("examples/roles/role_vars_prefix_detection"), + rules=default_rules_collection, + ).run() + assert len(results) == 2 + for result in results: + assert result.tag == "var-naming[no-role-prefix]" + def test_var_naming_with_pattern() -> None: """Test rule matches.""" role_path = "examples/roles/var_naming_pattern/tasks/main.yml"