Skip to content

Commit ba769c8

Browse files
authored
Avoid checking var-names on non ansible files (#2856)
1 parent be5ed6c commit ba769c8

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

.config/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Sorin
2626
Sshell
2727
TOXENV
2828
TYPECHECK
29+
Taskfiles
2930
Tsukinowa
3031
Tóth
3132
WSLENV
@@ -306,6 +307,7 @@ subtest
306307
supervisorctl
307308
synchronize
308309
sysvinit
310+
taskfile
309311
taskhandler
310312
taskimports
311313
taskincludes

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
7070
# Number of expected test passes, safety measure for accidental skip of
7171
# tests. Update value if you add/remove tests.
72-
PYTEST_REQPASS: 742
72+
PYTEST_REQPASS: 743
7373

7474
steps:
7575
- name: Activate WSL1

examples/Taskfile.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Sample taskfile, for testing linter ability to identify it
3+
version: "3"
4+
output: group
5+
vars:
6+
HOSTNAME: # <-- this is valid for Taskfiles but not for ansible files
7+
sh: echo ${HOSTNAME:-localhost}
8+
tasks: {}

src/ansiblelint/rules/var_naming.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from ansiblelint.config import options
1212
from ansiblelint.constants import LINE_NUMBER_KEY, SUCCESS_RC
1313
from ansiblelint.file_utils import Lintable
14-
from ansiblelint.rules import AnsibleLintRule
14+
from ansiblelint.rules import AnsibleLintRule, RulesCollection
15+
from ansiblelint.runner import Runner
1516
from ansiblelint.skip_utils import get_rule_skips_from_line
1617
from ansiblelint.utils import parse_yaml_from_file
1718

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

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

195196
@pytest.mark.parametrize(
196-
"rule_runner", (VariableNamingRule,), indirect=["rule_runner"]
197+
("file", "expected"),
198+
(
199+
pytest.param("examples/playbooks/rule-var-naming-fail.yml", 7, id="0"),
200+
pytest.param("examples/Taskfile.yml", 0, id="1"),
201+
),
197202
)
198-
def test_invalid_var_name_playbook(rule_runner: RunFromText) -> None:
203+
def test_invalid_var_name_playbook(file: str, expected: int) -> None:
199204
"""Test rule matches."""
200-
results = rule_runner.run("examples/playbooks/rule-var-naming-fail.yml")
201-
assert len(results) == 7
205+
rules = RulesCollection(options=options)
206+
rules.register(VariableNamingRule())
207+
results = Runner(Lintable(file), rules=rules).run()
208+
# results = rule_runner.run()
209+
assert len(results) == expected
202210
for result in results:
203211
assert result.rule.id == VariableNamingRule.id
204212
# We are not checking line numbers because they can vary between

0 commit comments

Comments
 (0)