Skip to content

Commit

Permalink
Avoid checking var-names on non ansible files (#2856)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Jan 1, 2023
1 parent be5ed6c commit ba769c8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Sorin
Sshell
TOXENV
TYPECHECK
Taskfiles
Tsukinowa
Tóth
WSLENV
Expand Down Expand Up @@ -306,6 +307,7 @@ subtest
supervisorctl
synchronize
sysvinit
taskfile
taskhandler
taskimports
taskincludes
Expand Down
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:
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: 742
PYTEST_REQPASS: 743

steps:
- name: Activate WSL1
Expand Down
8 changes: 8 additions & 0 deletions examples/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Sample taskfile, for testing linter ability to identify it
version: "3"
output: group
vars:
HOSTNAME: # <-- this is valid for Taskfiles but not for ansible files
sh: echo ${HOSTNAME:-localhost}
tasks: {}
20 changes: 14 additions & 6 deletions src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from ansiblelint.config import options
from ansiblelint.constants import LINE_NUMBER_KEY, SUCCESS_RC
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule
from ansiblelint.rules import AnsibleLintRule, RulesCollection
from ansiblelint.runner import Runner
from ansiblelint.skip_utils import get_rule_skips_from_line
from ansiblelint.utils import parse_yaml_from_file

Expand Down Expand Up @@ -74,7 +75,7 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
results: list[MatchError] = []
raw_results: list[MatchError] = []

if not data:
if not data or file.kind not in ("tasks", "handlers", "playbook", "vars"):
return results
# If the Play uses the 'vars' section to set variables
our_vars = data.get("vars", {})
Expand Down Expand Up @@ -193,12 +194,19 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
)

@pytest.mark.parametrize(
"rule_runner", (VariableNamingRule,), indirect=["rule_runner"]
("file", "expected"),
(
pytest.param("examples/playbooks/rule-var-naming-fail.yml", 7, id="0"),
pytest.param("examples/Taskfile.yml", 0, id="1"),
),
)
def test_invalid_var_name_playbook(rule_runner: RunFromText) -> None:
def test_invalid_var_name_playbook(file: str, expected: int) -> None:
"""Test rule matches."""
results = rule_runner.run("examples/playbooks/rule-var-naming-fail.yml")
assert len(results) == 7
rules = RulesCollection(options=options)
rules.register(VariableNamingRule())
results = Runner(Lintable(file), rules=rules).run()
# results = rule_runner.run()
assert len(results) == expected
for result in results:
assert result.rule.id == VariableNamingRule.id
# We are not checking line numbers because they can vary between
Expand Down

0 comments on commit ba769c8

Please sign in to comment.