Skip to content

Commit

Permalink
Improve noqa comment detection (#2440)
Browse files Browse the repository at this point in the history
In addition to fixing known bug, this also makes galaxy version
error specify the correct line number.

Fixes: #2382
  • Loading branch information
ssbarnea authored Sep 18, 2022
1 parent e918feb commit 9614067
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: foo
namespace: bar
version: 1.2.3
version: 0.0.0 # noqa: galaxy[version-incorrect]
authors:
- John
readme: ../README.md
Expand Down
2 changes: 2 additions & 0 deletions src/ansiblelint/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ def __init__(
self.filename = ""
if filename:
if isinstance(filename, Lintable):
self.lintable = filename
self.filename = normpath(str(filename.path))
else:
self.filename = normpath(filename)
self.lintable = Lintable(self.filename)
self.rule = rule
self.ignored = False # If set it will be displayed but not counted as failure
# This can be used by rules that can report multiple errors type, so
Expand Down
6 changes: 4 additions & 2 deletions src/ansiblelint/rules/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ def matchplay(self, file: Lintable, data: odict[str, Any]) -> list[MatchError]:
filename=file,
)
]
if Version(data.get("version")) < Version("1.0.0"):
version = data.get("version")
if Version(version) < Version("1.0.0"):
return [
self.create_matcherror(
message="collection version should be greater than or equal to 1.0.0",
linenumber=data[LINE_NUMBER_KEY],
# pylint: disable=protected-access
linenumber=version._line_number,
tag="galaxy[version-incorrect]",
filename=file,
)
Expand Down
4 changes: 3 additions & 1 deletion src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ def worker(lintable: Lintable) -> list[MatchError]:
# remove any matches made inside excluded files
matches = list(
filter(
lambda match: not self.is_excluded(Lintable(match.filename)), matches
lambda match: not self.is_excluded(Lintable(match.filename))
and match.tag not in match.lintable.line_skips[match.linenumber],
matches,
)
)

Expand Down
30 changes: 25 additions & 5 deletions src/ansiblelint/skip_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# Module 'ruamel.yaml' does not explicitly export attribute 'YAML'; implicit reexport disabled
from ruamel.yaml import YAML
from ruamel.yaml.composer import ComposerError
from ruamel.yaml.tokens import CommentToken

from ansiblelint.config import used_old_tags
from ansiblelint.constants import NESTED_TASK_KEYS, PLAYBOOK_TASK_KEYWORDS, RENAMED_TAGS
Expand Down Expand Up @@ -119,9 +120,17 @@ def _append_skipped_rules( # noqa: max-complexity: 12
) -> AnsibleBaseYAMLObject | None:
# parse file text using 2nd parser library
ruamel_data = load_data(lintable.content)
skipped_rules = _get_rule_skips_from_yaml(ruamel_data)

if lintable.kind in ["yaml", "requirements", "vars", "meta", "reno", "test-meta"]:
skipped_rules = _get_rule_skips_from_yaml(ruamel_data, lintable)

if lintable.kind in [
"yaml",
"requirements",
"vars",
"meta",
"reno",
"test-meta",
"galaxy",
]:
# AnsibleMapping, dict
if hasattr(pyyaml_data, "get"):
pyyaml_data["skipped_rules"] = skipped_rules
Expand Down Expand Up @@ -166,7 +175,7 @@ def _append_skipped_rules( # noqa: max-complexity: 12

if pyyaml_task.get("name") != ruamel_task.get("name"):
raise RuntimeError("Error in matching skip comment to a task")
pyyaml_task["skipped_rules"] = _get_rule_skips_from_yaml(ruamel_task)
pyyaml_task["skipped_rules"] = _get_rule_skips_from_yaml(ruamel_task, lintable)

return pyyaml_data

Expand Down Expand Up @@ -205,7 +214,7 @@ def get_nested_tasks(task: Any) -> Generator[Any, None, None]:


def _get_rule_skips_from_yaml( # noqa: max-complexity: 12
yaml_input: Sequence[Any],
yaml_input: Sequence[Any], lintable: Lintable
) -> Sequence[Any]:
"""Traverse yaml for comments with rule skips and return list of rules."""
yaml_comment_obj_strings = []
Expand All @@ -214,6 +223,17 @@ def _get_rule_skips_from_yaml( # noqa: max-complexity: 12
return []

def traverse_yaml(obj: Any) -> None:
for _, entry in obj.ca.items.items():
for v in entry:
if isinstance(v, CommentToken):
comment_str = v.value
if comment_str.startswith("# noqa:"):
line = v.start_mark.line + 1 # ruamel line numbers start at 0
# column = v.start_mark.column + 1 # ruamel column numbers start at 0
lintable.line_skips[line].update(
get_rule_skips_from_line(comment_str.strip())
)

yaml_comment_obj_strings.append(str(obj.ca.items))
if isinstance(obj, dict):
for _, val in obj.items():
Expand Down

0 comments on commit 9614067

Please sign in to comment.