Skip to content

Commit 8756033

Browse files
authored
Document skip_ansible_lint does not work with yamllint rule (#3144)
1 parent c4834c4 commit 8756033

File tree

5 files changed

+53
-37
lines changed

5 files changed

+53
-37
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
7171
# Number of expected test passes, safety measure for accidental skip of
7272
# tests. Update value if you add/remove tests.
73-
PYTEST_REQPASS: 799
73+
PYTEST_REQPASS: 801
7474
steps:
7575
- name: Activate WSL1
7676
if: "contains(matrix.shell, 'wsl')"

examples/playbooks/rule-yaml-fail.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
- name: Fixture for yaml rule that should generate 3 errors
3+
# https://github.com/ansible/ansible-lint/issues/3139
4+
hosts: localhost
5+
tasks:
6+
- name: "1"
7+
ansible.builtin.debug:
8+
msg: yes
9+
10+
- name: "2"
11+
ansible.builtin.debug:
12+
msg: yes
13+
tags:
14+
- skip_ansible_lint # this has no effect for yamllint rule
15+
16+
- name: "3"
17+
ansible.builtin.debug:
18+
msg: yes

examples/playbooks/rule-yaml-pass.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
# yamllint disable rule:truthy
3+
- name: Fixture for yaml rule testing ability to use disable comments
4+
hosts: localhost
5+
tasks: []
6+
become: yes # <-- allowed only due to comment above
7+
# yamllint enable rule:truthy

src/ansiblelint/rules/yaml.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ warn_list:
2828
- yaml[document-start]
2929
```
3030

31+
!!! warning
32+
33+
You cannot use `tags: [skip_ansible_lint]` to disable this rule but you can
34+
use [yamllint magic comments](https://yamllint.readthedocs.io/en/stable/disable_with_comments.html#disabling-checks-for-all-or-part-of-the-file) for tuning it.
35+
3136
See the
3237
[list of yamllint rules](https://yamllint.readthedocs.io/en/stable/rules.html)
3338
for more information.

src/ansiblelint/rules/yaml_rule.py

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class YamllintRule(AnsibleLintRule):
3838
def matchyaml(self, file: Lintable) -> list[MatchError]:
3939
"""Return matches found for a specific YAML text."""
4040
matches: list[MatchError] = []
41-
filtered_matches: list[MatchError] = []
4241
if str(file.base_kind) != "text/yaml":
4342
return matches
4443

@@ -58,23 +57,7 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
5857
tag=f"yaml[{problem.rule}]",
5958
)
6059
)
61-
62-
# Now we save inside the file the skips, so they can be removed later,
63-
# especially as these skips can be about other rules than yaml one.
64-
_fetch_skips(file.data, file.line_skips)
65-
66-
for match in matches:
67-
last_skips = set()
68-
69-
for line, skips in file.line_skips.items():
70-
if line > match.linenumber:
71-
break
72-
last_skips = skips
73-
if last_skips.intersection({"skip_ansible_lint", match.rule.id, match.tag}):
74-
continue
75-
filtered_matches.append(match)
76-
77-
return filtered_matches
60+
return matches
7861

7962

8063
def _combine_skip_rules(data: Any) -> set[str]:
@@ -126,37 +109,40 @@ def _fetch_skips(data: Any, collector: dict[int, set[str]]) -> dict[int, set[str
126109
@pytest.mark.parametrize(
127110
("file", "expected_kind", "expected"),
128111
(
129-
(
112+
pytest.param(
130113
"examples/yamllint/invalid.yml",
131114
"yaml",
132115
[
133116
'Missing document start "---"',
134117
'Duplication of key "foo" in mapping',
135118
"Trailing spaces",
136119
],
120+
id="invalid",
137121
),
138-
(
139-
"examples/yamllint/valid.yml",
140-
"yaml",
141-
[],
122+
pytest.param("examples/yamllint/valid.yml", "yaml", [], id="valid"),
123+
pytest.param(
124+
"examples/yamllint/multi-document.yaml", "yaml", [], id="multi-document"
142125
),
143-
(
144-
"examples/yamllint/multi-document.yaml",
145-
"yaml",
146-
[],
126+
pytest.param(
127+
"examples/yamllint/skipped-rule.yml", "yaml", [], id="skipped-rule"
147128
),
148-
(
149-
"examples/yamllint/skipped-rule.yml",
150-
"yaml",
129+
pytest.param(
130+
"examples/playbooks/rule-yaml-fail.yml",
131+
"playbook",
132+
[
133+
"Truthy value should be one of [false, true]",
134+
"Truthy value should be one of [false, true]",
135+
"Truthy value should be one of [false, true]",
136+
],
137+
id="rule-yaml-fail",
138+
),
139+
pytest.param(
140+
"examples/playbooks/rule-yaml-pass.yml",
141+
"playbook",
151142
[],
143+
id="rule-yaml-pass",
152144
),
153145
),
154-
ids=(
155-
"invalid",
156-
"valid",
157-
"multi-document",
158-
"skipped-rule",
159-
),
160146
)
161147
def test_yamllint(file: str, expected_kind: str, expected: list[str]) -> None:
162148
"""Validate parsing of ansible output."""

0 commit comments

Comments
 (0)