Skip to content

Commit

Permalink
Fix the detection of role vars missing role prefix (#3750)
Browse files Browse the repository at this point in the history
Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
cavcrosby and ssbarnea authored Sep 19, 2023
1 parent 0d61898 commit eb35080
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
Expand Down
2 changes: 2 additions & 0 deletions examples/roles/role_vars_prefix_detection/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
foo: bar
3 changes: 3 additions & 0 deletions examples/roles/role_vars_prefix_detection/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
role_vars_prefix_detection_bar: baz
bar: baz
15 changes: 14 additions & 1 deletion src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit eb35080

Please sign in to comment.