Skip to content

Commit

Permalink
Change meta rules to use matchyaml instead of matchplay (#2296)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Aug 15, 2022
1 parent 4a24ec4 commit 39c077a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
7 changes: 7 additions & 0 deletions examples/roles/meta_noqa/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
galaxy_info: # noqa meta-incorrect
author: your name # noqa meta-no-info
description: missing min_ansible_version and platforms. author default not changed
license: MIT
min_ansible_version: "2.10"
platforms: []
2 changes: 2 additions & 0 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ def run( # noqa: max-complexity: 12
]

for rule in self.rules:
if rule.id == "syntax-check":
continue
if (
not tags
or rule.has_dynamic_tags
Expand Down
8 changes: 5 additions & 3 deletions src/ansiblelint/rules/meta_incorrect.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,24 @@ class MetaChangeFromDefaultRule(AnsibleLintRule):
tags = ["metadata"]
version_added = "v4.0.0"

def matchplay(self, file: Lintable, data: odict[str, Any]) -> List[MatchError]:
def matchyaml(self, file: Lintable) -> List[MatchError]:
if file.kind != "meta":
return []

galaxy_info = data.get("galaxy_info", None)
galaxy_info = file.data.get("galaxy_info", None)
if not galaxy_info:
return []

results = []
for field, default in self.field_defaults:
value = galaxy_info.get(field, None)
if value and value == default:
if "meta-incorrect" in file.data.get("skipped_rules", []):
continue
results.append(
self.create_matcherror(
filename=file,
linenumber=data[LINE_NUMBER_KEY],
linenumber=file.data[LINE_NUMBER_KEY],
message=f"Should change default metadata: {field}",
)
)
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/meta_no_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class MetaTagValidRule(AnsibleLintRule):

TAG_REGEXP = re.compile("^[a-z0-9]+$")

def matchplay(self, file: Lintable, data: odict[str, Any]) -> List[MatchError]:
def matchyaml(self, file: Lintable) -> List[MatchError]:
"""Find violations inside meta files."""
if file.kind != "meta":
return []

galaxy_info = data.get("galaxy_info", None)
galaxy_info = file.data.get("galaxy_info", None)
if not galaxy_info:
return []

Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/rules/meta_video_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class MetaVideoLinksRule(AnsibleLintRule):
"youtube": re.compile(r"https://youtu\.be/([0-9A-Za-z-_]+)"),
}

def matchplay(self, file: Lintable, data: odict[str, Any]) -> List[MatchError]:
def matchyaml(self, file: Lintable) -> List[MatchError]:
if file.kind != "meta":
return []

galaxy_info = data.get("galaxy_info", None)
galaxy_info = file.data.get("galaxy_info", None)
if not galaxy_info:
return []

Expand Down
23 changes: 8 additions & 15 deletions test/test_skip_inside_yaml.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tests related to use of inline noqa."""
import pytest

from ansiblelint.testing import RunFromText
from ansiblelint.testing import RunFromText, run_ansible_lint

ROLE_TASKS = """\
---
Expand Down Expand Up @@ -74,16 +74,6 @@
- skip_ansible_lint
"""

ROLE_META = """\
---
galaxy_info: # noqa meta-no-info
author: your name # noqa meta-incorrect
description: missing min_ansible_version and platforms. author default not changed
license: MIT
min_ansible_version: "2.10"
platforms: []
"""

ROLE_TASKS_WITH_BLOCK_BECOME = """\
---
- hosts: localhost
Expand Down Expand Up @@ -129,7 +119,10 @@ def test_playbook(
assert len(results) == results_num


def test_role_meta(default_text_runner: RunFromText) -> None:
"""Check that role meta can contain skips."""
results = default_text_runner.run_role_meta_main(ROLE_META)
assert len(results) == 0
def test_role_meta() -> None:
"""Test running from inside meta folder."""
role_path = "examples/roles/meta_noqa"

result = run_ansible_lint("-v", role_path)
assert len(result.stdout) == 0
assert result.returncode == 0

0 comments on commit 39c077a

Please sign in to comment.